blob: 5ceade2e8232f186d46e066a832ea3505fd4e912 [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"
Craig Topperc1f6f422012-03-17 07:33:42 +000016#include "ARMISelLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000017#include "ARM.h"
Eric Christopher6f2ccef2010-09-10 22:42:06 +000018#include "ARMCallingConv.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMConstantPoolValue.h"
Evan Chenga8e29892007-01-19 07:51:42 +000020#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +000021#include "ARMPerfectShuffle.h"
Evan Chenga8e29892007-01-19 07:51:42 +000022#include "ARMSubtarget.h"
23#include "ARMTargetMachine.h"
Chris Lattner80ec2792009-08-02 00:34:36 +000024#include "ARMTargetObjectFile.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000025#include "MCTargetDesc/ARMAddressingModes.h"
Evan Chenga8e29892007-01-19 07:51:42 +000026#include "llvm/CallingConv.h"
27#include "llvm/Constants.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000028#include "llvm/Function.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000029#include "llvm/GlobalValue.h"
Evan Cheng27707472007-03-16 08:43:56 +000030#include "llvm/Instruction.h"
Bob Wilson65ffec42010-09-21 17:56:22 +000031#include "llvm/Instructions.h"
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +000032#include "llvm/Intrinsics.h"
Benjamin Kramer174101e2009-10-20 11:44:38 +000033#include "llvm/Type.h"
Bob Wilson1f595bb2009-04-17 19:07:39 +000034#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng55d42002011-01-08 01:24:27 +000035#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Chenga8e29892007-01-19 07:51:42 +000036#include "llvm/CodeGen/MachineBasicBlock.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
38#include "llvm/CodeGen/MachineFunction.h"
39#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling2a850152011-10-05 00:02:33 +000040#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000041#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000042#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling94a1c632010-03-09 02:46:12 +000043#include "llvm/MC/MCSectionMachO.h"
Evan Chengb6ab2542007-01-31 08:40:13 +000044#include "llvm/Target/TargetOptions.h"
Evan Cheng55d42002011-01-08 01:24:27 +000045#include "llvm/ADT/StringExtras.h"
Dale Johannesen51e28e62010-06-03 21:09:53 +000046#include "llvm/ADT/Statistic.h"
Jim Grosbache7b52522010-04-14 22:28:31 +000047#include "llvm/Support/CommandLine.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000048#include "llvm/Support/ErrorHandling.h"
Evan Chengb01fad62007-03-12 23:30:29 +000049#include "llvm/Support/MathExtras.h"
Jim Grosbache801dc42009-12-12 01:40:06 +000050#include "llvm/Support/raw_ostream.h"
Evan Chenga8e29892007-01-19 07:51:42 +000051using namespace llvm;
52
Dale Johannesen51e28e62010-06-03 21:09:53 +000053STATISTIC(NumTailCalls, "Number of tail calls");
Evan Chengfc8475b2011-01-19 02:16:49 +000054STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Dale Johannesen51e28e62010-06-03 21:09:53 +000055
Bob Wilson703af3a2010-08-13 22:43:33 +000056// This option should go away when tail calls fully work.
57static cl::opt<bool>
58EnableARMTailCalls("arm-tail-calls", cl::Hidden,
59 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
60 cl::init(false));
61
Eric Christopher836c6242010-12-15 23:47:29 +000062cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000063EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000064 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000065 cl::init(false));
66
Evan Cheng46df4eb2010-06-16 07:35:02 +000067static cl::opt<bool>
68ARMInterworking("arm-interworking", cl::Hidden,
69 cl::desc("Enable / disable ARM interworking (for debugging only)"),
70 cl::init(true));
71
Benjamin Kramer0861f572011-11-26 23:01:57 +000072namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000073 class ARMCCState : public CCState {
74 public:
75 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
76 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
77 LLVMContext &C, ParmContext PC)
78 : CCState(CC, isVarArg, MF, TM, locs, C) {
79 assert(((PC == Call) || (PC == Prologue)) &&
80 "ARMCCState users must specify whether their context is call"
81 "or prologue generation.");
82 CallOrPrologue = PC;
83 }
84 };
85}
86
Stuart Hastingsc7315872011-04-20 16:47:52 +000087// The APCS parameter registers.
Craig Topperc5eaae42012-03-11 07:57:25 +000088static const uint16_t GPRArgRegs[] = {
Stuart Hastingsc7315872011-04-20 16:47:52 +000089 ARM::R0, ARM::R1, ARM::R2, ARM::R3
90};
91
Owen Andersone50ed302009-08-10 22:56:29 +000092void ARMTargetLowering::addTypeForNEON(EVT VT, EVT PromotedLdStVT,
93 EVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000094 if (VT != PromotedLdStVT) {
Owen Anderson70671842009-08-10 20:18:46 +000095 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +000096 AddPromotedToType (ISD::LOAD, VT.getSimpleVT(),
97 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +000098
Owen Anderson70671842009-08-10 20:18:46 +000099 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000100 AddPromotedToType (ISD::STORE, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000101 PromotedLdStVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000102 }
103
Owen Andersone50ed302009-08-10 22:56:29 +0000104 EVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Duncan Sands28b77e92011-09-06 19:07:46 +0000106 setOperationAction(ISD::SETCC, VT.getSimpleVT(), Custom);
Eli Friedman5c89cb82011-10-24 23:08:52 +0000107 setOperationAction(ISD::INSERT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Bob Wilson3468c2e2010-11-03 16:24:50 +0000108 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000109 if (ElemTy == MVT::i32) {
110 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Custom);
111 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Custom);
112 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Custom);
113 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Custom);
114 } else {
Bob Wilson0696fdf2009-09-16 20:20:44 +0000115 setOperationAction(ISD::SINT_TO_FP, VT.getSimpleVT(), Expand);
116 setOperationAction(ISD::UINT_TO_FP, VT.getSimpleVT(), Expand);
117 setOperationAction(ISD::FP_TO_SINT, VT.getSimpleVT(), Expand);
118 setOperationAction(ISD::FP_TO_UINT, VT.getSimpleVT(), Expand);
119 }
Owen Anderson70671842009-08-10 20:18:46 +0000120 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
121 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
Bob Wilson07f6e802010-06-16 21:34:01 +0000122 setOperationAction(ISD::CONCAT_VECTORS, VT.getSimpleVT(), Legal);
Bob Wilson5e8b8332011-01-07 04:59:04 +0000123 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT.getSimpleVT(), Legal);
Bob Wilsond0910c42010-04-06 22:02:24 +0000124 setOperationAction(ISD::SELECT, VT.getSimpleVT(), Expand);
125 setOperationAction(ISD::SELECT_CC, VT.getSimpleVT(), Expand);
Eli Friedman15f58c52011-11-11 03:16:38 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000127 if (VT.isInteger()) {
Owen Anderson70671842009-08-10 20:18:46 +0000128 setOperationAction(ISD::SHL, VT.getSimpleVT(), Custom);
129 setOperationAction(ISD::SRA, VT.getSimpleVT(), Custom);
130 setOperationAction(ISD::SRL, VT.getSimpleVT(), Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000131 }
132
133 // Promote all bit-wise operations.
134 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Owen Anderson70671842009-08-10 20:18:46 +0000135 setOperationAction(ISD::AND, VT.getSimpleVT(), Promote);
Owen Andersond6662ad2009-08-10 20:46:15 +0000136 AddPromotedToType (ISD::AND, VT.getSimpleVT(),
137 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000138 setOperationAction(ISD::OR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000139 AddPromotedToType (ISD::OR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000140 PromotedBitwiseVT.getSimpleVT());
Owen Anderson70671842009-08-10 20:18:46 +0000141 setOperationAction(ISD::XOR, VT.getSimpleVT(), Promote);
Jim Grosbach764ab522009-08-11 15:33:49 +0000142 AddPromotedToType (ISD::XOR, VT.getSimpleVT(),
Owen Andersond6662ad2009-08-10 20:46:15 +0000143 PromotedBitwiseVT.getSimpleVT());
Bob Wilson5bafff32009-06-22 23:27:02 +0000144 }
Bob Wilson16330762009-09-16 00:17:28 +0000145
146 // Neon does not support vector divide/remainder operations.
147 setOperationAction(ISD::SDIV, VT.getSimpleVT(), Expand);
148 setOperationAction(ISD::UDIV, VT.getSimpleVT(), Expand);
149 setOperationAction(ISD::FDIV, VT.getSimpleVT(), Expand);
150 setOperationAction(ISD::SREM, VT.getSimpleVT(), Expand);
151 setOperationAction(ISD::UREM, VT.getSimpleVT(), Expand);
152 setOperationAction(ISD::FREM, VT.getSimpleVT(), Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000153}
154
Owen Andersone50ed302009-08-10 22:56:29 +0000155void ARMTargetLowering::addDRTypeForNEON(EVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000156 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000158}
159
Owen Andersone50ed302009-08-10 22:56:29 +0000160void ARMTargetLowering::addQRTypeForNEON(EVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000161 addRegisterClass(VT, &ARM::QPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000163}
164
Chris Lattnerf0144122009-07-28 03:13:23 +0000165static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
166 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000167 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000168
Chris Lattner80ec2792009-08-02 00:34:36 +0000169 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000170}
171
Evan Chenga8e29892007-01-19 07:51:42 +0000172ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000173 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000174 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000175 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000176 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000177
Duncan Sands28b77e92011-09-06 19:07:46 +0000178 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
179
Evan Chengb1df8f22007-04-27 08:15:43 +0000180 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000181 // Uses VFP for Thumb libfuncs if available.
182 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
183 // Single-precision floating-point arithmetic.
184 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
185 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
186 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
187 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000188
Evan Chengb1df8f22007-04-27 08:15:43 +0000189 // Double-precision floating-point arithmetic.
190 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
191 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
192 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
193 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000194
Evan Chengb1df8f22007-04-27 08:15:43 +0000195 // Single-precision comparisons.
196 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
197 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
198 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
199 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
200 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
201 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
202 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
203 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000204
Evan Chengb1df8f22007-04-27 08:15:43 +0000205 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
210 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000213
Evan Chengb1df8f22007-04-27 08:15:43 +0000214 // Double-precision comparisons.
215 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
216 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
217 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
218 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
219 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
220 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
221 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
222 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000223
Evan Chengb1df8f22007-04-27 08:15:43 +0000224 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
229 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
230 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
231 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000232
Evan Chengb1df8f22007-04-27 08:15:43 +0000233 // Floating-point to integer conversions.
234 // i64 conversions are done via library routines even when generating VFP
235 // instructions, so use the same ones.
236 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
237 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
238 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
239 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000240
Evan Chengb1df8f22007-04-27 08:15:43 +0000241 // Conversions between floating types.
242 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
243 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
244
245 // Integer to floating-point conversions.
246 // i64 conversions are done via library routines even when generating VFP
247 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000248 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
249 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000250 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
251 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
252 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
253 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
254 }
Evan Chenga8e29892007-01-19 07:51:42 +0000255 }
256
Bob Wilson2f954612009-05-22 17:38:41 +0000257 // These libcalls are not available in 32-bit.
258 setLibcallName(RTLIB::SHL_I128, 0);
259 setLibcallName(RTLIB::SRL_I128, 0);
260 setLibcallName(RTLIB::SRA_I128, 0);
261
Evan Cheng07043272012-02-21 20:46:00 +0000262 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000263 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000264 // RTABI chapter 4.1.2, Table 2
265 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
266 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
267 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
268 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
269 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
270 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
271 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
272 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
273
274 // Double-precision floating-point comparison helper functions
275 // RTABI chapter 4.1.2, Table 3
276 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
277 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
278 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
279 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
280 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
281 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
282 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
283 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
284 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
285 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
286 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
287 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
288 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
289 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
290 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
291 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
292 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
297 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
298 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
299 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
300
301 // Single-precision floating-point arithmetic helper functions
302 // RTABI chapter 4.1.2, Table 4
303 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
304 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
305 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
306 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
307 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
308 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
309 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
310 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
311
312 // Single-precision floating-point comparison helper functions
313 // RTABI chapter 4.1.2, Table 5
314 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
315 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
316 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
317 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
318 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
319 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
320 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
321 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
322 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
323 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
324 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
325 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
326 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
327 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
328 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
329 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
330 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
335 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
336 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
337 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
338
339 // Floating-point to integer conversions.
340 // RTABI chapter 4.1.2, Table 6
341 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
342 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
343 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
344 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
345 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
346 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
347 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
348 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
349 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
354 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
355 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
356 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
357
358 // Conversions between floating types.
359 // RTABI chapter 4.1.2, Table 7
360 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
361 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
362 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000363 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000364
365 // Integer to floating-point conversions.
366 // RTABI chapter 4.1.2, Table 8
367 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
368 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
369 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
370 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
371 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
372 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
373 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
374 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
375 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
380 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
381 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
382 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
383
384 // Long long helper functions
385 // RTABI chapter 4.2, Table 9
386 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000387 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
388 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
389 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
390 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
393 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
394 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
395 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
396
397 // Integer division functions
398 // RTABI chapter 4.3.1
399 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
400 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
401 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000402 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000403 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
404 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
405 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000406 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000407 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
408 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000410 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000411 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
412 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000413 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000414 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000415
416 // Memory operations
417 // RTABI chapter 4.3.4
418 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
419 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
420 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000421 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
422 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
423 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000424 }
425
Bob Wilson2fef4572011-10-07 16:59:21 +0000426 // Use divmod compiler-rt calls for iOS 5.0 and later.
427 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
428 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
429 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
430 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
431 }
432
David Goodwinf1daf7d2009-07-08 23:10:31 +0000433 if (Subtarget->isThumb1Only())
Craig Topper420761a2012-04-20 07:30:17 +0000434 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000435 else
Craig Topper420761a2012-04-20 07:30:17 +0000436 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000437 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
438 !Subtarget->isThumb1Only()) {
Craig Topper420761a2012-04-20 07:30:17 +0000439 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000440 if (!Subtarget->isFPOnlySP())
Craig Topper420761a2012-04-20 07:30:17 +0000441 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000442
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000444 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000445
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000446 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
447 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
448 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
449 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
450 setTruncStoreAction((MVT::SimpleValueType)VT,
451 (MVT::SimpleValueType)InnerVT, Expand);
452 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
453 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
454 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
455 }
456
Lang Hames45b5f882012-03-15 18:49:02 +0000457 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
458
Bob Wilson5bafff32009-06-22 23:27:02 +0000459 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 addDRTypeForNEON(MVT::v2f32);
461 addDRTypeForNEON(MVT::v8i8);
462 addDRTypeForNEON(MVT::v4i16);
463 addDRTypeForNEON(MVT::v2i32);
464 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000465
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 addQRTypeForNEON(MVT::v4f32);
467 addQRTypeForNEON(MVT::v2f64);
468 addQRTypeForNEON(MVT::v16i8);
469 addQRTypeForNEON(MVT::v8i16);
470 addQRTypeForNEON(MVT::v4i32);
471 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000472
Bob Wilson74dc72e2009-09-15 23:55:57 +0000473 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
474 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000475 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
476 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000477 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
478 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
479 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000480 // FIXME: Code duplication: FDIV and FREM are expanded always, see
481 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000482 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
483 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000484 // FIXME: Create unittest.
485 // In another words, find a way when "copysign" appears in DAG with vector
486 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000487 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000488 // FIXME: Code duplication: SETCC has custom operation action, see
489 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000490 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000491 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000492 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
493 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
494 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
495 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
496 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
497 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
498 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
499 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
500 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
501 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
502 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
503 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000504 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000505 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
506 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
507 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
508 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
509 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000510
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000511 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
512 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
513 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
514 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
515 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
516 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
517 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
518 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
519 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
520 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000521
Bob Wilson642b3292009-09-16 00:32:15 +0000522 // Neon does not support some operations on v1i64 and v2i64 types.
523 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000524 // Custom handling for some quad-vector types to detect VMULL.
525 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
526 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
527 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000528 // Custom handling for some vector types to avoid expensive expansions
529 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
530 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
531 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
532 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000533 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
534 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000535 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000536 // a destination type that is wider than the source, and nor does
537 // it have a FP_TO_[SU]INT instruction with a narrower destination than
538 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000539 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
540 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000541 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
542 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000543
Bob Wilson1c3ef902011-02-07 17:43:21 +0000544 setTargetDAGCombine(ISD::INTRINSIC_VOID);
545 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000546 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
547 setTargetDAGCombine(ISD::SHL);
548 setTargetDAGCombine(ISD::SRL);
549 setTargetDAGCombine(ISD::SRA);
550 setTargetDAGCombine(ISD::SIGN_EXTEND);
551 setTargetDAGCombine(ISD::ZERO_EXTEND);
552 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000553 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000554 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000555 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000556 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
557 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000558 setTargetDAGCombine(ISD::FP_TO_SINT);
559 setTargetDAGCombine(ISD::FP_TO_UINT);
560 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000561
James Molloy873fd5f2012-02-20 09:24:05 +0000562 // It is legal to extload from v4i8 to v4i16 or v4i32.
563 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
564 MVT::v4i16, MVT::v2i16,
565 MVT::v2i32};
566 for (unsigned i = 0; i < 6; ++i) {
567 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
568 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
569 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
570 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000571 }
572
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000573 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000574
575 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000577
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000578 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000579 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000580
Evan Chenga8e29892007-01-19 07:51:42 +0000581 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000582 if (!Subtarget->isThumb1Only()) {
583 for (unsigned im = (unsigned)ISD::PRE_INC;
584 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setIndexedLoadAction(im, MVT::i1, Legal);
586 setIndexedLoadAction(im, MVT::i8, Legal);
587 setIndexedLoadAction(im, MVT::i16, Legal);
588 setIndexedLoadAction(im, MVT::i32, Legal);
589 setIndexedStoreAction(im, MVT::i1, Legal);
590 setIndexedStoreAction(im, MVT::i8, Legal);
591 setIndexedStoreAction(im, MVT::i16, Legal);
592 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000593 }
Evan Chenga8e29892007-01-19 07:51:42 +0000594 }
595
596 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000597 setOperationAction(ISD::MUL, MVT::i64, Expand);
598 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000599 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
601 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000602 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000603 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
604 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000605 setOperationAction(ISD::MULHS, MVT::i32, Expand);
606
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000607 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000608 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000609 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::SRL, MVT::i64, Custom);
611 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000612
Evan Cheng342e3162011-08-30 01:34:54 +0000613 if (!Subtarget->isThumb1Only()) {
614 // FIXME: We should do this for Thumb1 as well.
615 setOperationAction(ISD::ADDC, MVT::i32, Custom);
616 setOperationAction(ISD::ADDE, MVT::i32, Custom);
617 setOperationAction(ISD::SUBC, MVT::i32, Custom);
618 setOperationAction(ISD::SUBE, MVT::i32, Custom);
619 }
620
Evan Chenga8e29892007-01-19 07:51:42 +0000621 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000623 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000625 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000627
Chandler Carruth63974b22011-12-13 01:56:10 +0000628 // These just redirect to CTTZ and CTLZ on ARM.
629 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
630 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
631
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000632 // Only ARMv6 has BSWAP.
633 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000635
Evan Chenga8e29892007-01-19 07:51:42 +0000636 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000637 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000638 // v7M has a hardware divider
639 setOperationAction(ISD::SDIV, MVT::i32, Expand);
640 setOperationAction(ISD::UDIV, MVT::i32, Expand);
641 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::SREM, MVT::i32, Expand);
643 setOperationAction(ISD::UREM, MVT::i32, Expand);
644 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
645 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000646
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
648 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
649 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
650 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000651 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000652
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000653 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000654
Evan Chenga8e29892007-01-19 07:51:42 +0000655 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::VASTART, MVT::Other, Custom);
657 setOperationAction(ISD::VAARG, MVT::Other, Expand);
658 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
659 setOperationAction(ISD::VAEND, MVT::Other, Expand);
660 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
661 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000662
663 if (!Subtarget->isTargetDarwin()) {
664 // Non-Darwin platforms may return values in these registers via the
665 // personality function.
666 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
667 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
668 setExceptionPointerRegister(ARM::R0);
669 setExceptionSelectorRegister(ARM::R1);
670 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000671
Evan Cheng3a1588a2010-04-15 22:20:34 +0000672 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000673 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
674 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000675 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000676 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000677 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000678 // membarrier needs custom lowering; the rest are legal and handled
679 // normally.
680 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000681 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000682 // Custom lowering for 64-bit ops
683 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
684 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
685 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
686 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
687 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
688 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000689 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000690 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
691 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000692 } else {
693 // Set them all for expansion, which will force libcalls.
694 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000695 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000696 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000697 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000698 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000699 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000700 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000701 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000702 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000703 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000704 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000705 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000706 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000707 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000708 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
709 // Unordered/Monotonic case.
710 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
711 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000712 // Since the libcalls include locking, fold in the fences
713 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000714 }
Evan Chenga8e29892007-01-19 07:51:42 +0000715
Evan Cheng416941d2010-11-04 05:19:35 +0000716 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000717
Eli Friedmana2c6f452010-06-26 04:36:50 +0000718 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
719 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
721 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000722 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000723 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000724
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000725 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
726 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000727 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
728 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000729 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000730 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
731 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000732
733 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000734 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000735 if (Subtarget->isTargetDarwin()) {
736 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
737 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000738 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000739 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000740
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 setOperationAction(ISD::SETCC, MVT::i32, Expand);
742 setOperationAction(ISD::SETCC, MVT::f32, Expand);
743 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000744 setOperationAction(ISD::SELECT, MVT::i32, Custom);
745 setOperationAction(ISD::SELECT, MVT::f32, Custom);
746 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
748 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
749 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000750
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
752 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
753 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
754 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
755 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000756
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000757 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 setOperationAction(ISD::FSIN, MVT::f64, Expand);
759 setOperationAction(ISD::FSIN, MVT::f32, Expand);
760 setOperationAction(ISD::FCOS, MVT::f32, Expand);
761 setOperationAction(ISD::FCOS, MVT::f64, Expand);
762 setOperationAction(ISD::FREM, MVT::f64, Expand);
763 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000764 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
765 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
767 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000768 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::FPOW, MVT::f64, Expand);
770 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000771
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000772 if (!Subtarget->hasVFP4()) {
773 setOperationAction(ISD::FMA, MVT::f64, Expand);
774 setOperationAction(ISD::FMA, MVT::f32, Expand);
775 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000776
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:
Craig Topper420761a2012-04-20 07:30:17 +0000852 RRC = &ARM::DPRRegClass;
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:
Craig Topper420761a2012-04-20 07:30:17 +0000862 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000863 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000864 break;
865 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000866 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000867 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000868 break;
869 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000870 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000871 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)
Craig Topper420761a2012-04-20 07:30:17 +00001030 return &ARM::QQPRRegClass;
1031 if (VT == MVT::v8i64)
1032 return &ARM::QQQQPRRegClass;
Evan Cheng4782b1e2010-05-15 02:20:21 +00001033 }
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
Craig Topperc89c7442012-03-27 07:21:54 +00001646ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001647 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,
Craig Topperacf20772012-03-25 23:49:58 +00001676 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001677 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();
Craig Topperacf20772012-03-25 23:49:58 +00001811 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001812 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1813 i != e;
1814 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001815 CCValAssign &VA = ArgLocs[i];
1816 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001817 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001818 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001819 if (VA.getLocInfo() == CCValAssign::Indirect)
1820 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001821 if (VA.needsCustom()) {
1822 // f64 and vector types are split into multiple registers or
1823 // register/stack-slot combinations. The types will not match
1824 // the registers; give up on memory f64 refs until we figure
1825 // out what to do about this.
1826 if (!VA.isRegLoc())
1827 return false;
1828 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001829 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001830 if (RegVT == MVT::v2f64) {
1831 if (!ArgLocs[++i].isRegLoc())
1832 return false;
1833 if (!ArgLocs[++i].isRegLoc())
1834 return false;
1835 }
1836 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001837 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1838 MFI, MRI, TII))
1839 return false;
1840 }
1841 }
1842 }
1843 }
1844
1845 return true;
1846}
1847
Dan Gohman98ca4f22009-08-05 01:29:28 +00001848SDValue
1849ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001850 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001851 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001852 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001853 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001854
Bob Wilsondee46d72009-04-17 20:35:10 +00001855 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001856 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001857
Bob Wilsondee46d72009-04-17 20:35:10 +00001858 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001859 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1860 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001861
Dan Gohman98ca4f22009-08-05 01:29:28 +00001862 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001863 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1864 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001865
1866 // If this is the first return lowered for this function, add
1867 // the regs to the liveout set for the function.
1868 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1869 for (unsigned i = 0; i != RVLocs.size(); ++i)
1870 if (RVLocs[i].isRegLoc())
1871 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001872 }
1873
Bob Wilson1f595bb2009-04-17 19:07:39 +00001874 SDValue Flag;
1875
1876 // Copy the result values into the output registers.
1877 for (unsigned i = 0, realRVLocIdx = 0;
1878 i != RVLocs.size();
1879 ++i, ++realRVLocIdx) {
1880 CCValAssign &VA = RVLocs[i];
1881 assert(VA.isRegLoc() && "Can only return in registers!");
1882
Dan Gohmanc9403652010-07-07 15:54:55 +00001883 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001884
1885 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001886 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001887 case CCValAssign::Full: break;
1888 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001889 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001890 break;
1891 }
1892
Bob Wilson1f595bb2009-04-17 19:07:39 +00001893 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001895 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1897 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001898 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001899 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001900
1901 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1902 Flag = Chain.getValue(1);
1903 VA = RVLocs[++i]; // skip ahead to next loc
1904 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1905 HalfGPRs.getValue(1), Flag);
1906 Flag = Chain.getValue(1);
1907 VA = RVLocs[++i]; // skip ahead to next loc
1908
1909 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1911 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001912 }
1913 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1914 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001915 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001916 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001917 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001918 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001919 VA = RVLocs[++i]; // skip ahead to next loc
1920 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1921 Flag);
1922 } else
1923 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1924
Bob Wilsondee46d72009-04-17 20:35:10 +00001925 // Guarantee that all emitted copies are
1926 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001927 Flag = Chain.getValue(1);
1928 }
1929
1930 SDValue result;
1931 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001932 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001933 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001934 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001935
1936 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001937}
1938
Evan Chengbf010eb2012-04-10 01:51:00 +00001939bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001940 if (N->getNumValues() != 1)
1941 return false;
1942 if (!N->hasNUsesOfValue(1, 0))
1943 return false;
1944
Evan Chengbf010eb2012-04-10 01:51:00 +00001945 SDValue TCChain = Chain;
1946 SDNode *Copy = *N->use_begin();
1947 if (Copy->getOpcode() == ISD::CopyToReg) {
1948 // If the copy has a glue operand, we conservatively assume it isn't safe to
1949 // perform a tail call.
1950 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1951 return false;
1952 TCChain = Copy->getOperand(0);
1953 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1954 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001955 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001956 SmallPtrSet<SDNode*, 2> Copies;
1957 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001958 UI != UE; ++UI) {
1959 if (UI->getOpcode() != ISD::CopyToReg)
1960 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001961 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001962 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001963 if (Copies.size() > 2)
1964 return false;
1965
1966 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1967 UI != UE; ++UI) {
1968 SDValue UseChain = UI->getOperand(0);
1969 if (Copies.count(UseChain.getNode()))
1970 // Second CopyToReg
1971 Copy = *UI;
1972 else
1973 // First CopyToReg
1974 TCChain = UseChain;
1975 }
1976 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001977 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001978 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001979 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001980 Copy = *Copy->use_begin();
1981 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001982 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001983 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001984 } else {
1985 return false;
1986 }
1987
Evan Cheng1bf891a2010-12-01 22:59:46 +00001988 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001989 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
1990 UI != UE; ++UI) {
1991 if (UI->getOpcode() != ARMISD::RET_FLAG)
1992 return false;
1993 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001994 }
1995
Evan Chengbf010eb2012-04-10 01:51:00 +00001996 if (!HasRet)
1997 return false;
1998
1999 Chain = TCChain;
2000 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002001}
2002
Evan Cheng485fafc2011-03-21 01:19:09 +00002003bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002004 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002005 return false;
2006
2007 if (!CI->isTailCall())
2008 return false;
2009
2010 return !Subtarget->isThumb1Only();
2011}
2012
Bob Wilsonb62d2572009-11-03 00:02:05 +00002013// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2014// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2015// one of the above mentioned nodes. It has to be wrapped because otherwise
2016// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2017// be used to form addressing mode. These wrapped nodes will be selected
2018// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002019static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002020 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002021 // FIXME there is no actual debug info here
2022 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002023 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002024 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002025 if (CP->isMachineConstantPoolEntry())
2026 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2027 CP->getAlignment());
2028 else
2029 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2030 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002031 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002032}
2033
Jim Grosbache1102ca2010-07-19 17:20:38 +00002034unsigned ARMTargetLowering::getJumpTableEncoding() const {
2035 return MachineJumpTableInfo::EK_Inline;
2036}
2037
Dan Gohmand858e902010-04-17 15:26:15 +00002038SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2039 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002040 MachineFunction &MF = DAG.getMachineFunction();
2041 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2042 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002043 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002044 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002045 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002046 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2047 SDValue CPAddr;
2048 if (RelocM == Reloc::Static) {
2049 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2050 } else {
2051 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002052 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002053 ARMConstantPoolValue *CPV =
2054 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2055 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002056 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2057 }
2058 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2059 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002060 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002061 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002062 if (RelocM == Reloc::Static)
2063 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002064 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002065 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002066}
2067
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002068// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002069SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002070ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002071 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002072 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002073 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002074 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002075 MachineFunction &MF = DAG.getMachineFunction();
2076 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002077 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002078 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002079 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2080 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002081 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002082 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002083 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002084 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002085 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002086 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002087
Evan Chenge7e0d622009-11-06 22:24:13 +00002088 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002089 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002090
2091 // call __tls_get_addr.
2092 ArgListTy Args;
2093 ArgListEntry Entry;
2094 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002095 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002096 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002097 // FIXME: is there useful debug info available here?
Dan Gohman475871a2008-07-27 21:46:04 +00002098 std::pair<SDValue, SDValue> CallResult =
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002099 LowerCallTo(Chain, (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002100 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002101 0, CallingConv::C, /*isTailCall=*/false,
2102 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002103 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002104 return CallResult.first;
2105}
2106
2107// Lower ISD::GlobalTLSAddress using the "initial exec" or
2108// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002109SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002110ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002111 SelectionDAG &DAG,
2112 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002113 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002114 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002115 SDValue Offset;
2116 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002117 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002118 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002119 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002120
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002121 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002122 MachineFunction &MF = DAG.getMachineFunction();
2123 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002124 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002125 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002126 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2127 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002128 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2129 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2130 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002131 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002132 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002133 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002134 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002135 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002136 Chain = Offset.getValue(1);
2137
Evan Chenge7e0d622009-11-06 22:24:13 +00002138 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002139 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002140
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 } else {
2145 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002146 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002147 ARMConstantPoolValue *CPV =
2148 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002149 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002150 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002151 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002152 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002153 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002154 }
2155
2156 // The address of the thread local variable is the add of the thread
2157 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002158 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002159}
2160
Dan Gohman475871a2008-07-27 21:46:04 +00002161SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002162ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002163 // TODO: implement the "local dynamic" model
2164 assert(Subtarget->isTargetELF() &&
2165 "TLS not implemented for non-ELF targets");
2166 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002167
2168 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2169
2170 switch (model) {
2171 case TLSModel::GeneralDynamic:
2172 case TLSModel::LocalDynamic:
2173 return LowerToTLSGeneralDynamicModel(GA, DAG);
2174 case TLSModel::InitialExec:
2175 case TLSModel::LocalExec:
2176 return LowerToTLSExecModels(GA, DAG, model);
2177 }
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002178}
2179
Dan Gohman475871a2008-07-27 21:46:04 +00002180SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002181 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002182 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002183 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002184 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002185 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2186 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002187 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002188 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002189 ARMConstantPoolConstant::Create(GV,
2190 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002191 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002192 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002193 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002194 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002195 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002196 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002197 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002198 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002199 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002200 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002201 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002202 MachinePointerInfo::getGOT(),
2203 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002204 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002205 }
2206
2207 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002208 // pair. This is always cheaper.
2209 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002210 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002211 // FIXME: Once remat is capable of dealing with instructions with register
2212 // operands, expand this into two nodes.
2213 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2214 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002215 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002216 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2217 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2218 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2219 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002220 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002221 }
2222}
2223
Dan Gohman475871a2008-07-27 21:46:04 +00002224SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002225 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002226 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002227 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002228 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002229 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002230 MachineFunction &MF = DAG.getMachineFunction();
2231 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2232
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002233 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2234 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002235 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002236 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002237 // FIXME: Once remat is capable of dealing with instructions with register
2238 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002239 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002240 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2241 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2242
Evan Cheng53519f02011-01-21 18:55:51 +00002243 unsigned Wrapper = (RelocM == Reloc::PIC_)
2244 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2245 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002246 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002247 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2248 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002249 MachinePointerInfo::getGOT(),
2250 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002251 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002252 }
2253
2254 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002255 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002256 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002257 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002258 } else {
2259 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002260 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2261 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002262 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2263 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002264 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002265 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002266 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002267
Evan Cheng9eda6892009-10-31 03:39:36 +00002268 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002269 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002270 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002271 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002272
2273 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002274 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002275 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002276 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002277
Evan Cheng63476a82009-09-03 07:04:02 +00002278 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002279 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002280 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002281
2282 return Result;
2283}
2284
Dan Gohman475871a2008-07-27 21:46:04 +00002285SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002286 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002287 assert(Subtarget->isTargetELF() &&
2288 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002289 MachineFunction &MF = DAG.getMachineFunction();
2290 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002291 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002292 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002293 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002294 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002295 ARMConstantPoolValue *CPV =
2296 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2297 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002298 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002299 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002300 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002301 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002302 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002303 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002304 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002305}
2306
Jim Grosbach0e0da732009-05-12 23:59:14 +00002307SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002308ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2309 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002310 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002311 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2312 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002313 Op.getOperand(1), Val);
2314}
2315
2316SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002317ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2318 DebugLoc dl = Op.getDebugLoc();
2319 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2320 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2321}
2322
2323SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002324ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002325 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002326 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002327 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002328 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002329 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002330 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002331 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002332 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2333 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002334 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002335 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002336 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002337 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002338 EVT PtrVT = getPointerTy();
2339 DebugLoc dl = Op.getDebugLoc();
2340 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2341 SDValue CPAddr;
2342 unsigned PCAdj = (RelocM != Reloc::PIC_)
2343 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002344 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002345 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2346 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002347 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002348 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002349 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002350 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002351 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002352 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002353
2354 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002355 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002356 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2357 }
2358 return Result;
2359 }
Evan Cheng92e39162011-03-29 23:06:19 +00002360 case Intrinsic::arm_neon_vmulls:
2361 case Intrinsic::arm_neon_vmullu: {
2362 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2363 ? ARMISD::VMULLs : ARMISD::VMULLu;
2364 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2365 Op.getOperand(1), Op.getOperand(2));
2366 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002367 }
2368}
2369
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002370static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002371 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002372 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002373 if (!Subtarget->hasDataBarrier()) {
2374 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2375 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2376 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002377 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002378 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002379 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002380 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002381 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002382
2383 SDValue Op5 = Op.getOperand(5);
2384 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2385 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2386 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2387 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2388
2389 ARM_MB::MemBOpt DMBOpt;
2390 if (isDeviceBarrier)
2391 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2392 else
2393 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2394 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2395 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002396}
2397
Eli Friedman26689ac2011-08-03 21:06:02 +00002398
2399static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2400 const ARMSubtarget *Subtarget) {
2401 // FIXME: handle "fence singlethread" more efficiently.
2402 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002403 if (!Subtarget->hasDataBarrier()) {
2404 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2405 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2406 // here.
2407 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2408 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002409 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002410 DAG.getConstant(0, MVT::i32));
2411 }
2412
Eli Friedman26689ac2011-08-03 21:06:02 +00002413 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002414 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002415}
2416
Evan Chengdfed19f2010-11-03 06:34:55 +00002417static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2418 const ARMSubtarget *Subtarget) {
2419 // ARM pre v5TE and Thumb1 does not have preload instructions.
2420 if (!(Subtarget->isThumb2() ||
2421 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2422 // Just preserve the chain.
2423 return Op.getOperand(0);
2424
2425 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002426 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2427 if (!isRead &&
2428 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2429 // ARMv7 with MP extension has PLDW.
2430 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002431
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002432 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2433 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002434 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002435 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002436 isData = ~isData & 1;
2437 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002438
2439 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002440 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2441 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002442}
2443
Dan Gohman1e93df62010-04-17 14:41:14 +00002444static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2445 MachineFunction &MF = DAG.getMachineFunction();
2446 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2447
Evan Chenga8e29892007-01-19 07:51:42 +00002448 // vastart just stores the address of the VarArgsFrameIndex slot into the
2449 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002450 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002451 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002452 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002453 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002454 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2455 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002456}
2457
Dan Gohman475871a2008-07-27 21:46:04 +00002458SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002459ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2460 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002461 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002462 MachineFunction &MF = DAG.getMachineFunction();
2463 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2464
Craig Topper44d23822012-02-22 05:59:10 +00002465 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002466 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002467 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002468 else
Craig Topper420761a2012-04-20 07:30:17 +00002469 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002470
2471 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002472 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002473 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002474
2475 SDValue ArgValue2;
2476 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002477 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002478 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002479
2480 // Create load node to retrieve arguments from the stack.
2481 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002482 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002483 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002484 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002485 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002486 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002487 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002488 }
2489
Jim Grosbache5165492009-11-09 00:11:35 +00002490 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002491}
2492
Stuart Hastingsc7315872011-04-20 16:47:52 +00002493void
2494ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2495 unsigned &VARegSize, unsigned &VARegSaveSize)
2496 const {
2497 unsigned NumGPRs;
2498 if (CCInfo.isFirstByValRegValid())
2499 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2500 else {
2501 unsigned int firstUnalloced;
2502 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2503 sizeof(GPRArgRegs) /
2504 sizeof(GPRArgRegs[0]));
2505 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2506 }
2507
2508 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2509 VARegSize = NumGPRs * 4;
2510 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2511}
2512
2513// The remaining GPRs hold either the beginning of variable-argument
2514// data, or the beginning of an aggregate passed by value (usuall
2515// byval). Either way, we allocate stack slots adjacent to the data
2516// provided by our caller, and store the unallocated registers there.
2517// If this is a variadic function, the va_list pointer will begin with
2518// these values; otherwise, this reassembles a (byval) structure that
2519// was split between registers and memory.
2520void
2521ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2522 DebugLoc dl, SDValue &Chain,
2523 unsigned ArgOffset) const {
2524 MachineFunction &MF = DAG.getMachineFunction();
2525 MachineFrameInfo *MFI = MF.getFrameInfo();
2526 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2527 unsigned firstRegToSaveIndex;
2528 if (CCInfo.isFirstByValRegValid())
2529 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2530 else {
2531 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2532 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2533 }
2534
2535 unsigned VARegSize, VARegSaveSize;
2536 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2537 if (VARegSaveSize) {
2538 // If this function is vararg, store any remaining integer argument regs
2539 // to their spots on the stack so that they may be loaded by deferencing
2540 // the result of va_next.
2541 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002542 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2543 ArgOffset + VARegSaveSize
2544 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002545 false));
2546 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2547 getPointerTy());
2548
2549 SmallVector<SDValue, 4> MemOps;
2550 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002551 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002552 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002553 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002554 else
Craig Topper420761a2012-04-20 07:30:17 +00002555 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002556
2557 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2558 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2559 SDValue Store =
2560 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002561 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002562 false, false, 0);
2563 MemOps.push_back(Store);
2564 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2565 DAG.getConstant(4, getPointerTy()));
2566 }
2567 if (!MemOps.empty())
2568 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2569 &MemOps[0], MemOps.size());
2570 } else
2571 // This will point to the next argument passed via stack.
2572 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2573}
2574
Bob Wilson5bafff32009-06-22 23:27:02 +00002575SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002576ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002577 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002578 const SmallVectorImpl<ISD::InputArg>
2579 &Ins,
2580 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002581 SmallVectorImpl<SDValue> &InVals)
2582 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002583 MachineFunction &MF = DAG.getMachineFunction();
2584 MachineFrameInfo *MFI = MF.getFrameInfo();
2585
Bob Wilson1f595bb2009-04-17 19:07:39 +00002586 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2587
2588 // Assign locations to all of the incoming arguments.
2589 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002590 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2591 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002592 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002593 CCAssignFnForNode(CallConv, /* Return*/ false,
2594 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002595
2596 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002597 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002598
Stuart Hastingsf222e592011-02-28 17:17:53 +00002599 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002600 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2601 CCValAssign &VA = ArgLocs[i];
2602
Bob Wilsondee46d72009-04-17 20:35:10 +00002603 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002604 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002605 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002606
Bob Wilson1f595bb2009-04-17 19:07:39 +00002607 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002608 // f64 and vector types are split up into multiple registers or
2609 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002610 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002611 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002612 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002613 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002614 SDValue ArgValue2;
2615 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002616 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002617 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2618 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002619 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002620 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002621 } else {
2622 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2623 Chain, DAG, dl);
2624 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002625 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2626 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002627 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002628 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002629 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2630 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002631 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002632
Bob Wilson5bafff32009-06-22 23:27:02 +00002633 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002634 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002635
Owen Anderson825b72b2009-08-11 20:47:22 +00002636 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002637 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002638 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002639 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002640 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002641 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002642 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002643 RC = AFI->isThumb1OnlyFunction() ?
2644 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2645 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002646 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002647 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002648
2649 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002650 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002651 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002652 }
2653
2654 // If this is an 8 or 16-bit value, it is really passed promoted
2655 // to 32 bits. Insert an assert[sz]ext to capture this, then
2656 // truncate to the right size.
2657 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002658 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002659 case CCValAssign::Full: break;
2660 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002661 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002662 break;
2663 case CCValAssign::SExt:
2664 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2665 DAG.getValueType(VA.getValVT()));
2666 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2667 break;
2668 case CCValAssign::ZExt:
2669 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2670 DAG.getValueType(VA.getValVT()));
2671 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2672 break;
2673 }
2674
Dan Gohman98ca4f22009-08-05 01:29:28 +00002675 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002676
2677 } else { // VA.isRegLoc()
2678
2679 // sanity check
2680 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002681 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002682
Stuart Hastingsf222e592011-02-28 17:17:53 +00002683 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002684
Stuart Hastingsf222e592011-02-28 17:17:53 +00002685 // Some Ins[] entries become multiple ArgLoc[] entries.
2686 // Process them only once.
2687 if (index != lastInsIndex)
2688 {
2689 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002690 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002691 // This can be changed with more analysis.
2692 // In case of tail call optimization mark all arguments mutable.
2693 // Since they could be overwritten by lowering of arguments in case of
2694 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002695 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002696 unsigned VARegSize, VARegSaveSize;
2697 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2698 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2699 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002700 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002701 int FI = MFI->CreateFixedObject(Bytes,
2702 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002703 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2704 } else {
2705 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2706 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002707
Stuart Hastingsf222e592011-02-28 17:17:53 +00002708 // Create load nodes to retrieve arguments from the stack.
2709 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2710 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2711 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002712 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002713 }
2714 lastInsIndex = index;
2715 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002716 }
2717 }
2718
2719 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002720 if (isVarArg)
2721 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002722
Dan Gohman98ca4f22009-08-05 01:29:28 +00002723 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002724}
2725
2726/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002727static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002728 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002729 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002730 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002731 // Maybe this has already been legalized into the constant pool?
2732 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002733 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002734 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002735 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002736 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002737 }
2738 }
2739 return false;
2740}
2741
Evan Chenga8e29892007-01-19 07:51:42 +00002742/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2743/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002744SDValue
2745ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002746 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002747 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002748 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002749 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002750 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002751 // Constant does not fit, try adjusting it by one?
2752 switch (CC) {
2753 default: break;
2754 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002755 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002756 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002757 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002758 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002759 }
2760 break;
2761 case ISD::SETULT:
2762 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002763 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002764 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002765 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002766 }
2767 break;
2768 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002769 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002770 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002771 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002772 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002773 }
2774 break;
2775 case ISD::SETULE:
2776 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002777 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002778 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002779 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002780 }
2781 break;
2782 }
2783 }
2784 }
2785
2786 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002787 ARMISD::NodeType CompareType;
2788 switch (CondCode) {
2789 default:
2790 CompareType = ARMISD::CMP;
2791 break;
2792 case ARMCC::EQ:
2793 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002794 // Uses only Z Flag
2795 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002796 break;
2797 }
Evan Cheng218977b2010-07-13 19:27:42 +00002798 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002799 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002800}
2801
2802/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002803SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002804ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002805 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002806 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002807 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002808 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002809 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002810 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2811 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002812}
2813
Bob Wilson79f56c92011-03-08 01:17:20 +00002814/// duplicateCmp - Glue values can have only one use, so this function
2815/// duplicates a comparison node.
2816SDValue
2817ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2818 unsigned Opc = Cmp.getOpcode();
2819 DebugLoc DL = Cmp.getDebugLoc();
2820 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2821 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2822
2823 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2824 Cmp = Cmp.getOperand(0);
2825 Opc = Cmp.getOpcode();
2826 if (Opc == ARMISD::CMPFP)
2827 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2828 else {
2829 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2830 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2831 }
2832 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2833}
2834
Bill Wendlingde2b1512010-08-11 08:43:16 +00002835SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2836 SDValue Cond = Op.getOperand(0);
2837 SDValue SelectTrue = Op.getOperand(1);
2838 SDValue SelectFalse = Op.getOperand(2);
2839 DebugLoc dl = Op.getDebugLoc();
2840
2841 // Convert:
2842 //
2843 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2844 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2845 //
2846 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2847 const ConstantSDNode *CMOVTrue =
2848 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2849 const ConstantSDNode *CMOVFalse =
2850 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2851
2852 if (CMOVTrue && CMOVFalse) {
2853 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2854 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2855
2856 SDValue True;
2857 SDValue False;
2858 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2859 True = SelectTrue;
2860 False = SelectFalse;
2861 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2862 True = SelectFalse;
2863 False = SelectTrue;
2864 }
2865
2866 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002867 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002868 SDValue ARMcc = Cond.getOperand(2);
2869 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002870 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002871 assert(True.getValueType() == VT);
2872 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002873 }
2874 }
2875 }
2876
Dan Gohmandb953892012-02-24 00:09:36 +00002877 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2878 // undefined bits before doing a full-word comparison with zero.
2879 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2880 DAG.getConstant(1, Cond.getValueType()));
2881
Bill Wendlingde2b1512010-08-11 08:43:16 +00002882 return DAG.getSelectCC(dl, Cond,
2883 DAG.getConstant(0, Cond.getValueType()),
2884 SelectTrue, SelectFalse, ISD::SETNE);
2885}
2886
Dan Gohmand858e902010-04-17 15:26:15 +00002887SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002888 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002889 SDValue LHS = Op.getOperand(0);
2890 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002891 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002892 SDValue TrueVal = Op.getOperand(2);
2893 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002894 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002895
Owen Anderson825b72b2009-08-11 20:47:22 +00002896 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002897 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002898 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002899 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002900 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002901 }
2902
2903 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002904 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002905
Evan Cheng218977b2010-07-13 19:27:42 +00002906 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2907 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002908 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002909 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002910 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002911 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002912 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002913 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002914 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002915 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002916 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002917 }
2918 return Result;
2919}
2920
Evan Cheng218977b2010-07-13 19:27:42 +00002921/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2922/// to morph to an integer compare sequence.
2923static bool canChangeToInt(SDValue Op, bool &SeenZero,
2924 const ARMSubtarget *Subtarget) {
2925 SDNode *N = Op.getNode();
2926 if (!N->hasOneUse())
2927 // Otherwise it requires moving the value from fp to integer registers.
2928 return false;
2929 if (!N->getNumValues())
2930 return false;
2931 EVT VT = Op.getValueType();
2932 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2933 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2934 // vmrs are very slow, e.g. cortex-a8.
2935 return false;
2936
2937 if (isFloatingPointZero(Op)) {
2938 SeenZero = true;
2939 return true;
2940 }
2941 return ISD::isNormalLoad(N);
2942}
2943
2944static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2945 if (isFloatingPointZero(Op))
2946 return DAG.getConstant(0, MVT::i32);
2947
2948 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2949 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002950 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002951 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002952 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002953
2954 llvm_unreachable("Unknown VFP cmp argument!");
2955}
2956
2957static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2958 SDValue &RetVal1, SDValue &RetVal2) {
2959 if (isFloatingPointZero(Op)) {
2960 RetVal1 = DAG.getConstant(0, MVT::i32);
2961 RetVal2 = DAG.getConstant(0, MVT::i32);
2962 return;
2963 }
2964
2965 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2966 SDValue Ptr = Ld->getBasePtr();
2967 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2968 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002969 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002970 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002971 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002972
2973 EVT PtrType = Ptr.getValueType();
2974 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2975 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2976 PtrType, Ptr, DAG.getConstant(4, PtrType));
2977 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2978 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002979 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002980 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002981 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002982 return;
2983 }
2984
2985 llvm_unreachable("Unknown VFP cmp argument!");
2986}
2987
2988/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
2989/// f32 and even f64 comparisons to integer ones.
2990SDValue
2991ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
2992 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002993 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00002994 SDValue LHS = Op.getOperand(2);
2995 SDValue RHS = Op.getOperand(3);
2996 SDValue Dest = Op.getOperand(4);
2997 DebugLoc dl = Op.getDebugLoc();
2998
Evan Chengfc501a32012-03-01 23:27:13 +00002999 bool LHSSeenZero = false;
3000 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3001 bool RHSSeenZero = false;
3002 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3003 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003004 // If unsafe fp math optimization is enabled and there are no other uses of
3005 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003006 // to an integer comparison.
3007 if (CC == ISD::SETOEQ)
3008 CC = ISD::SETEQ;
3009 else if (CC == ISD::SETUNE)
3010 CC = ISD::SETNE;
3011
Evan Chengfc501a32012-03-01 23:27:13 +00003012 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003013 SDValue ARMcc;
3014 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003015 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3016 bitcastf32Toi32(LHS, DAG), Mask);
3017 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3018 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003019 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3020 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3021 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3022 Chain, Dest, ARMcc, CCR, Cmp);
3023 }
3024
3025 SDValue LHS1, LHS2;
3026 SDValue RHS1, RHS2;
3027 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3028 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003029 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3030 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003031 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3032 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003033 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003034 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3035 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3036 }
3037
3038 return SDValue();
3039}
3040
3041SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3042 SDValue Chain = Op.getOperand(0);
3043 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3044 SDValue LHS = Op.getOperand(2);
3045 SDValue RHS = Op.getOperand(3);
3046 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003047 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003048
Owen Anderson825b72b2009-08-11 20:47:22 +00003049 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003050 SDValue ARMcc;
3051 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003052 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003053 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003054 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003055 }
3056
Owen Anderson825b72b2009-08-11 20:47:22 +00003057 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003058
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003059 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003060 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3061 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3062 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3063 if (Result.getNode())
3064 return Result;
3065 }
3066
Evan Chenga8e29892007-01-19 07:51:42 +00003067 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003068 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003069
Evan Cheng218977b2010-07-13 19:27:42 +00003070 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3071 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003072 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003073 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003074 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003075 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003076 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003077 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3078 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003079 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003080 }
3081 return Res;
3082}
3083
Dan Gohmand858e902010-04-17 15:26:15 +00003084SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003085 SDValue Chain = Op.getOperand(0);
3086 SDValue Table = Op.getOperand(1);
3087 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003088 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003089
Owen Andersone50ed302009-08-10 22:56:29 +00003090 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003091 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3092 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003093 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003094 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003095 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003096 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3097 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003098 if (Subtarget->isThumb2()) {
3099 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3100 // which does another jump to the destination. This also makes it easier
3101 // to translate it to TBB / TBH later.
3102 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003103 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003104 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003105 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003106 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003107 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003108 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003109 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003110 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003111 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003112 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003113 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003114 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003115 MachinePointerInfo::getJumpTable(),
3116 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003117 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003118 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003119 }
Evan Chenga8e29892007-01-19 07:51:42 +00003120}
3121
Eli Friedman14e809c2011-11-09 23:36:02 +00003122static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003123 EVT VT = Op.getValueType();
3124 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003125
James Molloy873fd5f2012-02-20 09:24:05 +00003126 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3127 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3128 return Op;
3129 return DAG.UnrollVectorOp(Op.getNode());
3130 }
3131
3132 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3133 "Invalid type for custom lowering!");
3134 if (VT != MVT::v4i16)
3135 return DAG.UnrollVectorOp(Op.getNode());
3136
3137 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3138 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003139}
3140
Bob Wilson76a312b2010-03-19 22:51:32 +00003141static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003142 EVT VT = Op.getValueType();
3143 if (VT.isVector())
3144 return LowerVectorFP_TO_INT(Op, DAG);
3145
Bob Wilson76a312b2010-03-19 22:51:32 +00003146 DebugLoc dl = Op.getDebugLoc();
3147 unsigned Opc;
3148
3149 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003150 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003151 case ISD::FP_TO_SINT:
3152 Opc = ARMISD::FTOSI;
3153 break;
3154 case ISD::FP_TO_UINT:
3155 Opc = ARMISD::FTOUI;
3156 break;
3157 }
3158 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003159 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003160}
3161
Cameron Zwarich3007d332011-03-29 21:41:55 +00003162static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3163 EVT VT = Op.getValueType();
3164 DebugLoc dl = Op.getDebugLoc();
3165
Eli Friedman14e809c2011-11-09 23:36:02 +00003166 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3167 if (VT.getVectorElementType() == MVT::f32)
3168 return Op;
3169 return DAG.UnrollVectorOp(Op.getNode());
3170 }
3171
Duncan Sands1f6a3292011-08-12 14:54:45 +00003172 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3173 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003174 if (VT != MVT::v4f32)
3175 return DAG.UnrollVectorOp(Op.getNode());
3176
3177 unsigned CastOpc;
3178 unsigned Opc;
3179 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003180 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003181 case ISD::SINT_TO_FP:
3182 CastOpc = ISD::SIGN_EXTEND;
3183 Opc = ISD::SINT_TO_FP;
3184 break;
3185 case ISD::UINT_TO_FP:
3186 CastOpc = ISD::ZERO_EXTEND;
3187 Opc = ISD::UINT_TO_FP;
3188 break;
3189 }
3190
3191 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3192 return DAG.getNode(Opc, dl, VT, Op);
3193}
3194
Bob Wilson76a312b2010-03-19 22:51:32 +00003195static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3196 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003197 if (VT.isVector())
3198 return LowerVectorINT_TO_FP(Op, DAG);
3199
Bob Wilson76a312b2010-03-19 22:51:32 +00003200 DebugLoc dl = Op.getDebugLoc();
3201 unsigned Opc;
3202
3203 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003204 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003205 case ISD::SINT_TO_FP:
3206 Opc = ARMISD::SITOF;
3207 break;
3208 case ISD::UINT_TO_FP:
3209 Opc = ARMISD::UITOF;
3210 break;
3211 }
3212
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003213 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003214 return DAG.getNode(Opc, dl, VT, Op);
3215}
3216
Evan Cheng515fe3a2010-07-08 02:08:50 +00003217SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003218 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003219 SDValue Tmp0 = Op.getOperand(0);
3220 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003221 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003222 EVT VT = Op.getValueType();
3223 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003224 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3225 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3226 bool UseNEON = !InGPR && Subtarget->hasNEON();
3227
3228 if (UseNEON) {
3229 // Use VBSL to copy the sign bit.
3230 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3231 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3232 DAG.getTargetConstant(EncodedVal, MVT::i32));
3233 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3234 if (VT == MVT::f64)
3235 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3236 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3237 DAG.getConstant(32, MVT::i32));
3238 else /*if (VT == MVT::f32)*/
3239 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3240 if (SrcVT == MVT::f32) {
3241 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3242 if (VT == MVT::f64)
3243 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3244 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3245 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003246 } else if (VT == MVT::f32)
3247 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3248 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3249 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003250 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3251 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3252
3253 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3254 MVT::i32);
3255 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3256 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3257 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003258
Evan Chenge573fb32011-02-23 02:24:55 +00003259 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3260 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3261 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003262 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003263 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3264 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3265 DAG.getConstant(0, MVT::i32));
3266 } else {
3267 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3268 }
3269
3270 return Res;
3271 }
Evan Chengc143dd42011-02-11 02:28:55 +00003272
3273 // Bitcast operand 1 to i32.
3274 if (SrcVT == MVT::f64)
3275 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3276 &Tmp1, 1).getValue(1);
3277 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3278
Evan Chenge573fb32011-02-23 02:24:55 +00003279 // Or in the signbit with integer operations.
3280 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3281 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3282 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3283 if (VT == MVT::f32) {
3284 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3285 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3286 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3287 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003288 }
3289
Evan Chenge573fb32011-02-23 02:24:55 +00003290 // f64: Or the high part with signbit and then combine two parts.
3291 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3292 &Tmp0, 1);
3293 SDValue Lo = Tmp0.getValue(0);
3294 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3295 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3296 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003297}
3298
Evan Cheng2457f2c2010-05-22 01:47:14 +00003299SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3300 MachineFunction &MF = DAG.getMachineFunction();
3301 MachineFrameInfo *MFI = MF.getFrameInfo();
3302 MFI->setReturnAddressIsTaken(true);
3303
3304 EVT VT = Op.getValueType();
3305 DebugLoc dl = Op.getDebugLoc();
3306 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3307 if (Depth) {
3308 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3309 SDValue Offset = DAG.getConstant(4, MVT::i32);
3310 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3311 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003312 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003313 }
3314
3315 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003316 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003317 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3318}
3319
Dan Gohmand858e902010-04-17 15:26:15 +00003320SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003321 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3322 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003323
Owen Andersone50ed302009-08-10 22:56:29 +00003324 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003325 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3326 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003327 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003328 ? ARM::R7 : ARM::R11;
3329 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3330 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003331 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3332 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003333 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003334 return FrameAddr;
3335}
3336
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003337/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003338/// expand a bit convert where either the source or destination type is i64 to
3339/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3340/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3341/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003342static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003343 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3344 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003345 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003346
Bob Wilson9f3f0612010-04-17 05:30:19 +00003347 // This function is only supposed to be called for i64 types, either as the
3348 // source or destination of the bit convert.
3349 EVT SrcVT = Op.getValueType();
3350 EVT DstVT = N->getValueType(0);
3351 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003352 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003353
Bob Wilson9f3f0612010-04-17 05:30:19 +00003354 // Turn i64->f64 into VMOVDRR.
3355 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3357 DAG.getConstant(0, MVT::i32));
3358 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3359 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003360 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003361 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003362 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003363
Jim Grosbache5165492009-11-09 00:11:35 +00003364 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003365 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3366 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3367 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3368 // Merge the pieces into a single i64 value.
3369 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3370 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003371
Bob Wilson9f3f0612010-04-17 05:30:19 +00003372 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003373}
3374
Bob Wilson5bafff32009-06-22 23:27:02 +00003375/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003376/// Zero vectors are used to represent vector negation and in those cases
3377/// will be implemented with the NEON VNEG instruction. However, VNEG does
3378/// not support i64 elements, so sometimes the zero vectors will need to be
3379/// explicitly constructed. Regardless, use a canonical VMOV to create the
3380/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003381static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003382 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003383 // The canonical modified immediate encoding of a zero vector is....0!
3384 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3385 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3386 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003387 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003388}
3389
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003390/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3391/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003392SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3393 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003394 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3395 EVT VT = Op.getValueType();
3396 unsigned VTBits = VT.getSizeInBits();
3397 DebugLoc dl = Op.getDebugLoc();
3398 SDValue ShOpLo = Op.getOperand(0);
3399 SDValue ShOpHi = Op.getOperand(1);
3400 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003401 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003402 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003403
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003404 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3405
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003406 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3407 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3408 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3409 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3410 DAG.getConstant(VTBits, MVT::i32));
3411 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3412 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003413 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003414
3415 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3416 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003417 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003418 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003419 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003420 CCR, Cmp);
3421
3422 SDValue Ops[2] = { Lo, Hi };
3423 return DAG.getMergeValues(Ops, 2, dl);
3424}
3425
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003426/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3427/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003428SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3429 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003430 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3431 EVT VT = Op.getValueType();
3432 unsigned VTBits = VT.getSizeInBits();
3433 DebugLoc dl = Op.getDebugLoc();
3434 SDValue ShOpLo = Op.getOperand(0);
3435 SDValue ShOpHi = Op.getOperand(1);
3436 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003437 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003438
3439 assert(Op.getOpcode() == ISD::SHL_PARTS);
3440 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3441 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3442 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3443 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3444 DAG.getConstant(VTBits, MVT::i32));
3445 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3446 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3447
3448 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3449 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3450 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003451 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003452 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003453 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003454 CCR, Cmp);
3455
3456 SDValue Ops[2] = { Lo, Hi };
3457 return DAG.getMergeValues(Ops, 2, dl);
3458}
3459
Jim Grosbach4725ca72010-09-08 03:54:02 +00003460SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003461 SelectionDAG &DAG) const {
3462 // The rounding mode is in bits 23:22 of the FPSCR.
3463 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3464 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3465 // so that the shift + and get folded into a bitfield extract.
3466 DebugLoc dl = Op.getDebugLoc();
3467 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3468 DAG.getConstant(Intrinsic::arm_get_fpscr,
3469 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003470 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003471 DAG.getConstant(1U << 22, MVT::i32));
3472 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3473 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003474 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003475 DAG.getConstant(3, MVT::i32));
3476}
3477
Jim Grosbach3482c802010-01-18 19:58:49 +00003478static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3479 const ARMSubtarget *ST) {
3480 EVT VT = N->getValueType(0);
3481 DebugLoc dl = N->getDebugLoc();
3482
3483 if (!ST->hasV6T2Ops())
3484 return SDValue();
3485
3486 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3487 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3488}
3489
Bob Wilson5bafff32009-06-22 23:27:02 +00003490static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3491 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003492 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003493 DebugLoc dl = N->getDebugLoc();
3494
Bob Wilsond5448bb2010-11-18 21:16:28 +00003495 if (!VT.isVector())
3496 return SDValue();
3497
Bob Wilson5bafff32009-06-22 23:27:02 +00003498 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003499 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003500
Bob Wilsond5448bb2010-11-18 21:16:28 +00003501 // Left shifts translate directly to the vshiftu intrinsic.
3502 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003503 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003504 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3505 N->getOperand(0), N->getOperand(1));
3506
3507 assert((N->getOpcode() == ISD::SRA ||
3508 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3509
3510 // NEON uses the same intrinsics for both left and right shifts. For
3511 // right shifts, the shift amounts are negative, so negate the vector of
3512 // shift amounts.
3513 EVT ShiftVT = N->getOperand(1).getValueType();
3514 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3515 getZeroVector(ShiftVT, DAG, dl),
3516 N->getOperand(1));
3517 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3518 Intrinsic::arm_neon_vshifts :
3519 Intrinsic::arm_neon_vshiftu);
3520 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3521 DAG.getConstant(vshiftInt, MVT::i32),
3522 N->getOperand(0), NegatedCount);
3523}
3524
3525static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3526 const ARMSubtarget *ST) {
3527 EVT VT = N->getValueType(0);
3528 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003529
Eli Friedmance392eb2009-08-22 03:13:10 +00003530 // We can get here for a node like i32 = ISD::SHL i32, i64
3531 if (VT != MVT::i64)
3532 return SDValue();
3533
3534 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003535 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003536
Chris Lattner27a6c732007-11-24 07:07:01 +00003537 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3538 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003539 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003540 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003541
Chris Lattner27a6c732007-11-24 07:07:01 +00003542 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003543 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003544
Chris Lattner27a6c732007-11-24 07:07:01 +00003545 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003547 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003548 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003549 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003550
Chris Lattner27a6c732007-11-24 07:07:01 +00003551 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3552 // captures the result into a carry flag.
3553 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003554 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003555
Chris Lattner27a6c732007-11-24 07:07:01 +00003556 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003558
Chris Lattner27a6c732007-11-24 07:07:01 +00003559 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003561}
3562
Bob Wilson5bafff32009-06-22 23:27:02 +00003563static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3564 SDValue TmpOp0, TmpOp1;
3565 bool Invert = false;
3566 bool Swap = false;
3567 unsigned Opc = 0;
3568
3569 SDValue Op0 = Op.getOperand(0);
3570 SDValue Op1 = Op.getOperand(1);
3571 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003572 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003573 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3574 DebugLoc dl = Op.getDebugLoc();
3575
3576 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3577 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003578 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003579 case ISD::SETUNE:
3580 case ISD::SETNE: Invert = true; // Fallthrough
3581 case ISD::SETOEQ:
3582 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3583 case ISD::SETOLT:
3584 case ISD::SETLT: Swap = true; // Fallthrough
3585 case ISD::SETOGT:
3586 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3587 case ISD::SETOLE:
3588 case ISD::SETLE: Swap = true; // Fallthrough
3589 case ISD::SETOGE:
3590 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3591 case ISD::SETUGE: Swap = true; // Fallthrough
3592 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3593 case ISD::SETUGT: Swap = true; // Fallthrough
3594 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3595 case ISD::SETUEQ: Invert = true; // Fallthrough
3596 case ISD::SETONE:
3597 // Expand this to (OLT | OGT).
3598 TmpOp0 = Op0;
3599 TmpOp1 = Op1;
3600 Opc = ISD::OR;
3601 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3602 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3603 break;
3604 case ISD::SETUO: Invert = true; // Fallthrough
3605 case ISD::SETO:
3606 // Expand this to (OLT | OGE).
3607 TmpOp0 = Op0;
3608 TmpOp1 = Op1;
3609 Opc = ISD::OR;
3610 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3611 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3612 break;
3613 }
3614 } else {
3615 // Integer comparisons.
3616 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003617 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003618 case ISD::SETNE: Invert = true;
3619 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3620 case ISD::SETLT: Swap = true;
3621 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3622 case ISD::SETLE: Swap = true;
3623 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3624 case ISD::SETULT: Swap = true;
3625 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3626 case ISD::SETULE: Swap = true;
3627 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3628 }
3629
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003630 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003631 if (Opc == ARMISD::VCEQ) {
3632
3633 SDValue AndOp;
3634 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3635 AndOp = Op0;
3636 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3637 AndOp = Op1;
3638
3639 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003640 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003641 AndOp = AndOp.getOperand(0);
3642
3643 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3644 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003645 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3646 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003647 Invert = !Invert;
3648 }
3649 }
3650 }
3651
3652 if (Swap)
3653 std::swap(Op0, Op1);
3654
Owen Andersonc24cb352010-11-08 23:21:22 +00003655 // If one of the operands is a constant vector zero, attempt to fold the
3656 // comparison to a specialized compare-against-zero form.
3657 SDValue SingleOp;
3658 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3659 SingleOp = Op0;
3660 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3661 if (Opc == ARMISD::VCGE)
3662 Opc = ARMISD::VCLEZ;
3663 else if (Opc == ARMISD::VCGT)
3664 Opc = ARMISD::VCLTZ;
3665 SingleOp = Op1;
3666 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003667
Owen Andersonc24cb352010-11-08 23:21:22 +00003668 SDValue Result;
3669 if (SingleOp.getNode()) {
3670 switch (Opc) {
3671 case ARMISD::VCEQ:
3672 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3673 case ARMISD::VCGE:
3674 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3675 case ARMISD::VCLEZ:
3676 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3677 case ARMISD::VCGT:
3678 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3679 case ARMISD::VCLTZ:
3680 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3681 default:
3682 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3683 }
3684 } else {
3685 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3686 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003687
3688 if (Invert)
3689 Result = DAG.getNOT(dl, Result, VT);
3690
3691 return Result;
3692}
3693
Bob Wilsond3c42842010-06-14 22:19:57 +00003694/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3695/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003696/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003697static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3698 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003699 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003700 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003701
Bob Wilson827b2102010-06-15 19:05:35 +00003702 // SplatBitSize is set to the smallest size that splats the vector, so a
3703 // zero vector will always have SplatBitSize == 8. However, NEON modified
3704 // immediate instructions others than VMOV do not support the 8-bit encoding
3705 // of a zero vector, and the default encoding of zero is supposed to be the
3706 // 32-bit version.
3707 if (SplatBits == 0)
3708 SplatBitSize = 32;
3709
Bob Wilson5bafff32009-06-22 23:27:02 +00003710 switch (SplatBitSize) {
3711 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003712 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003713 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003714 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003715 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003716 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003717 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003718 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003719 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003720
3721 case 16:
3722 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003723 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003724 if ((SplatBits & ~0xff) == 0) {
3725 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003726 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003727 Imm = SplatBits;
3728 break;
3729 }
3730 if ((SplatBits & ~0xff00) == 0) {
3731 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003732 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003733 Imm = SplatBits >> 8;
3734 break;
3735 }
3736 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003737
3738 case 32:
3739 // NEON's 32-bit VMOV supports splat values where:
3740 // * only one byte is nonzero, or
3741 // * the least significant byte is 0xff and the second byte is nonzero, or
3742 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003743 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003744 if ((SplatBits & ~0xff) == 0) {
3745 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003746 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003747 Imm = SplatBits;
3748 break;
3749 }
3750 if ((SplatBits & ~0xff00) == 0) {
3751 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003752 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003753 Imm = SplatBits >> 8;
3754 break;
3755 }
3756 if ((SplatBits & ~0xff0000) == 0) {
3757 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003758 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003759 Imm = SplatBits >> 16;
3760 break;
3761 }
3762 if ((SplatBits & ~0xff000000) == 0) {
3763 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003764 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003765 Imm = SplatBits >> 24;
3766 break;
3767 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003768
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003769 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3770 if (type == OtherModImm) return SDValue();
3771
Bob Wilson5bafff32009-06-22 23:27:02 +00003772 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003773 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3774 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003775 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003776 Imm = SplatBits >> 8;
3777 SplatBits |= 0xff;
3778 break;
3779 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003780
3781 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003782 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3783 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003784 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003785 Imm = SplatBits >> 16;
3786 SplatBits |= 0xffff;
3787 break;
3788 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003789
3790 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3791 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3792 // VMOV.I32. A (very) minor optimization would be to replicate the value
3793 // and fall through here to test for a valid 64-bit splat. But, then the
3794 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003795 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003796
3797 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003798 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003799 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003800 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003801 uint64_t BitMask = 0xff;
3802 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003803 unsigned ImmMask = 1;
3804 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003805 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003806 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003807 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003808 Imm |= ImmMask;
3809 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003810 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003811 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003812 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003813 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003814 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003815 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003816 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003817 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003818 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003819 break;
3820 }
3821
Bob Wilson1a913ed2010-06-11 21:34:50 +00003822 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003823 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003824 }
3825
Bob Wilsoncba270d2010-07-13 21:16:48 +00003826 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3827 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003828}
3829
Lang Hamesc0a9f822012-03-29 21:56:11 +00003830SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3831 const ARMSubtarget *ST) const {
3832 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3833 return SDValue();
3834
3835 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3836 assert(Op.getValueType() == MVT::f32 &&
3837 "ConstantFP custom lowering should only occur for f32.");
3838
3839 // Try splatting with a VMOV.f32...
3840 APFloat FPVal = CFP->getValueAPF();
3841 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3842 if (ImmVal != -1) {
3843 DebugLoc DL = Op.getDebugLoc();
3844 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3845 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3846 NewVal);
3847 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3848 DAG.getConstant(0, MVT::i32));
3849 }
3850
3851 // If that fails, try a VMOV.i32
3852 EVT VMovVT;
3853 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3854 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3855 VMOVModImm);
3856 if (NewVal != SDValue()) {
3857 DebugLoc DL = Op.getDebugLoc();
3858 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3859 NewVal);
3860 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3861 VecConstant);
3862 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3863 DAG.getConstant(0, MVT::i32));
3864 }
3865
3866 // Finally, try a VMVN.i32
3867 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3868 VMVNModImm);
3869 if (NewVal != SDValue()) {
3870 DebugLoc DL = Op.getDebugLoc();
3871 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3872 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3873 VecConstant);
3874 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3875 DAG.getConstant(0, MVT::i32));
3876 }
3877
3878 return SDValue();
3879}
3880
3881
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003882static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003883 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003884 unsigned NumElts = VT.getVectorNumElements();
3885 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003886
3887 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3888 if (M[0] < 0)
3889 return false;
3890
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003891 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003892
3893 // If this is a VEXT shuffle, the immediate value is the index of the first
3894 // element. The other shuffle indices must be the successive elements after
3895 // the first one.
3896 unsigned ExpectedElt = Imm;
3897 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003898 // Increment the expected index. If it wraps around, it may still be
3899 // a VEXT but the source vectors must be swapped.
3900 ExpectedElt += 1;
3901 if (ExpectedElt == NumElts * 2) {
3902 ExpectedElt = 0;
3903 ReverseVEXT = true;
3904 }
3905
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003906 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003907 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003908 return false;
3909 }
3910
3911 // Adjust the index value if the source operands will be swapped.
3912 if (ReverseVEXT)
3913 Imm -= NumElts;
3914
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003915 return true;
3916}
3917
Bob Wilson8bb9e482009-07-26 00:39:34 +00003918/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3919/// instruction with the specified blocksize. (The order of the elements
3920/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003921static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003922 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3923 "Only possible block sizes for VREV are: 16, 32, 64");
3924
Bob Wilson8bb9e482009-07-26 00:39:34 +00003925 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003926 if (EltSz == 64)
3927 return false;
3928
3929 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003930 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003931 // If the first shuffle index is UNDEF, be optimistic.
3932 if (M[0] < 0)
3933 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003934
3935 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3936 return false;
3937
3938 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003939 if (M[i] < 0) continue; // ignore UNDEF indices
3940 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003941 return false;
3942 }
3943
3944 return true;
3945}
3946
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003947static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003948 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3949 // range, then 0 is placed into the resulting vector. So pretty much any mask
3950 // of 8 elements can work here.
3951 return VT == MVT::v8i8 && M.size() == 8;
3952}
3953
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003954static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003955 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3956 if (EltSz == 64)
3957 return false;
3958
Bob Wilsonc692cb72009-08-21 20:54:19 +00003959 unsigned NumElts = VT.getVectorNumElements();
3960 WhichResult = (M[0] == 0 ? 0 : 1);
3961 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003962 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3963 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003964 return false;
3965 }
3966 return true;
3967}
3968
Bob Wilson324f4f12009-12-03 06:40:55 +00003969/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3970/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3971/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003972static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003973 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3974 if (EltSz == 64)
3975 return false;
3976
3977 unsigned NumElts = VT.getVectorNumElements();
3978 WhichResult = (M[0] == 0 ? 0 : 1);
3979 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003980 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3981 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003982 return false;
3983 }
3984 return true;
3985}
3986
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003987static bool isVUZPMask(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 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003995 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00003996 if ((unsigned) M[i] != 2 * i + WhichResult)
3997 return false;
3998 }
3999
4000 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004001 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004002 return false;
4003
4004 return true;
4005}
4006
Bob Wilson324f4f12009-12-03 06:40:55 +00004007/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4008/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4009/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004010static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004011 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4012 if (EltSz == 64)
4013 return false;
4014
4015 unsigned Half = VT.getVectorNumElements() / 2;
4016 WhichResult = (M[0] == 0 ? 0 : 1);
4017 for (unsigned j = 0; j != 2; ++j) {
4018 unsigned Idx = WhichResult;
4019 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004020 int MIdx = M[i + j * Half];
4021 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004022 return false;
4023 Idx += 2;
4024 }
4025 }
4026
4027 // VUZP.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
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004034static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004035 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4036 if (EltSz == 64)
4037 return false;
4038
Bob Wilsonc692cb72009-08-21 20:54:19 +00004039 unsigned NumElts = VT.getVectorNumElements();
4040 WhichResult = (M[0] == 0 ? 0 : 1);
4041 unsigned Idx = WhichResult * NumElts / 2;
4042 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004043 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4044 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004045 return false;
4046 Idx += 1;
4047 }
4048
4049 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004050 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004051 return false;
4052
4053 return true;
4054}
4055
Bob Wilson324f4f12009-12-03 06:40:55 +00004056/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4057/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4058/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004059static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004060 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4061 if (EltSz == 64)
4062 return false;
4063
4064 unsigned NumElts = VT.getVectorNumElements();
4065 WhichResult = (M[0] == 0 ? 0 : 1);
4066 unsigned Idx = WhichResult * NumElts / 2;
4067 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004068 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4069 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004070 return false;
4071 Idx += 1;
4072 }
4073
4074 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4075 if (VT.is64BitVector() && EltSz == 32)
4076 return false;
4077
4078 return true;
4079}
4080
Dale Johannesenf630c712010-07-29 20:10:08 +00004081// If N is an integer constant that can be moved into a register in one
4082// instruction, return an SDValue of such a constant (will become a MOV
4083// instruction). Otherwise return null.
4084static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4085 const ARMSubtarget *ST, DebugLoc dl) {
4086 uint64_t Val;
4087 if (!isa<ConstantSDNode>(N))
4088 return SDValue();
4089 Val = cast<ConstantSDNode>(N)->getZExtValue();
4090
4091 if (ST->isThumb1Only()) {
4092 if (Val <= 255 || ~Val <= 255)
4093 return DAG.getConstant(Val, MVT::i32);
4094 } else {
4095 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4096 return DAG.getConstant(Val, MVT::i32);
4097 }
4098 return SDValue();
4099}
4100
Bob Wilson5bafff32009-06-22 23:27:02 +00004101// If this is a case we can't handle, return null and let the default
4102// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004103SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4104 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004105 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004106 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004107 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004108
4109 APInt SplatBits, SplatUndef;
4110 unsigned SplatBitSize;
4111 bool HasAnyUndefs;
4112 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004113 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004114 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004115 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004116 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004117 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004118 DAG, VmovVT, VT.is128BitVector(),
4119 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004120 if (Val.getNode()) {
4121 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004122 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004123 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004124
4125 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004126 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004127 Val = isNEONModifiedImm(NegatedImm,
4128 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004129 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004130 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004131 if (Val.getNode()) {
4132 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004133 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004134 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004135
4136 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004137 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004138 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004139 if (ImmVal != -1) {
4140 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4141 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4142 }
4143 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004144 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004145 }
4146
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004147 // Scan through the operands to see if only one value is used.
4148 unsigned NumElts = VT.getVectorNumElements();
4149 bool isOnlyLowElement = true;
4150 bool usesOnlyOneValue = true;
4151 bool isConstant = true;
4152 SDValue Value;
4153 for (unsigned i = 0; i < NumElts; ++i) {
4154 SDValue V = Op.getOperand(i);
4155 if (V.getOpcode() == ISD::UNDEF)
4156 continue;
4157 if (i > 0)
4158 isOnlyLowElement = false;
4159 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4160 isConstant = false;
4161
4162 if (!Value.getNode())
4163 Value = V;
4164 else if (V != Value)
4165 usesOnlyOneValue = false;
4166 }
4167
4168 if (!Value.getNode())
4169 return DAG.getUNDEF(VT);
4170
4171 if (isOnlyLowElement)
4172 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4173
Dale Johannesenf630c712010-07-29 20:10:08 +00004174 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4175
Dale Johannesen575cd142010-10-19 20:00:17 +00004176 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4177 // i32 and try again.
4178 if (usesOnlyOneValue && EltSize <= 32) {
4179 if (!isConstant)
4180 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4181 if (VT.getVectorElementType().isFloatingPoint()) {
4182 SmallVector<SDValue, 8> Ops;
4183 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004184 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004185 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004186 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4187 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004188 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4189 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004190 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004191 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004192 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4193 if (Val.getNode())
4194 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004195 }
4196
4197 // If all elements are constants and the case above didn't get hit, fall back
4198 // to the default expansion, which will generate a load from the constant
4199 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004200 if (isConstant)
4201 return SDValue();
4202
Bob Wilson11a1dff2011-01-07 21:37:30 +00004203 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4204 if (NumElts >= 4) {
4205 SDValue shuffle = ReconstructShuffle(Op, DAG);
4206 if (shuffle != SDValue())
4207 return shuffle;
4208 }
4209
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004210 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004211 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4212 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004213 if (EltSize >= 32) {
4214 // Do the expansion with floating-point types, since that is what the VFP
4215 // registers are defined to use, and since i64 is not legal.
4216 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4217 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004218 SmallVector<SDValue, 8> Ops;
4219 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004220 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004221 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004222 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004223 }
4224
4225 return SDValue();
4226}
4227
Bob Wilson11a1dff2011-01-07 21:37:30 +00004228// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004229// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004230SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4231 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004232 DebugLoc dl = Op.getDebugLoc();
4233 EVT VT = Op.getValueType();
4234 unsigned NumElts = VT.getVectorNumElements();
4235
4236 SmallVector<SDValue, 2> SourceVecs;
4237 SmallVector<unsigned, 2> MinElts;
4238 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004239
Bob Wilson11a1dff2011-01-07 21:37:30 +00004240 for (unsigned i = 0; i < NumElts; ++i) {
4241 SDValue V = Op.getOperand(i);
4242 if (V.getOpcode() == ISD::UNDEF)
4243 continue;
4244 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4245 // A shuffle can only come from building a vector from various
4246 // elements of other vectors.
4247 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004248 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4249 VT.getVectorElementType()) {
4250 // This code doesn't know how to handle shuffles where the vector
4251 // element types do not match (this happens because type legalization
4252 // promotes the return type of EXTRACT_VECTOR_ELT).
4253 // FIXME: It might be appropriate to extend this code to handle
4254 // mismatched types.
4255 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004256 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004257
Bob Wilson11a1dff2011-01-07 21:37:30 +00004258 // Record this extraction against the appropriate vector if possible...
4259 SDValue SourceVec = V.getOperand(0);
4260 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4261 bool FoundSource = false;
4262 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4263 if (SourceVecs[j] == SourceVec) {
4264 if (MinElts[j] > EltNo)
4265 MinElts[j] = EltNo;
4266 if (MaxElts[j] < EltNo)
4267 MaxElts[j] = EltNo;
4268 FoundSource = true;
4269 break;
4270 }
4271 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004272
Bob Wilson11a1dff2011-01-07 21:37:30 +00004273 // Or record a new source if not...
4274 if (!FoundSource) {
4275 SourceVecs.push_back(SourceVec);
4276 MinElts.push_back(EltNo);
4277 MaxElts.push_back(EltNo);
4278 }
4279 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004280
Bob Wilson11a1dff2011-01-07 21:37:30 +00004281 // Currently only do something sane when at most two source vectors
4282 // involved.
4283 if (SourceVecs.size() > 2)
4284 return SDValue();
4285
4286 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4287 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004288
Bob Wilson11a1dff2011-01-07 21:37:30 +00004289 // This loop extracts the usage patterns of the source vectors
4290 // and prepares appropriate SDValues for a shuffle if possible.
4291 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4292 if (SourceVecs[i].getValueType() == VT) {
4293 // No VEXT necessary
4294 ShuffleSrcs[i] = SourceVecs[i];
4295 VEXTOffsets[i] = 0;
4296 continue;
4297 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4298 // It probably isn't worth padding out a smaller vector just to
4299 // break it down again in a shuffle.
4300 return SDValue();
4301 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004302
Bob Wilson11a1dff2011-01-07 21:37:30 +00004303 // Since only 64-bit and 128-bit vectors are legal on ARM and
4304 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004305 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4306 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004307
Bob Wilson11a1dff2011-01-07 21:37:30 +00004308 if (MaxElts[i] - MinElts[i] >= NumElts) {
4309 // Span too large for a VEXT to cope
4310 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004311 }
4312
Bob Wilson11a1dff2011-01-07 21:37:30 +00004313 if (MinElts[i] >= NumElts) {
4314 // The extraction can just take the second half
4315 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004316 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4317 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004318 DAG.getIntPtrConstant(NumElts));
4319 } else if (MaxElts[i] < NumElts) {
4320 // The extraction can just take the first half
4321 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004322 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4323 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004324 DAG.getIntPtrConstant(0));
4325 } else {
4326 // An actual VEXT is needed
4327 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004328 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4329 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004330 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004331 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4332 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004333 DAG.getIntPtrConstant(NumElts));
4334 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4335 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4336 }
4337 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004338
Bob Wilson11a1dff2011-01-07 21:37:30 +00004339 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004340
Bob Wilson11a1dff2011-01-07 21:37:30 +00004341 for (unsigned i = 0; i < NumElts; ++i) {
4342 SDValue Entry = Op.getOperand(i);
4343 if (Entry.getOpcode() == ISD::UNDEF) {
4344 Mask.push_back(-1);
4345 continue;
4346 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004347
Bob Wilson11a1dff2011-01-07 21:37:30 +00004348 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004349 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4350 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004351 if (ExtractVec == SourceVecs[0]) {
4352 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4353 } else {
4354 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4355 }
4356 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004357
Bob Wilson11a1dff2011-01-07 21:37:30 +00004358 // Final check before we try to produce nonsense...
4359 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004360 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4361 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004362
Bob Wilson11a1dff2011-01-07 21:37:30 +00004363 return SDValue();
4364}
4365
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004366/// isShuffleMaskLegal - Targets can use this to indicate that they only
4367/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4368/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4369/// are assumed to be legal.
4370bool
4371ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4372 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004373 if (VT.getVectorNumElements() == 4 &&
4374 (VT.is128BitVector() || VT.is64BitVector())) {
4375 unsigned PFIndexes[4];
4376 for (unsigned i = 0; i != 4; ++i) {
4377 if (M[i] < 0)
4378 PFIndexes[i] = 8;
4379 else
4380 PFIndexes[i] = M[i];
4381 }
4382
4383 // Compute the index in the perfect shuffle table.
4384 unsigned PFTableIndex =
4385 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4386 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4387 unsigned Cost = (PFEntry >> 30);
4388
4389 if (Cost <= 4)
4390 return true;
4391 }
4392
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004393 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004394 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004395
Bob Wilson53dd2452010-06-07 23:53:38 +00004396 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4397 return (EltSize >= 32 ||
4398 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004399 isVREVMask(M, VT, 64) ||
4400 isVREVMask(M, VT, 32) ||
4401 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004402 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004403 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004404 isVTRNMask(M, VT, WhichResult) ||
4405 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004406 isVZIPMask(M, VT, WhichResult) ||
4407 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4408 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4409 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004410}
4411
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004412/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4413/// the specified operations to build the shuffle.
4414static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4415 SDValue RHS, SelectionDAG &DAG,
4416 DebugLoc dl) {
4417 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4418 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4419 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4420
4421 enum {
4422 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4423 OP_VREV,
4424 OP_VDUP0,
4425 OP_VDUP1,
4426 OP_VDUP2,
4427 OP_VDUP3,
4428 OP_VEXT1,
4429 OP_VEXT2,
4430 OP_VEXT3,
4431 OP_VUZPL, // VUZP, left result
4432 OP_VUZPR, // VUZP, right result
4433 OP_VZIPL, // VZIP, left result
4434 OP_VZIPR, // VZIP, right result
4435 OP_VTRNL, // VTRN, left result
4436 OP_VTRNR // VTRN, right result
4437 };
4438
4439 if (OpNum == OP_COPY) {
4440 if (LHSID == (1*9+2)*9+3) return LHS;
4441 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4442 return RHS;
4443 }
4444
4445 SDValue OpLHS, OpRHS;
4446 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4447 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4448 EVT VT = OpLHS.getValueType();
4449
4450 switch (OpNum) {
4451 default: llvm_unreachable("Unknown shuffle opcode!");
4452 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004453 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004454 if (VT.getVectorElementType() == MVT::i32 ||
4455 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004456 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4457 // vrev <4 x i16> -> VREV32
4458 if (VT.getVectorElementType() == MVT::i16)
4459 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4460 // vrev <4 x i8> -> VREV16
4461 assert(VT.getVectorElementType() == MVT::i8);
4462 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004463 case OP_VDUP0:
4464 case OP_VDUP1:
4465 case OP_VDUP2:
4466 case OP_VDUP3:
4467 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004468 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004469 case OP_VEXT1:
4470 case OP_VEXT2:
4471 case OP_VEXT3:
4472 return DAG.getNode(ARMISD::VEXT, dl, VT,
4473 OpLHS, OpRHS,
4474 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4475 case OP_VUZPL:
4476 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004477 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004478 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4479 case OP_VZIPL:
4480 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004481 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004482 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4483 case OP_VTRNL:
4484 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004485 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4486 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004487 }
4488}
4489
Bill Wendling69a05a72011-03-14 23:02:38 +00004490static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004491 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004492 SelectionDAG &DAG) {
4493 // Check to see if we can use the VTBL instruction.
4494 SDValue V1 = Op.getOperand(0);
4495 SDValue V2 = Op.getOperand(1);
4496 DebugLoc DL = Op.getDebugLoc();
4497
4498 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004499 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004500 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4501 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4502
4503 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4504 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4505 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4506 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004507
Owen Anderson76706012011-04-05 21:48:57 +00004508 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004509 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4510 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004511}
4512
Bob Wilson5bafff32009-06-22 23:27:02 +00004513static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004514 SDValue V1 = Op.getOperand(0);
4515 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004516 DebugLoc dl = Op.getDebugLoc();
4517 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004518 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004519
Bob Wilson28865062009-08-13 02:13:04 +00004520 // Convert shuffles that are directly supported on NEON to target-specific
4521 // DAG nodes, instead of keeping them as shuffles and matching them again
4522 // during code selection. This is more efficient and avoids the possibility
4523 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004524 // FIXME: floating-point vectors should be canonicalized to integer vectors
4525 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004526 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004527
Bob Wilson53dd2452010-06-07 23:53:38 +00004528 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4529 if (EltSize <= 32) {
4530 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4531 int Lane = SVN->getSplatIndex();
4532 // If this is undef splat, generate it via "just" vdup, if possible.
4533 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004534
Dan Gohman65fd6562011-11-03 21:49:52 +00004535 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004536 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4537 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4538 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004539 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4540 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4541 // reaches it).
4542 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4543 !isa<ConstantSDNode>(V1.getOperand(0))) {
4544 bool IsScalarToVector = true;
4545 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4546 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4547 IsScalarToVector = false;
4548 break;
4549 }
4550 if (IsScalarToVector)
4551 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4552 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004553 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4554 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004555 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004556
4557 bool ReverseVEXT;
4558 unsigned Imm;
4559 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4560 if (ReverseVEXT)
4561 std::swap(V1, V2);
4562 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4563 DAG.getConstant(Imm, MVT::i32));
4564 }
4565
4566 if (isVREVMask(ShuffleMask, VT, 64))
4567 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4568 if (isVREVMask(ShuffleMask, VT, 32))
4569 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4570 if (isVREVMask(ShuffleMask, VT, 16))
4571 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4572
4573 // Check for Neon shuffles that modify both input vectors in place.
4574 // If both results are used, i.e., if there are two shuffles with the same
4575 // source operands and with masks corresponding to both results of one of
4576 // these operations, DAG memoization will ensure that a single node is
4577 // used for both shuffles.
4578 unsigned WhichResult;
4579 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4580 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4581 V1, V2).getValue(WhichResult);
4582 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4583 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4584 V1, V2).getValue(WhichResult);
4585 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4586 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4587 V1, V2).getValue(WhichResult);
4588
4589 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4590 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4591 V1, V1).getValue(WhichResult);
4592 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4593 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4594 V1, V1).getValue(WhichResult);
4595 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4596 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4597 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004598 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004599
Bob Wilsonc692cb72009-08-21 20:54:19 +00004600 // If the shuffle is not directly supported and it has 4 elements, use
4601 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004602 unsigned NumElts = VT.getVectorNumElements();
4603 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004604 unsigned PFIndexes[4];
4605 for (unsigned i = 0; i != 4; ++i) {
4606 if (ShuffleMask[i] < 0)
4607 PFIndexes[i] = 8;
4608 else
4609 PFIndexes[i] = ShuffleMask[i];
4610 }
4611
4612 // Compute the index in the perfect shuffle table.
4613 unsigned PFTableIndex =
4614 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004615 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4616 unsigned Cost = (PFEntry >> 30);
4617
4618 if (Cost <= 4)
4619 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4620 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004621
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004622 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004623 if (EltSize >= 32) {
4624 // Do the expansion with floating-point types, since that is what the VFP
4625 // registers are defined to use, and since i64 is not legal.
4626 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4627 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004628 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4629 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004630 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004631 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004632 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004633 Ops.push_back(DAG.getUNDEF(EltVT));
4634 else
4635 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4636 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4637 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4638 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004639 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004640 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004641 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004642 }
4643
Bill Wendling69a05a72011-03-14 23:02:38 +00004644 if (VT == MVT::v8i8) {
4645 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4646 if (NewOp.getNode())
4647 return NewOp;
4648 }
4649
Bob Wilson22cac0d2009-08-14 05:16:33 +00004650 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004651}
4652
Eli Friedman5c89cb82011-10-24 23:08:52 +00004653static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4654 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4655 SDValue Lane = Op.getOperand(2);
4656 if (!isa<ConstantSDNode>(Lane))
4657 return SDValue();
4658
4659 return Op;
4660}
4661
Bob Wilson5bafff32009-06-22 23:27:02 +00004662static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004663 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004664 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004665 if (!isa<ConstantSDNode>(Lane))
4666 return SDValue();
4667
4668 SDValue Vec = Op.getOperand(0);
4669 if (Op.getValueType() == MVT::i32 &&
4670 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4671 DebugLoc dl = Op.getDebugLoc();
4672 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4673 }
4674
4675 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004676}
4677
Bob Wilsona6d65862009-08-03 20:36:38 +00004678static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4679 // The only time a CONCAT_VECTORS operation can have legal types is when
4680 // two 64-bit vectors are concatenated to a 128-bit vector.
4681 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4682 "unexpected CONCAT_VECTORS");
4683 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004684 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004685 SDValue Op0 = Op.getOperand(0);
4686 SDValue Op1 = Op.getOperand(1);
4687 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004688 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004689 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004690 DAG.getIntPtrConstant(0));
4691 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004692 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004693 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004694 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004695 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004696}
4697
Bob Wilson626613d2010-11-23 19:38:38 +00004698/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4699/// element has been zero/sign-extended, depending on the isSigned parameter,
4700/// from an integer type half its size.
4701static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4702 bool isSigned) {
4703 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4704 EVT VT = N->getValueType(0);
4705 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4706 SDNode *BVN = N->getOperand(0).getNode();
4707 if (BVN->getValueType(0) != MVT::v4i32 ||
4708 BVN->getOpcode() != ISD::BUILD_VECTOR)
4709 return false;
4710 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4711 unsigned HiElt = 1 - LoElt;
4712 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4713 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4714 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4715 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4716 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4717 return false;
4718 if (isSigned) {
4719 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4720 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4721 return true;
4722 } else {
4723 if (Hi0->isNullValue() && Hi1->isNullValue())
4724 return true;
4725 }
4726 return false;
4727 }
4728
4729 if (N->getOpcode() != ISD::BUILD_VECTOR)
4730 return false;
4731
4732 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4733 SDNode *Elt = N->getOperand(i).getNode();
4734 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4735 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4736 unsigned HalfSize = EltSize / 2;
4737 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004738 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004739 return false;
4740 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004741 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004742 return false;
4743 }
4744 continue;
4745 }
4746 return false;
4747 }
4748
4749 return true;
4750}
4751
4752/// isSignExtended - Check if a node is a vector value that is sign-extended
4753/// or a constant BUILD_VECTOR with sign-extended elements.
4754static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4755 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4756 return true;
4757 if (isExtendedBUILD_VECTOR(N, DAG, true))
4758 return true;
4759 return false;
4760}
4761
4762/// isZeroExtended - Check if a node is a vector value that is zero-extended
4763/// or a constant BUILD_VECTOR with zero-extended elements.
4764static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4765 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4766 return true;
4767 if (isExtendedBUILD_VECTOR(N, DAG, false))
4768 return true;
4769 return false;
4770}
4771
4772/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4773/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004774static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4775 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4776 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004777 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4778 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4779 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004780 LD->isNonTemporal(), LD->isInvariant(),
4781 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004782 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4783 // have been legalized as a BITCAST from v4i32.
4784 if (N->getOpcode() == ISD::BITCAST) {
4785 SDNode *BVN = N->getOperand(0).getNode();
4786 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4787 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4788 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4789 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4790 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4791 }
4792 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4793 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4794 EVT VT = N->getValueType(0);
4795 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4796 unsigned NumElts = VT.getVectorNumElements();
4797 MVT TruncVT = MVT::getIntegerVT(EltSize);
4798 SmallVector<SDValue, 8> Ops;
4799 for (unsigned i = 0; i != NumElts; ++i) {
4800 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4801 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004802 // Element types smaller than 32 bits are not legal, so use i32 elements.
4803 // The values are implicitly truncated so sext vs. zext doesn't matter.
4804 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004805 }
4806 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4807 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004808}
4809
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004810static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4811 unsigned Opcode = N->getOpcode();
4812 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4813 SDNode *N0 = N->getOperand(0).getNode();
4814 SDNode *N1 = N->getOperand(1).getNode();
4815 return N0->hasOneUse() && N1->hasOneUse() &&
4816 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4817 }
4818 return false;
4819}
4820
4821static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4822 unsigned Opcode = N->getOpcode();
4823 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4824 SDNode *N0 = N->getOperand(0).getNode();
4825 SDNode *N1 = N->getOperand(1).getNode();
4826 return N0->hasOneUse() && N1->hasOneUse() &&
4827 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4828 }
4829 return false;
4830}
4831
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004832static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4833 // Multiplications are only custom-lowered for 128-bit vectors so that
4834 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4835 EVT VT = Op.getValueType();
4836 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4837 SDNode *N0 = Op.getOperand(0).getNode();
4838 SDNode *N1 = Op.getOperand(1).getNode();
4839 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004840 bool isMLA = false;
4841 bool isN0SExt = isSignExtended(N0, DAG);
4842 bool isN1SExt = isSignExtended(N1, DAG);
4843 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004844 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004845 else {
4846 bool isN0ZExt = isZeroExtended(N0, DAG);
4847 bool isN1ZExt = isZeroExtended(N1, DAG);
4848 if (isN0ZExt && isN1ZExt)
4849 NewOpc = ARMISD::VMULLu;
4850 else if (isN1SExt || isN1ZExt) {
4851 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4852 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4853 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4854 NewOpc = ARMISD::VMULLs;
4855 isMLA = true;
4856 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4857 NewOpc = ARMISD::VMULLu;
4858 isMLA = true;
4859 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4860 std::swap(N0, N1);
4861 NewOpc = ARMISD::VMULLu;
4862 isMLA = true;
4863 }
4864 }
4865
4866 if (!NewOpc) {
4867 if (VT == MVT::v2i64)
4868 // Fall through to expand this. It is not legal.
4869 return SDValue();
4870 else
4871 // Other vector multiplications are legal.
4872 return Op;
4873 }
4874 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004875
4876 // Legalize to a VMULL instruction.
4877 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004878 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004879 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004880 if (!isMLA) {
4881 Op0 = SkipExtension(N0, DAG);
4882 assert(Op0.getValueType().is64BitVector() &&
4883 Op1.getValueType().is64BitVector() &&
4884 "unexpected types for extended operands to VMULL");
4885 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4886 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004887
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004888 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4889 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4890 // vmull q0, d4, d6
4891 // vmlal q0, d5, d6
4892 // is faster than
4893 // vaddl q0, d4, d5
4894 // vmovl q1, d6
4895 // vmul q0, q0, q1
4896 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4897 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4898 EVT Op1VT = Op1.getValueType();
4899 return DAG.getNode(N0->getOpcode(), DL, VT,
4900 DAG.getNode(NewOpc, DL, VT,
4901 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4902 DAG.getNode(NewOpc, DL, VT,
4903 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004904}
4905
Owen Anderson76706012011-04-05 21:48:57 +00004906static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004907LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4908 // Convert to float
4909 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4910 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4911 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4912 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4913 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4914 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4915 // Get reciprocal estimate.
4916 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004917 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004918 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4919 // Because char has a smaller range than uchar, we can actually get away
4920 // without any newton steps. This requires that we use a weird bias
4921 // of 0xb000, however (again, this has been exhaustively tested).
4922 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4923 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4924 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4925 Y = DAG.getConstant(0xb000, MVT::i32);
4926 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4927 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4928 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4929 // Convert back to short.
4930 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4931 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4932 return X;
4933}
4934
Owen Anderson76706012011-04-05 21:48:57 +00004935static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004936LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4937 SDValue N2;
4938 // Convert to float.
4939 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4940 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4941 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4942 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4943 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4944 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004945
Nate Begeman7973f352011-02-11 20:53:29 +00004946 // Use reciprocal estimate and one refinement step.
4947 // float4 recip = vrecpeq_f32(yf);
4948 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004949 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004950 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004951 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004952 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4953 N1, N2);
4954 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4955 // Because short has a smaller range than ushort, we can actually get away
4956 // with only a single newton step. This requires that we use a weird bias
4957 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004958 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004959 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4960 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004961 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004962 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4963 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4964 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4965 // Convert back to integer and return.
4966 // return vmovn_s32(vcvt_s32_f32(result));
4967 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4968 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4969 return N0;
4970}
4971
4972static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4973 EVT VT = Op.getValueType();
4974 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4975 "unexpected type for custom-lowering ISD::SDIV");
4976
4977 DebugLoc dl = Op.getDebugLoc();
4978 SDValue N0 = Op.getOperand(0);
4979 SDValue N1 = Op.getOperand(1);
4980 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004981
Nate Begeman7973f352011-02-11 20:53:29 +00004982 if (VT == MVT::v8i8) {
4983 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
4984 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004985
Nate Begeman7973f352011-02-11 20:53:29 +00004986 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4987 DAG.getIntPtrConstant(4));
4988 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00004989 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00004990 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
4991 DAG.getIntPtrConstant(0));
4992 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
4993 DAG.getIntPtrConstant(0));
4994
4995 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
4996 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
4997
4998 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
4999 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005000
Nate Begeman7973f352011-02-11 20:53:29 +00005001 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5002 return N0;
5003 }
5004 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5005}
5006
5007static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5008 EVT VT = Op.getValueType();
5009 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5010 "unexpected type for custom-lowering ISD::UDIV");
5011
5012 DebugLoc dl = Op.getDebugLoc();
5013 SDValue N0 = Op.getOperand(0);
5014 SDValue N1 = Op.getOperand(1);
5015 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005016
Nate Begeman7973f352011-02-11 20:53:29 +00005017 if (VT == MVT::v8i8) {
5018 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5019 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005020
Nate Begeman7973f352011-02-11 20:53:29 +00005021 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5022 DAG.getIntPtrConstant(4));
5023 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005024 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005025 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5026 DAG.getIntPtrConstant(0));
5027 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5028 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005029
Nate Begeman7973f352011-02-11 20:53:29 +00005030 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5031 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005032
Nate Begeman7973f352011-02-11 20:53:29 +00005033 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5034 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005035
5036 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005037 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5038 N0);
5039 return N0;
5040 }
Owen Anderson76706012011-04-05 21:48:57 +00005041
Nate Begeman7973f352011-02-11 20:53:29 +00005042 // v4i16 sdiv ... Convert to float.
5043 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5044 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5045 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5046 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5047 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005048 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005049
5050 // Use reciprocal estimate and two refinement steps.
5051 // float4 recip = vrecpeq_f32(yf);
5052 // recip *= vrecpsq_f32(yf, recip);
5053 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005054 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005055 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005056 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005057 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005058 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005059 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005060 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005061 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005062 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005063 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5064 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5065 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5066 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005067 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005068 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5069 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5070 N1 = DAG.getConstant(2, MVT::i32);
5071 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5072 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5073 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5074 // Convert back to integer and return.
5075 // return vmovn_u32(vcvt_s32_f32(result));
5076 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5077 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5078 return N0;
5079}
5080
Evan Cheng342e3162011-08-30 01:34:54 +00005081static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5082 EVT VT = Op.getNode()->getValueType(0);
5083 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5084
5085 unsigned Opc;
5086 bool ExtraOp = false;
5087 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005088 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005089 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5090 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5091 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5092 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5093 }
5094
5095 if (!ExtraOp)
5096 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5097 Op.getOperand(1));
5098 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5099 Op.getOperand(1), Op.getOperand(2));
5100}
5101
Eli Friedman74bf18c2011-09-15 22:26:18 +00005102static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005103 // Monotonic load/store is legal for all targets
5104 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5105 return Op;
5106
5107 // Aquire/Release load/store is not legal for targets without a
5108 // dmb or equivalent available.
5109 return SDValue();
5110}
5111
5112
Eli Friedman2bdffe42011-08-31 00:31:29 +00005113static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005114ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5115 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005116 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005117 assert (Node->getValueType(0) == MVT::i64 &&
5118 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005119
Eli Friedman4d3f3292011-08-31 17:52:22 +00005120 SmallVector<SDValue, 6> Ops;
5121 Ops.push_back(Node->getOperand(0)); // Chain
5122 Ops.push_back(Node->getOperand(1)); // Ptr
5123 // Low part of Val1
5124 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5125 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5126 // High part of Val1
5127 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5128 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005129 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005130 // High part of Val1
5131 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5132 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5133 // High part of Val2
5134 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5135 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5136 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005137 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5138 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005139 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005140 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005141 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005142 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5143 Results.push_back(Result.getValue(2));
5144}
5145
Dan Gohmand858e902010-04-17 15:26:15 +00005146SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005147 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005148 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005149 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005150 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005151 case ISD::GlobalAddress:
5152 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5153 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005154 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005155 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005156 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5157 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005158 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005159 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005160 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005161 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005162 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005163 case ISD::SINT_TO_FP:
5164 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5165 case ISD::FP_TO_SINT:
5166 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005167 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005168 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005169 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005170 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005171 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005172 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005173 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5174 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005175 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005176 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005177 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005178 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005179 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005180 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005181 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005182 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005183 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005184 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005185 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005186 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005187 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005188 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005189 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005190 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005191 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005192 case ISD::SDIV: return LowerSDIV(Op, DAG);
5193 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005194 case ISD::ADDC:
5195 case ISD::ADDE:
5196 case ISD::SUBC:
5197 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005198 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005199 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005200 }
Evan Chenga8e29892007-01-19 07:51:42 +00005201}
5202
Duncan Sands1607f052008-12-01 11:39:25 +00005203/// ReplaceNodeResults - Replace the results of node with an illegal result
5204/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005205void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5206 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005207 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005208 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005209 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005210 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005211 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005212 case ISD::BITCAST:
5213 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005214 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005215 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005216 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005217 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005218 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005219 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005220 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005221 return;
5222 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005223 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005224 return;
5225 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005226 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005227 return;
5228 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005229 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005230 return;
5231 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005232 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005233 return;
5234 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005235 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005236 return;
5237 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005238 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005239 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005240 case ISD::ATOMIC_CMP_SWAP:
5241 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5242 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005243 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005244 if (Res.getNode())
5245 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005246}
Chris Lattner27a6c732007-11-24 07:07:01 +00005247
Evan Chenga8e29892007-01-19 07:51:42 +00005248//===----------------------------------------------------------------------===//
5249// ARM Scheduler Hooks
5250//===----------------------------------------------------------------------===//
5251
5252MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005253ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5254 MachineBasicBlock *BB,
5255 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005256 unsigned dest = MI->getOperand(0).getReg();
5257 unsigned ptr = MI->getOperand(1).getReg();
5258 unsigned oldval = MI->getOperand(2).getReg();
5259 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005260 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5261 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005262 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005263
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005264 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005265 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5266 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5267 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005268
5269 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005270 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5271 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5272 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005273 }
5274
Jim Grosbach5278eb82009-12-11 01:42:04 +00005275 unsigned ldrOpc, strOpc;
5276 switch (Size) {
5277 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005278 case 1:
5279 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005280 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005281 break;
5282 case 2:
5283 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5284 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5285 break;
5286 case 4:
5287 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5288 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5289 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005290 }
5291
5292 MachineFunction *MF = BB->getParent();
5293 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5294 MachineFunction::iterator It = BB;
5295 ++It; // insert the new blocks after the current block
5296
5297 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5298 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5299 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5300 MF->insert(It, loop1MBB);
5301 MF->insert(It, loop2MBB);
5302 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005303
5304 // Transfer the remainder of BB and its successor edges to exitMBB.
5305 exitMBB->splice(exitMBB->begin(), BB,
5306 llvm::next(MachineBasicBlock::iterator(MI)),
5307 BB->end());
5308 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005309
5310 // thisMBB:
5311 // ...
5312 // fallthrough --> loop1MBB
5313 BB->addSuccessor(loop1MBB);
5314
5315 // loop1MBB:
5316 // ldrex dest, [ptr]
5317 // cmp dest, oldval
5318 // bne exitMBB
5319 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005320 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5321 if (ldrOpc == ARM::t2LDREX)
5322 MIB.addImm(0);
5323 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005324 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005325 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005326 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5327 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005328 BB->addSuccessor(loop2MBB);
5329 BB->addSuccessor(exitMBB);
5330
5331 // loop2MBB:
5332 // strex scratch, newval, [ptr]
5333 // cmp scratch, #0
5334 // bne loop1MBB
5335 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005336 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5337 if (strOpc == ARM::t2STREX)
5338 MIB.addImm(0);
5339 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005340 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005341 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005342 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5343 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005344 BB->addSuccessor(loop1MBB);
5345 BB->addSuccessor(exitMBB);
5346
5347 // exitMBB:
5348 // ...
5349 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005350
Dan Gohman14152b42010-07-06 20:24:04 +00005351 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005352
Jim Grosbach5278eb82009-12-11 01:42:04 +00005353 return BB;
5354}
5355
5356MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005357ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5358 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005359 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5360 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5361
5362 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005363 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005364 MachineFunction::iterator It = BB;
5365 ++It;
5366
5367 unsigned dest = MI->getOperand(0).getReg();
5368 unsigned ptr = MI->getOperand(1).getReg();
5369 unsigned incr = MI->getOperand(2).getReg();
5370 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005371 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005372
5373 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5374 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005375 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5376 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005377 }
5378
Jim Grosbachc3c23542009-12-14 04:22:04 +00005379 unsigned ldrOpc, strOpc;
5380 switch (Size) {
5381 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005382 case 1:
5383 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005384 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005385 break;
5386 case 2:
5387 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5388 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5389 break;
5390 case 4:
5391 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5392 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5393 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005394 }
5395
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005396 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5397 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5398 MF->insert(It, loopMBB);
5399 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005400
5401 // Transfer the remainder of BB and its successor edges to exitMBB.
5402 exitMBB->splice(exitMBB->begin(), BB,
5403 llvm::next(MachineBasicBlock::iterator(MI)),
5404 BB->end());
5405 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005406
Craig Topper420761a2012-04-20 07:30:17 +00005407 const TargetRegisterClass *TRC = isThumb2 ?
5408 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5409 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005410 unsigned scratch = MRI.createVirtualRegister(TRC);
5411 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005412
5413 // thisMBB:
5414 // ...
5415 // fallthrough --> loopMBB
5416 BB->addSuccessor(loopMBB);
5417
5418 // loopMBB:
5419 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005420 // <binop> scratch2, dest, incr
5421 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005422 // cmp scratch, #0
5423 // bne- loopMBB
5424 // fallthrough --> exitMBB
5425 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005426 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5427 if (ldrOpc == ARM::t2LDREX)
5428 MIB.addImm(0);
5429 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005430 if (BinOpcode) {
5431 // operand order needs to go the other way for NAND
5432 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5433 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5434 addReg(incr).addReg(dest)).addReg(0);
5435 else
5436 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5437 addReg(dest).addReg(incr)).addReg(0);
5438 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005439
Jim Grosbachb6aed502011-09-09 18:37:27 +00005440 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5441 if (strOpc == ARM::t2STREX)
5442 MIB.addImm(0);
5443 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005444 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005445 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005446 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5447 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005448
5449 BB->addSuccessor(loopMBB);
5450 BB->addSuccessor(exitMBB);
5451
5452 // exitMBB:
5453 // ...
5454 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005455
Dan Gohman14152b42010-07-06 20:24:04 +00005456 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005457
Jim Grosbachc3c23542009-12-14 04:22:04 +00005458 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005459}
5460
Jim Grosbachf7da8822011-04-26 19:44:18 +00005461MachineBasicBlock *
5462ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5463 MachineBasicBlock *BB,
5464 unsigned Size,
5465 bool signExtend,
5466 ARMCC::CondCodes Cond) const {
5467 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5468
5469 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5470 MachineFunction *MF = BB->getParent();
5471 MachineFunction::iterator It = BB;
5472 ++It;
5473
5474 unsigned dest = MI->getOperand(0).getReg();
5475 unsigned ptr = MI->getOperand(1).getReg();
5476 unsigned incr = MI->getOperand(2).getReg();
5477 unsigned oldval = dest;
5478 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005479 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005480
5481 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5482 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005483 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5484 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005485 }
5486
Jim Grosbachf7da8822011-04-26 19:44:18 +00005487 unsigned ldrOpc, strOpc, extendOpc;
5488 switch (Size) {
5489 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5490 case 1:
5491 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5492 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005493 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005494 break;
5495 case 2:
5496 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5497 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005498 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005499 break;
5500 case 4:
5501 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5502 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5503 extendOpc = 0;
5504 break;
5505 }
5506
5507 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5508 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5509 MF->insert(It, loopMBB);
5510 MF->insert(It, exitMBB);
5511
5512 // Transfer the remainder of BB and its successor edges to exitMBB.
5513 exitMBB->splice(exitMBB->begin(), BB,
5514 llvm::next(MachineBasicBlock::iterator(MI)),
5515 BB->end());
5516 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5517
Craig Topper420761a2012-04-20 07:30:17 +00005518 const TargetRegisterClass *TRC = isThumb2 ?
5519 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5520 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005521 unsigned scratch = MRI.createVirtualRegister(TRC);
5522 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005523
5524 // thisMBB:
5525 // ...
5526 // fallthrough --> loopMBB
5527 BB->addSuccessor(loopMBB);
5528
5529 // loopMBB:
5530 // ldrex dest, ptr
5531 // (sign extend dest, if required)
5532 // cmp dest, incr
5533 // cmov.cond scratch2, dest, incr
5534 // strex scratch, scratch2, ptr
5535 // cmp scratch, #0
5536 // bne- loopMBB
5537 // fallthrough --> exitMBB
5538 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005539 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5540 if (ldrOpc == ARM::t2LDREX)
5541 MIB.addImm(0);
5542 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005543
5544 // Sign extend the value, if necessary.
5545 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005546 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005547 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5548 .addReg(dest)
5549 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005550 }
5551
5552 // Build compare and cmov instructions.
5553 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5554 .addReg(oldval).addReg(incr));
5555 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5556 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5557
Jim Grosbachb6aed502011-09-09 18:37:27 +00005558 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5559 if (strOpc == ARM::t2STREX)
5560 MIB.addImm(0);
5561 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005562 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5563 .addReg(scratch).addImm(0));
5564 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5565 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5566
5567 BB->addSuccessor(loopMBB);
5568 BB->addSuccessor(exitMBB);
5569
5570 // exitMBB:
5571 // ...
5572 BB = exitMBB;
5573
5574 MI->eraseFromParent(); // The instruction is gone now.
5575
5576 return BB;
5577}
5578
Eli Friedman2bdffe42011-08-31 00:31:29 +00005579MachineBasicBlock *
5580ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5581 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005582 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005583 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5584 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5585
5586 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5587 MachineFunction *MF = BB->getParent();
5588 MachineFunction::iterator It = BB;
5589 ++It;
5590
5591 unsigned destlo = MI->getOperand(0).getReg();
5592 unsigned desthi = MI->getOperand(1).getReg();
5593 unsigned ptr = MI->getOperand(2).getReg();
5594 unsigned vallo = MI->getOperand(3).getReg();
5595 unsigned valhi = MI->getOperand(4).getReg();
5596 DebugLoc dl = MI->getDebugLoc();
5597 bool isThumb2 = Subtarget->isThumb2();
5598
5599 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5600 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005601 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5602 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5603 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005604 }
5605
5606 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5607 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5608
5609 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005610 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005611 if (IsCmpxchg) {
5612 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5613 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5614 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005615 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5616 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005617 if (IsCmpxchg) {
5618 MF->insert(It, contBB);
5619 MF->insert(It, cont2BB);
5620 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005621 MF->insert(It, exitMBB);
5622
5623 // Transfer the remainder of BB and its successor edges to exitMBB.
5624 exitMBB->splice(exitMBB->begin(), BB,
5625 llvm::next(MachineBasicBlock::iterator(MI)),
5626 BB->end());
5627 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5628
Craig Topper420761a2012-04-20 07:30:17 +00005629 const TargetRegisterClass *TRC = isThumb2 ?
5630 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5631 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005632 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5633
5634 // thisMBB:
5635 // ...
5636 // fallthrough --> loopMBB
5637 BB->addSuccessor(loopMBB);
5638
5639 // loopMBB:
5640 // ldrexd r2, r3, ptr
5641 // <binopa> r0, r2, incr
5642 // <binopb> r1, r3, incr
5643 // strexd storesuccess, r0, r1, ptr
5644 // cmp storesuccess, #0
5645 // bne- loopMBB
5646 // fallthrough --> exitMBB
5647 //
5648 // Note that the registers are explicitly specified because there is not any
5649 // way to force the register allocator to allocate a register pair.
5650 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005651 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005652 // need to properly enforce the restriction that the two output registers
5653 // for ldrexd must be different.
5654 BB = loopMBB;
5655 // Load
5656 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5657 .addReg(ARM::R2, RegState::Define)
5658 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5659 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5660 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5661 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005662
5663 if (IsCmpxchg) {
5664 // Add early exit
5665 for (unsigned i = 0; i < 2; i++) {
5666 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5667 ARM::CMPrr))
5668 .addReg(i == 0 ? destlo : desthi)
5669 .addReg(i == 0 ? vallo : valhi));
5670 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5671 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5672 BB->addSuccessor(exitMBB);
5673 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5674 BB = (i == 0 ? contBB : cont2BB);
5675 }
5676
5677 // Copy to physregs for strexd
5678 unsigned setlo = MI->getOperand(5).getReg();
5679 unsigned sethi = MI->getOperand(6).getReg();
5680 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5681 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5682 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005683 // Perform binary operation
5684 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5685 .addReg(destlo).addReg(vallo))
5686 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5687 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5688 .addReg(desthi).addReg(valhi)).addReg(0);
5689 } else {
5690 // Copy to physregs for strexd
5691 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5692 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5693 }
5694
5695 // Store
5696 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5697 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5698 // Cmp+jump
5699 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5700 .addReg(storesuccess).addImm(0));
5701 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5702 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5703
5704 BB->addSuccessor(loopMBB);
5705 BB->addSuccessor(exitMBB);
5706
5707 // exitMBB:
5708 // ...
5709 BB = exitMBB;
5710
5711 MI->eraseFromParent(); // The instruction is gone now.
5712
5713 return BB;
5714}
5715
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005716/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5717/// registers the function context.
5718void ARMTargetLowering::
5719SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5720 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005721 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5722 DebugLoc dl = MI->getDebugLoc();
5723 MachineFunction *MF = MBB->getParent();
5724 MachineRegisterInfo *MRI = &MF->getRegInfo();
5725 MachineConstantPool *MCP = MF->getConstantPool();
5726 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5727 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005728
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005729 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005730 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005731
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005732 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005733 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005734 ARMConstantPoolValue *CPV =
5735 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5736 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5737
Craig Topper420761a2012-04-20 07:30:17 +00005738 const TargetRegisterClass *TRC = isThumb ?
5739 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5740 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005741
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005742 // Grab constant pool and fixed stack memory operands.
5743 MachineMemOperand *CPMMO =
5744 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5745 MachineMemOperand::MOLoad, 4, 4);
5746
5747 MachineMemOperand *FIMMOSt =
5748 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5749 MachineMemOperand::MOStore, 4, 4);
5750
5751 // Load the address of the dispatch MBB into the jump buffer.
5752 if (isThumb2) {
5753 // Incoming value: jbuf
5754 // ldr.n r5, LCPI1_1
5755 // orr r5, r5, #1
5756 // add r5, pc
5757 // str r5, [$jbuf, #+4] ; &jbuf[1]
5758 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5759 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5760 .addConstantPoolIndex(CPI)
5761 .addMemOperand(CPMMO));
5762 // Set the low bit because of thumb mode.
5763 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5764 AddDefaultCC(
5765 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5766 .addReg(NewVReg1, RegState::Kill)
5767 .addImm(0x01)));
5768 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5769 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5770 .addReg(NewVReg2, RegState::Kill)
5771 .addImm(PCLabelId);
5772 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5773 .addReg(NewVReg3, RegState::Kill)
5774 .addFrameIndex(FI)
5775 .addImm(36) // &jbuf[1] :: pc
5776 .addMemOperand(FIMMOSt));
5777 } else if (isThumb) {
5778 // Incoming value: jbuf
5779 // ldr.n r1, LCPI1_4
5780 // add r1, pc
5781 // mov r2, #1
5782 // orrs r1, r2
5783 // add r2, $jbuf, #+4 ; &jbuf[1]
5784 // str r1, [r2]
5785 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5786 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5787 .addConstantPoolIndex(CPI)
5788 .addMemOperand(CPMMO));
5789 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5790 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5791 .addReg(NewVReg1, RegState::Kill)
5792 .addImm(PCLabelId);
5793 // Set the low bit because of thumb mode.
5794 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5795 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5796 .addReg(ARM::CPSR, RegState::Define)
5797 .addImm(1));
5798 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5799 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5800 .addReg(ARM::CPSR, RegState::Define)
5801 .addReg(NewVReg2, RegState::Kill)
5802 .addReg(NewVReg3, RegState::Kill));
5803 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5804 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5805 .addFrameIndex(FI)
5806 .addImm(36)); // &jbuf[1] :: pc
5807 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5808 .addReg(NewVReg4, RegState::Kill)
5809 .addReg(NewVReg5, RegState::Kill)
5810 .addImm(0)
5811 .addMemOperand(FIMMOSt));
5812 } else {
5813 // Incoming value: jbuf
5814 // ldr r1, LCPI1_1
5815 // add r1, pc, r1
5816 // str r1, [$jbuf, #+4] ; &jbuf[1]
5817 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5818 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5819 .addConstantPoolIndex(CPI)
5820 .addImm(0)
5821 .addMemOperand(CPMMO));
5822 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5823 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5824 .addReg(NewVReg1, RegState::Kill)
5825 .addImm(PCLabelId));
5826 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5827 .addReg(NewVReg2, RegState::Kill)
5828 .addFrameIndex(FI)
5829 .addImm(36) // &jbuf[1] :: pc
5830 .addMemOperand(FIMMOSt));
5831 }
5832}
5833
5834MachineBasicBlock *ARMTargetLowering::
5835EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5836 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5837 DebugLoc dl = MI->getDebugLoc();
5838 MachineFunction *MF = MBB->getParent();
5839 MachineRegisterInfo *MRI = &MF->getRegInfo();
5840 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5841 MachineFrameInfo *MFI = MF->getFrameInfo();
5842 int FI = MFI->getFunctionContextIndex();
5843
Craig Topper420761a2012-04-20 07:30:17 +00005844 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5845 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5846 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005847
Bill Wendling04f15b42011-10-06 21:29:56 +00005848 // Get a mapping of the call site numbers to all of the landing pads they're
5849 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005850 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5851 unsigned MaxCSNum = 0;
5852 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005853 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5854 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005855 if (!BB->isLandingPad()) continue;
5856
5857 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5858 // pad.
5859 for (MachineBasicBlock::iterator
5860 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5861 if (!II->isEHLabel()) continue;
5862
5863 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005864 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005865
Bill Wendling5cbef192011-10-05 23:28:57 +00005866 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5867 for (SmallVectorImpl<unsigned>::iterator
5868 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5869 CSI != CSE; ++CSI) {
5870 CallSiteNumToLPad[*CSI].push_back(BB);
5871 MaxCSNum = std::max(MaxCSNum, *CSI);
5872 }
Bill Wendling2a850152011-10-05 00:02:33 +00005873 break;
5874 }
5875 }
5876
5877 // Get an ordered list of the machine basic blocks for the jump table.
5878 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005879 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005880 LPadList.reserve(CallSiteNumToLPad.size());
5881 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5882 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5883 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005884 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005885 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005886 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5887 }
Bill Wendling2a850152011-10-05 00:02:33 +00005888 }
5889
Bill Wendling5cbef192011-10-05 23:28:57 +00005890 assert(!LPadList.empty() &&
5891 "No landing pad destinations for the dispatch jump table!");
5892
Bill Wendling04f15b42011-10-06 21:29:56 +00005893 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005894 MachineJumpTableInfo *JTI =
5895 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5896 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5897 unsigned UId = AFI->createJumpTableUId();
5898
Bill Wendling04f15b42011-10-06 21:29:56 +00005899 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005900
5901 // Shove the dispatch's address into the return slot in the function context.
5902 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5903 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005904
Bill Wendlingbb734682011-10-05 00:39:32 +00005905 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005906 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005907 DispatchBB->addSuccessor(TrapBB);
5908
5909 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5910 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005911
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005912 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005913 MF->insert(MF->end(), DispatchBB);
5914 MF->insert(MF->end(), DispContBB);
5915 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005916
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005917 // Insert code into the entry block that creates and registers the function
5918 // context.
5919 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5920
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005921 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005922 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005923 MachineMemOperand::MOLoad |
5924 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005925
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005926 if (AFI->isThumb1OnlyFunction())
5927 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5928 else if (!Subtarget->hasVFP2())
5929 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005930 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005931 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005932
Bill Wendling952cb502011-10-18 22:49:07 +00005933 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005934 if (Subtarget->isThumb2()) {
5935 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5936 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5937 .addFrameIndex(FI)
5938 .addImm(4)
5939 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005940
Bill Wendling952cb502011-10-18 22:49:07 +00005941 if (NumLPads < 256) {
5942 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5943 .addReg(NewVReg1)
5944 .addImm(LPadList.size()));
5945 } else {
5946 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5947 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005948 .addImm(NumLPads & 0xFFFF));
5949
5950 unsigned VReg2 = VReg1;
5951 if ((NumLPads & 0xFFFF0000) != 0) {
5952 VReg2 = MRI->createVirtualRegister(TRC);
5953 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5954 .addReg(VReg1)
5955 .addImm(NumLPads >> 16));
5956 }
5957
Bill Wendling952cb502011-10-18 22:49:07 +00005958 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5959 .addReg(NewVReg1)
5960 .addReg(VReg2));
5961 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005962
Bill Wendling95ce2e92011-10-06 22:53:00 +00005963 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5964 .addMBB(TrapBB)
5965 .addImm(ARMCC::HI)
5966 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005967
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005968 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5969 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005970 .addJumpTableIndex(MJTI)
5971 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005972
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005973 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005974 AddDefaultCC(
5975 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005976 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5977 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005978 .addReg(NewVReg1)
5979 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5980
5981 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005982 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005983 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005984 .addJumpTableIndex(MJTI)
5985 .addImm(UId);
5986 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00005987 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5988 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
5989 .addFrameIndex(FI)
5990 .addImm(1)
5991 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00005992
Bill Wendlinga5871dc2011-10-18 23:11:05 +00005993 if (NumLPads < 256) {
5994 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
5995 .addReg(NewVReg1)
5996 .addImm(NumLPads));
5997 } else {
5998 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00005999 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6000 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6001
6002 // MachineConstantPool wants an explicit alignment.
6003 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6004 if (Align == 0)
6005 Align = getTargetData()->getTypeAllocSize(C->getType());
6006 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006007
6008 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6009 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6010 .addReg(VReg1, RegState::Define)
6011 .addConstantPoolIndex(Idx));
6012 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6013 .addReg(NewVReg1)
6014 .addReg(VReg1));
6015 }
6016
Bill Wendling083a8eb2011-10-06 23:37:36 +00006017 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6018 .addMBB(TrapBB)
6019 .addImm(ARMCC::HI)
6020 .addReg(ARM::CPSR);
6021
6022 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6023 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6024 .addReg(ARM::CPSR, RegState::Define)
6025 .addReg(NewVReg1)
6026 .addImm(2));
6027
6028 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006029 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006030 .addJumpTableIndex(MJTI)
6031 .addImm(UId));
6032
6033 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6034 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6035 .addReg(ARM::CPSR, RegState::Define)
6036 .addReg(NewVReg2, RegState::Kill)
6037 .addReg(NewVReg3));
6038
6039 MachineMemOperand *JTMMOLd =
6040 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6041 MachineMemOperand::MOLoad, 4, 4);
6042
6043 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6044 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6045 .addReg(NewVReg4, RegState::Kill)
6046 .addImm(0)
6047 .addMemOperand(JTMMOLd));
6048
6049 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6050 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6051 .addReg(ARM::CPSR, RegState::Define)
6052 .addReg(NewVReg5, RegState::Kill)
6053 .addReg(NewVReg3));
6054
6055 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6056 .addReg(NewVReg6, RegState::Kill)
6057 .addJumpTableIndex(MJTI)
6058 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006059 } else {
6060 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6061 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6062 .addFrameIndex(FI)
6063 .addImm(4)
6064 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006065
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006066 if (NumLPads < 256) {
6067 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6068 .addReg(NewVReg1)
6069 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006070 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006071 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6072 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006073 .addImm(NumLPads & 0xFFFF));
6074
6075 unsigned VReg2 = VReg1;
6076 if ((NumLPads & 0xFFFF0000) != 0) {
6077 VReg2 = MRI->createVirtualRegister(TRC);
6078 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6079 .addReg(VReg1)
6080 .addImm(NumLPads >> 16));
6081 }
6082
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006083 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6084 .addReg(NewVReg1)
6085 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006086 } else {
6087 MachineConstantPool *ConstantPool = MF->getConstantPool();
6088 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6089 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6090
6091 // MachineConstantPool wants an explicit alignment.
6092 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6093 if (Align == 0)
6094 Align = getTargetData()->getTypeAllocSize(C->getType());
6095 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6096
6097 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6098 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6099 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006100 .addConstantPoolIndex(Idx)
6101 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006102 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6103 .addReg(NewVReg1)
6104 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006105 }
6106
Bill Wendling95ce2e92011-10-06 22:53:00 +00006107 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6108 .addMBB(TrapBB)
6109 .addImm(ARMCC::HI)
6110 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006111
Bill Wendling564392b2011-10-18 22:11:18 +00006112 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006113 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006114 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006115 .addReg(NewVReg1)
6116 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006117 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6118 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006119 .addJumpTableIndex(MJTI)
6120 .addImm(UId));
6121
6122 MachineMemOperand *JTMMOLd =
6123 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6124 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006125 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006126 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006127 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6128 .addReg(NewVReg3, RegState::Kill)
6129 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006130 .addImm(0)
6131 .addMemOperand(JTMMOLd));
6132
6133 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006134 .addReg(NewVReg5, RegState::Kill)
6135 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006136 .addJumpTableIndex(MJTI)
6137 .addImm(UId);
6138 }
Bill Wendling2a850152011-10-05 00:02:33 +00006139
Bill Wendlingbb734682011-10-05 00:39:32 +00006140 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006141 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006142 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006143 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6144 MachineBasicBlock *CurMBB = *I;
6145 if (PrevMBB != CurMBB)
6146 DispContBB->addSuccessor(CurMBB);
6147 PrevMBB = CurMBB;
6148 }
6149
Bill Wendling24bb9252011-10-17 05:25:09 +00006150 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006151 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6152 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006153 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006154 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006155 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6156 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6157 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006158
6159 // Remove the landing pad successor from the invoke block and replace it
6160 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006161 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6162 BB->succ_end());
6163 while (!Successors.empty()) {
6164 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006165 if (SMBB->isLandingPad()) {
6166 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006167 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006168 }
6169 }
6170
6171 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006172
6173 // Find the invoke call and mark all of the callee-saved registers as
6174 // 'implicit defined' so that they're spilled. This prevents code from
6175 // moving instructions to before the EH block, where they will never be
6176 // executed.
6177 for (MachineBasicBlock::reverse_iterator
6178 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006179 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006180
6181 DenseMap<unsigned, bool> DefRegs;
6182 for (MachineInstr::mop_iterator
6183 OI = II->operands_begin(), OE = II->operands_end();
6184 OI != OE; ++OI) {
6185 if (!OI->isReg()) continue;
6186 DefRegs[OI->getReg()] = true;
6187 }
6188
6189 MachineInstrBuilder MIB(&*II);
6190
Bill Wendling5d798592011-10-14 23:55:44 +00006191 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006192 unsigned Reg = SavedRegs[i];
6193 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006194 !ARM::tGPRRegClass.contains(Reg) &&
6195 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006196 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006197 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006198 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006199 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006200 continue;
6201 if (!DefRegs[Reg])
6202 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006203 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006204
6205 break;
6206 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006207 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006208
Bill Wendlingf7b02072011-10-18 18:30:49 +00006209 // Mark all former landing pads as non-landing pads. The dispatch is the only
6210 // landing pad now.
6211 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6212 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6213 (*I)->setIsLandingPad(false);
6214
Bill Wendlingbb734682011-10-05 00:39:32 +00006215 // The instruction is gone now.
6216 MI->eraseFromParent();
6217
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006218 return MBB;
6219}
6220
Evan Cheng218977b2010-07-13 19:27:42 +00006221static
6222MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6223 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6224 E = MBB->succ_end(); I != E; ++I)
6225 if (*I != Succ)
6226 return *I;
6227 llvm_unreachable("Expecting a BB with two successors!");
6228}
6229
Jim Grosbache801dc42009-12-12 01:40:06 +00006230MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006231ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006232 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006233 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006234 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006235 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006236 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006237 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006238 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006239 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006240 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006241 // The Thumb2 pre-indexed stores have the same MI operands, they just
6242 // define them differently in the .td files from the isel patterns, so
6243 // they need pseudos.
6244 case ARM::t2STR_preidx:
6245 MI->setDesc(TII->get(ARM::t2STR_PRE));
6246 return BB;
6247 case ARM::t2STRB_preidx:
6248 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6249 return BB;
6250 case ARM::t2STRH_preidx:
6251 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6252 return BB;
6253
Jim Grosbach19dec202011-08-05 20:35:44 +00006254 case ARM::STRi_preidx:
6255 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006256 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006257 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6258 // Decode the offset.
6259 unsigned Offset = MI->getOperand(4).getImm();
6260 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6261 Offset = ARM_AM::getAM2Offset(Offset);
6262 if (isSub)
6263 Offset = -Offset;
6264
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006265 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006266 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006267 .addOperand(MI->getOperand(0)) // Rn_wb
6268 .addOperand(MI->getOperand(1)) // Rt
6269 .addOperand(MI->getOperand(2)) // Rn
6270 .addImm(Offset) // offset (skip GPR==zero_reg)
6271 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006272 .addOperand(MI->getOperand(6))
6273 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006274 MI->eraseFromParent();
6275 return BB;
6276 }
6277 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006278 case ARM::STRBr_preidx:
6279 case ARM::STRH_preidx: {
6280 unsigned NewOpc;
6281 switch (MI->getOpcode()) {
6282 default: llvm_unreachable("unexpected opcode!");
6283 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6284 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6285 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6286 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006287 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6288 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6289 MIB.addOperand(MI->getOperand(i));
6290 MI->eraseFromParent();
6291 return BB;
6292 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006293 case ARM::ATOMIC_LOAD_ADD_I8:
6294 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6295 case ARM::ATOMIC_LOAD_ADD_I16:
6296 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6297 case ARM::ATOMIC_LOAD_ADD_I32:
6298 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006299
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006300 case ARM::ATOMIC_LOAD_AND_I8:
6301 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6302 case ARM::ATOMIC_LOAD_AND_I16:
6303 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6304 case ARM::ATOMIC_LOAD_AND_I32:
6305 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006306
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006307 case ARM::ATOMIC_LOAD_OR_I8:
6308 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6309 case ARM::ATOMIC_LOAD_OR_I16:
6310 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6311 case ARM::ATOMIC_LOAD_OR_I32:
6312 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006313
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006314 case ARM::ATOMIC_LOAD_XOR_I8:
6315 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6316 case ARM::ATOMIC_LOAD_XOR_I16:
6317 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6318 case ARM::ATOMIC_LOAD_XOR_I32:
6319 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006320
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006321 case ARM::ATOMIC_LOAD_NAND_I8:
6322 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6323 case ARM::ATOMIC_LOAD_NAND_I16:
6324 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6325 case ARM::ATOMIC_LOAD_NAND_I32:
6326 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006327
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006328 case ARM::ATOMIC_LOAD_SUB_I8:
6329 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6330 case ARM::ATOMIC_LOAD_SUB_I16:
6331 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6332 case ARM::ATOMIC_LOAD_SUB_I32:
6333 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006334
Jim Grosbachf7da8822011-04-26 19:44:18 +00006335 case ARM::ATOMIC_LOAD_MIN_I8:
6336 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6337 case ARM::ATOMIC_LOAD_MIN_I16:
6338 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6339 case ARM::ATOMIC_LOAD_MIN_I32:
6340 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6341
6342 case ARM::ATOMIC_LOAD_MAX_I8:
6343 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6344 case ARM::ATOMIC_LOAD_MAX_I16:
6345 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6346 case ARM::ATOMIC_LOAD_MAX_I32:
6347 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6348
6349 case ARM::ATOMIC_LOAD_UMIN_I8:
6350 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6351 case ARM::ATOMIC_LOAD_UMIN_I16:
6352 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6353 case ARM::ATOMIC_LOAD_UMIN_I32:
6354 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6355
6356 case ARM::ATOMIC_LOAD_UMAX_I8:
6357 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6358 case ARM::ATOMIC_LOAD_UMAX_I16:
6359 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6360 case ARM::ATOMIC_LOAD_UMAX_I32:
6361 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6362
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006363 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6364 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6365 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006366
6367 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6368 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6369 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006370
Eli Friedman2bdffe42011-08-31 00:31:29 +00006371
6372 case ARM::ATOMADD6432:
6373 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006374 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6375 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006376 case ARM::ATOMSUB6432:
6377 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006378 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6379 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006380 case ARM::ATOMOR6432:
6381 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006382 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006383 case ARM::ATOMXOR6432:
6384 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006385 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006386 case ARM::ATOMAND6432:
6387 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006388 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006389 case ARM::ATOMSWAP6432:
6390 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006391 case ARM::ATOMCMPXCHG6432:
6392 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6393 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6394 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006395
Evan Cheng007ea272009-08-12 05:17:19 +00006396 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006397 // To "insert" a SELECT_CC instruction, we actually have to insert the
6398 // diamond control-flow pattern. The incoming instruction knows the
6399 // destination vreg to set, the condition code register to branch on, the
6400 // true/false values to select between, and a branch opcode to use.
6401 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006402 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006403 ++It;
6404
6405 // thisMBB:
6406 // ...
6407 // TrueVal = ...
6408 // cmpTY ccX, r1, r2
6409 // bCC copy1MBB
6410 // fallthrough --> copy0MBB
6411 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006412 MachineFunction *F = BB->getParent();
6413 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6414 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006415 F->insert(It, copy0MBB);
6416 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006417
6418 // Transfer the remainder of BB and its successor edges to sinkMBB.
6419 sinkMBB->splice(sinkMBB->begin(), BB,
6420 llvm::next(MachineBasicBlock::iterator(MI)),
6421 BB->end());
6422 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6423
Dan Gohman258c58c2010-07-06 15:49:48 +00006424 BB->addSuccessor(copy0MBB);
6425 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006426
Dan Gohman14152b42010-07-06 20:24:04 +00006427 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6428 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6429
Evan Chenga8e29892007-01-19 07:51:42 +00006430 // copy0MBB:
6431 // %FalseValue = ...
6432 // # fallthrough to sinkMBB
6433 BB = copy0MBB;
6434
6435 // Update machine-CFG edges
6436 BB->addSuccessor(sinkMBB);
6437
6438 // sinkMBB:
6439 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6440 // ...
6441 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006442 BuildMI(*BB, BB->begin(), dl,
6443 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006444 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6445 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6446
Dan Gohman14152b42010-07-06 20:24:04 +00006447 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006448 return BB;
6449 }
Evan Cheng86198642009-08-07 00:34:42 +00006450
Evan Cheng218977b2010-07-13 19:27:42 +00006451 case ARM::BCCi64:
6452 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006453 // If there is an unconditional branch to the other successor, remove it.
6454 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006455
Evan Cheng218977b2010-07-13 19:27:42 +00006456 // Compare both parts that make up the double comparison separately for
6457 // equality.
6458 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6459
6460 unsigned LHS1 = MI->getOperand(1).getReg();
6461 unsigned LHS2 = MI->getOperand(2).getReg();
6462 if (RHSisZero) {
6463 AddDefaultPred(BuildMI(BB, dl,
6464 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6465 .addReg(LHS1).addImm(0));
6466 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6467 .addReg(LHS2).addImm(0)
6468 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6469 } else {
6470 unsigned RHS1 = MI->getOperand(3).getReg();
6471 unsigned RHS2 = MI->getOperand(4).getReg();
6472 AddDefaultPred(BuildMI(BB, dl,
6473 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6474 .addReg(LHS1).addReg(RHS1));
6475 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6476 .addReg(LHS2).addReg(RHS2)
6477 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6478 }
6479
6480 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6481 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6482 if (MI->getOperand(0).getImm() == ARMCC::NE)
6483 std::swap(destMBB, exitMBB);
6484
6485 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6486 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006487 if (isThumb2)
6488 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6489 else
6490 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006491
6492 MI->eraseFromParent(); // The pseudo instruction is gone now.
6493 return BB;
6494 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006495
Bill Wendling5bc85282011-10-17 20:37:20 +00006496 case ARM::Int_eh_sjlj_setjmp:
6497 case ARM::Int_eh_sjlj_setjmp_nofp:
6498 case ARM::tInt_eh_sjlj_setjmp:
6499 case ARM::t2Int_eh_sjlj_setjmp:
6500 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6501 EmitSjLjDispatchBlock(MI, BB);
6502 return BB;
6503
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006504 case ARM::ABS:
6505 case ARM::t2ABS: {
6506 // To insert an ABS instruction, we have to insert the
6507 // diamond control-flow pattern. The incoming instruction knows the
6508 // source vreg to test against 0, the destination vreg to set,
6509 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006510 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006511 // It transforms
6512 // V1 = ABS V0
6513 // into
6514 // V2 = MOVS V0
6515 // BCC (branch to SinkBB if V0 >= 0)
6516 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006517 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006518 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6519 MachineFunction::iterator BBI = BB;
6520 ++BBI;
6521 MachineFunction *Fn = BB->getParent();
6522 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6523 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6524 Fn->insert(BBI, RSBBB);
6525 Fn->insert(BBI, SinkBB);
6526
6527 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6528 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6529 bool isThumb2 = Subtarget->isThumb2();
6530 MachineRegisterInfo &MRI = Fn->getRegInfo();
6531 // In Thumb mode S must not be specified if source register is the SP or
6532 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006533 unsigned NewMovDstReg = MRI.createVirtualRegister(isThumb2 ?
6534 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6535 (const TargetRegisterClass*)&ARM::GPRRegClass);
6536 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6537 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6538 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006539
6540 // Transfer the remainder of BB and its successor edges to sinkMBB.
6541 SinkBB->splice(SinkBB->begin(), BB,
6542 llvm::next(MachineBasicBlock::iterator(MI)),
6543 BB->end());
6544 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6545
6546 BB->addSuccessor(RSBBB);
6547 BB->addSuccessor(SinkBB);
6548
6549 // fall through to SinkMBB
6550 RSBBB->addSuccessor(SinkBB);
6551
6552 // insert a movs at the end of BB
6553 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVr : ARM::MOVr),
6554 NewMovDstReg)
6555 .addReg(ABSSrcReg, RegState::Kill)
6556 .addImm((unsigned)ARMCC::AL).addReg(0)
6557 .addReg(ARM::CPSR, RegState::Define);
6558
6559 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006560 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006561 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6562 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6563
6564 // insert rsbri in RSBBB
6565 // Note: BCC and rsbri will be converted into predicated rsbmi
6566 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006567 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006568 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
6569 .addReg(NewMovDstReg, RegState::Kill)
6570 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6571
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006572 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006573 // reuse ABSDstReg to not change uses of ABS instruction
6574 BuildMI(*SinkBB, SinkBB->begin(), dl,
6575 TII->get(ARM::PHI), ABSDstReg)
6576 .addReg(NewRsbDstReg).addMBB(RSBBB)
6577 .addReg(NewMovDstReg).addMBB(BB);
6578
6579 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006580 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006581
6582 // return last added BB
6583 return SinkBB;
6584 }
Evan Chenga8e29892007-01-19 07:51:42 +00006585 }
6586}
6587
Evan Cheng37fefc22011-08-30 19:09:48 +00006588void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6589 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006590 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006591 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6592 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6593 return;
6594 }
6595
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006596 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006597 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6598 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6599 // operand is still set to noreg. If needed, set the optional operand's
6600 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006601 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006602 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006603
Andrew Trick3be654f2011-09-21 02:20:46 +00006604 // Rename pseudo opcodes.
6605 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6606 if (NewOpc) {
6607 const ARMBaseInstrInfo *TII =
6608 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006609 MCID = &TII->get(NewOpc);
6610
6611 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6612 "converted opcode should be the same except for cc_out");
6613
6614 MI->setDesc(*MCID);
6615
6616 // Add the optional cc_out operand
6617 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006618 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006619 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006620
6621 // Any ARM instruction that sets the 's' bit should specify an optional
6622 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006623 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006624 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006625 return;
6626 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006627 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6628 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006629 bool definesCPSR = false;
6630 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006631 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006632 i != e; ++i) {
6633 const MachineOperand &MO = MI->getOperand(i);
6634 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6635 definesCPSR = true;
6636 if (MO.isDead())
6637 deadCPSR = true;
6638 MI->RemoveOperand(i);
6639 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006640 }
6641 }
Andrew Trick4815d562011-09-20 03:17:40 +00006642 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006643 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006644 return;
6645 }
6646 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006647 if (deadCPSR) {
6648 assert(!MI->getOperand(ccOutIdx).getReg() &&
6649 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006650 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006651 }
Andrew Trick4815d562011-09-20 03:17:40 +00006652
Andrew Trick3be654f2011-09-21 02:20:46 +00006653 // If this instruction was defined with an optional CPSR def and its dag node
6654 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006655 MachineOperand &MO = MI->getOperand(ccOutIdx);
6656 MO.setReg(ARM::CPSR);
6657 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006658}
6659
Evan Chenga8e29892007-01-19 07:51:42 +00006660//===----------------------------------------------------------------------===//
6661// ARM Optimization Hooks
6662//===----------------------------------------------------------------------===//
6663
Chris Lattnerd1980a52009-03-12 06:52:53 +00006664static
6665SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6666 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00006667 SelectionDAG &DAG = DCI.DAG;
6668 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00006669 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00006670 unsigned Opc = N->getOpcode();
6671 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
6672 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
6673 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
6674 ISD::CondCode CC = ISD::SETCC_INVALID;
6675
6676 if (isSlctCC) {
6677 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
6678 } else {
6679 SDValue CCOp = Slct.getOperand(0);
6680 if (CCOp.getOpcode() == ISD::SETCC)
6681 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
6682 }
6683
6684 bool DoXform = false;
6685 bool InvCC = false;
6686 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
6687 "Bad input!");
6688
6689 if (LHS.getOpcode() == ISD::Constant &&
6690 cast<ConstantSDNode>(LHS)->isNullValue()) {
6691 DoXform = true;
6692 } else if (CC != ISD::SETCC_INVALID &&
6693 RHS.getOpcode() == ISD::Constant &&
6694 cast<ConstantSDNode>(RHS)->isNullValue()) {
6695 std::swap(LHS, RHS);
6696 SDValue Op0 = Slct.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006697 EVT OpVT = isSlctCC ? Op0.getValueType() :
Chris Lattnerd1980a52009-03-12 06:52:53 +00006698 Op0.getOperand(0).getValueType();
6699 bool isInt = OpVT.isInteger();
6700 CC = ISD::getSetCCInverse(CC, isInt);
6701
6702 if (!TLI.isCondCodeLegal(CC, OpVT))
6703 return SDValue(); // Inverse operator isn't legal.
6704
6705 DoXform = true;
6706 InvCC = true;
6707 }
6708
6709 if (DoXform) {
6710 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
6711 if (isSlctCC)
6712 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
6713 Slct.getOperand(0), Slct.getOperand(1), CC);
6714 SDValue CCOp = Slct.getOperand(0);
6715 if (InvCC)
6716 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
6717 CCOp.getOperand(0), CCOp.getOperand(1), CC);
6718 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
6719 CCOp, OtherOp, Result);
6720 }
6721 return SDValue();
6722}
6723
Eric Christopherfa6f5912011-06-29 21:10:36 +00006724// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00006725// (only after legalization).
6726static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
6727 TargetLowering::DAGCombinerInfo &DCI,
6728 const ARMSubtarget *Subtarget) {
6729
6730 // Only perform optimization if after legalize, and if NEON is available. We
6731 // also expected both operands to be BUILD_VECTORs.
6732 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
6733 || N0.getOpcode() != ISD::BUILD_VECTOR
6734 || N1.getOpcode() != ISD::BUILD_VECTOR)
6735 return SDValue();
6736
6737 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
6738 EVT VT = N->getValueType(0);
6739 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
6740 return SDValue();
6741
6742 // Check that the vector operands are of the right form.
6743 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
6744 // operands, where N is the size of the formed vector.
6745 // Each EXTRACT_VECTOR should have the same input vector and odd or even
6746 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00006747
6748 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00006749 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00006750 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00006751 SDValue Vec = N0->getOperand(0)->getOperand(0);
6752 SDNode *V = Vec.getNode();
6753 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00006754
Eric Christopherfa6f5912011-06-29 21:10:36 +00006755 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00006756 // check to see if each of their operands are an EXTRACT_VECTOR with
6757 // the same vector and appropriate index.
6758 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
6759 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
6760 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00006761
Tanya Lattner189531f2011-06-14 23:48:48 +00006762 SDValue ExtVec0 = N0->getOperand(i);
6763 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006764
Tanya Lattner189531f2011-06-14 23:48:48 +00006765 // First operand is the vector, verify its the same.
6766 if (V != ExtVec0->getOperand(0).getNode() ||
6767 V != ExtVec1->getOperand(0).getNode())
6768 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00006769
Tanya Lattner189531f2011-06-14 23:48:48 +00006770 // Second is the constant, verify its correct.
6771 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
6772 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00006773
Tanya Lattner189531f2011-06-14 23:48:48 +00006774 // For the constant, we want to see all the even or all the odd.
6775 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
6776 || C1->getZExtValue() != nextIndex+1)
6777 return SDValue();
6778
6779 // Increment index.
6780 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006781 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00006782 return SDValue();
6783 }
6784
6785 // Create VPADDL node.
6786 SelectionDAG &DAG = DCI.DAG;
6787 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00006788
6789 // Build operand list.
6790 SmallVector<SDValue, 8> Ops;
6791 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
6792 TLI.getPointerTy()));
6793
6794 // Input is the vector.
6795 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00006796
Tanya Lattner189531f2011-06-14 23:48:48 +00006797 // Get widened type and narrowed type.
6798 MVT widenType;
6799 unsigned numElem = VT.getVectorNumElements();
6800 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
6801 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
6802 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
6803 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
6804 default:
Craig Topperbc219812012-02-07 02:50:20 +00006805 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00006806 }
6807
6808 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
6809 widenType, &Ops[0], Ops.size());
6810 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
6811}
6812
Bob Wilson3d5792a2010-07-29 20:34:14 +00006813/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
6814/// operands N0 and N1. This is a helper for PerformADDCombine that is
6815/// called with the default operands, and if that fails, with commuted
6816/// operands.
6817static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00006818 TargetLowering::DAGCombinerInfo &DCI,
6819 const ARMSubtarget *Subtarget){
6820
6821 // Attempt to create vpaddl for this add.
6822 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
6823 if (Result.getNode())
6824 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00006825
Chris Lattnerd1980a52009-03-12 06:52:53 +00006826 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6827 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
6828 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
6829 if (Result.getNode()) return Result;
6830 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00006831 return SDValue();
6832}
6833
Bob Wilson3d5792a2010-07-29 20:34:14 +00006834/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
6835///
6836static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00006837 TargetLowering::DAGCombinerInfo &DCI,
6838 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006839 SDValue N0 = N->getOperand(0);
6840 SDValue N1 = N->getOperand(1);
6841
6842 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00006843 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006844 if (Result.getNode())
6845 return Result;
6846
6847 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00006848 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00006849}
6850
Chris Lattnerd1980a52009-03-12 06:52:53 +00006851/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00006852///
Chris Lattnerd1980a52009-03-12 06:52:53 +00006853static SDValue PerformSUBCombine(SDNode *N,
6854 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00006855 SDValue N0 = N->getOperand(0);
6856 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00006857
Chris Lattnerd1980a52009-03-12 06:52:53 +00006858 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6859 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
6860 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
6861 if (Result.getNode()) return Result;
6862 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00006863
Chris Lattnerd1980a52009-03-12 06:52:53 +00006864 return SDValue();
6865}
6866
Evan Cheng463d3582011-03-31 19:38:48 +00006867/// PerformVMULCombine
6868/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
6869/// special multiplier accumulator forwarding.
6870/// vmul d3, d0, d2
6871/// vmla d3, d1, d2
6872/// is faster than
6873/// vadd d3, d0, d1
6874/// vmul d3, d3, d2
6875static SDValue PerformVMULCombine(SDNode *N,
6876 TargetLowering::DAGCombinerInfo &DCI,
6877 const ARMSubtarget *Subtarget) {
6878 if (!Subtarget->hasVMLxForwarding())
6879 return SDValue();
6880
6881 SelectionDAG &DAG = DCI.DAG;
6882 SDValue N0 = N->getOperand(0);
6883 SDValue N1 = N->getOperand(1);
6884 unsigned Opcode = N0.getOpcode();
6885 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6886 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00006887 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00006888 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
6889 Opcode != ISD::FADD && Opcode != ISD::FSUB)
6890 return SDValue();
6891 std::swap(N0, N1);
6892 }
6893
6894 EVT VT = N->getValueType(0);
6895 DebugLoc DL = N->getDebugLoc();
6896 SDValue N00 = N0->getOperand(0);
6897 SDValue N01 = N0->getOperand(1);
6898 return DAG.getNode(Opcode, DL, VT,
6899 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
6900 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
6901}
6902
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006903static SDValue PerformMULCombine(SDNode *N,
6904 TargetLowering::DAGCombinerInfo &DCI,
6905 const ARMSubtarget *Subtarget) {
6906 SelectionDAG &DAG = DCI.DAG;
6907
6908 if (Subtarget->isThumb1Only())
6909 return SDValue();
6910
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006911 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
6912 return SDValue();
6913
6914 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00006915 if (VT.is64BitVector() || VT.is128BitVector())
6916 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006917 if (VT != MVT::i32)
6918 return SDValue();
6919
6920 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
6921 if (!C)
6922 return SDValue();
6923
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006924 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006925 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006926
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006927 ShiftAmt = ShiftAmt & (32 - 1);
6928 SDValue V = N->getOperand(0);
6929 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006930
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006931 SDValue Res;
6932 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006933
6934 if (MulAmt >= 0) {
6935 if (isPowerOf2_32(MulAmt - 1)) {
6936 // (mul x, 2^N + 1) => (add (shl x, N), x)
6937 Res = DAG.getNode(ISD::ADD, DL, VT,
6938 V,
6939 DAG.getNode(ISD::SHL, DL, VT,
6940 V,
6941 DAG.getConstant(Log2_32(MulAmt - 1),
6942 MVT::i32)));
6943 } else if (isPowerOf2_32(MulAmt + 1)) {
6944 // (mul x, 2^N - 1) => (sub (shl x, N), x)
6945 Res = DAG.getNode(ISD::SUB, DL, VT,
6946 DAG.getNode(ISD::SHL, DL, VT,
6947 V,
6948 DAG.getConstant(Log2_32(MulAmt + 1),
6949 MVT::i32)),
6950 V);
6951 } else
6952 return SDValue();
6953 } else {
6954 uint64_t MulAmtAbs = -MulAmt;
6955 if (isPowerOf2_32(MulAmtAbs + 1)) {
6956 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
6957 Res = DAG.getNode(ISD::SUB, DL, VT,
6958 V,
6959 DAG.getNode(ISD::SHL, DL, VT,
6960 V,
6961 DAG.getConstant(Log2_32(MulAmtAbs + 1),
6962 MVT::i32)));
6963 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
6964 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
6965 Res = DAG.getNode(ISD::ADD, DL, VT,
6966 V,
6967 DAG.getNode(ISD::SHL, DL, VT,
6968 V,
6969 DAG.getConstant(Log2_32(MulAmtAbs-1),
6970 MVT::i32)));
6971 Res = DAG.getNode(ISD::SUB, DL, VT,
6972 DAG.getConstant(0, MVT::i32),Res);
6973
6974 } else
6975 return SDValue();
6976 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006977
6978 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00006979 Res = DAG.getNode(ISD::SHL, DL, VT,
6980 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006981
6982 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00006983 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00006984 return SDValue();
6985}
6986
Evan Chengc892aeb2012-02-23 01:19:06 +00006987static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
6988 if (N.getOpcode() != ARMISD::CMOV || !N.getNode()->hasOneUse())
6989 return false;
6990
6991 SDValue FalseVal = N.getOperand(0);
6992 ConstantSDNode *C = dyn_cast<ConstantSDNode>(FalseVal);
6993 if (!C)
6994 return false;
6995 if (AllOnes)
6996 return C->isAllOnesValue();
6997 return C->isNullValue();
6998}
6999
7000/// formConditionalOp - Combine an operation with a conditional move operand
7001/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
7002/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
7003static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
7004 bool Commutable) {
7005 SDValue N0 = N->getOperand(0);
7006 SDValue N1 = N->getOperand(1);
7007
7008 bool isAND = N->getOpcode() == ISD::AND;
7009 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
7010 if (!isCand && Commutable) {
7011 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
7012 if (isCand)
7013 std::swap(N0, N1);
7014 }
7015 if (!isCand)
7016 return SDValue();
7017
7018 unsigned Opc = 0;
7019 switch (N->getOpcode()) {
7020 default: llvm_unreachable("Unexpected node");
7021 case ISD::AND: Opc = ARMISD::CAND; break;
7022 case ISD::OR: Opc = ARMISD::COR; break;
7023 case ISD::XOR: Opc = ARMISD::CXOR; break;
7024 }
7025 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
7026 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
7027 N1.getOperand(4));
7028}
7029
Owen Anderson080c0922010-11-05 19:27:46 +00007030static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007031 TargetLowering::DAGCombinerInfo &DCI,
7032 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007033
Owen Anderson080c0922010-11-05 19:27:46 +00007034 // Attempt to use immediate-form VBIC
7035 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7036 DebugLoc dl = N->getDebugLoc();
7037 EVT VT = N->getValueType(0);
7038 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007039
Tanya Lattner0433b212011-04-07 15:24:20 +00007040 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7041 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007042
Owen Anderson080c0922010-11-05 19:27:46 +00007043 APInt SplatBits, SplatUndef;
7044 unsigned SplatBitSize;
7045 bool HasAnyUndefs;
7046 if (BVN &&
7047 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7048 if (SplatBitSize <= 64) {
7049 EVT VbicVT;
7050 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7051 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007052 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007053 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007054 if (Val.getNode()) {
7055 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007056 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007057 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007058 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007059 }
7060 }
7061 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007062
Evan Chengc892aeb2012-02-23 01:19:06 +00007063 if (!Subtarget->isThumb1Only()) {
7064 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7065 SDValue CAND = formConditionalOp(N, DAG, true);
7066 if (CAND.getNode())
7067 return CAND;
7068 }
7069
Owen Anderson080c0922010-11-05 19:27:46 +00007070 return SDValue();
7071}
7072
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007073/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7074static SDValue PerformORCombine(SDNode *N,
7075 TargetLowering::DAGCombinerInfo &DCI,
7076 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007077 // Attempt to use immediate-form VORR
7078 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7079 DebugLoc dl = N->getDebugLoc();
7080 EVT VT = N->getValueType(0);
7081 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007082
Tanya Lattner0433b212011-04-07 15:24:20 +00007083 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7084 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007085
Owen Anderson60f48702010-11-03 23:15:26 +00007086 APInt SplatBits, SplatUndef;
7087 unsigned SplatBitSize;
7088 bool HasAnyUndefs;
7089 if (BVN && Subtarget->hasNEON() &&
7090 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7091 if (SplatBitSize <= 64) {
7092 EVT VorrVT;
7093 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7094 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007095 DAG, VorrVT, VT.is128BitVector(),
7096 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007097 if (Val.getNode()) {
7098 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007099 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007100 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007101 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007102 }
7103 }
7104 }
7105
Evan Chengc892aeb2012-02-23 01:19:06 +00007106 if (!Subtarget->isThumb1Only()) {
7107 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7108 SDValue COR = formConditionalOp(N, DAG, true);
7109 if (COR.getNode())
7110 return COR;
7111 }
7112
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007113 SDValue N0 = N->getOperand(0);
7114 if (N0.getOpcode() != ISD::AND)
7115 return SDValue();
7116 SDValue N1 = N->getOperand(1);
7117
7118 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7119 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7120 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7121 APInt SplatUndef;
7122 unsigned SplatBitSize;
7123 bool HasAnyUndefs;
7124
7125 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7126 APInt SplatBits0;
7127 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7128 HasAnyUndefs) && !HasAnyUndefs) {
7129 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7130 APInt SplatBits1;
7131 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7132 HasAnyUndefs) && !HasAnyUndefs &&
7133 SplatBits0 == ~SplatBits1) {
7134 // Canonicalize the vector type to make instruction selection simpler.
7135 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7136 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7137 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007138 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007139 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7140 }
7141 }
7142 }
7143
Jim Grosbach54238562010-07-17 03:30:54 +00007144 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7145 // reasonable.
7146
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007147 // BFI is only available on V6T2+
7148 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7149 return SDValue();
7150
Jim Grosbach54238562010-07-17 03:30:54 +00007151 DebugLoc DL = N->getDebugLoc();
7152 // 1) or (and A, mask), val => ARMbfi A, val, mask
7153 // iff (val & mask) == val
7154 //
7155 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7156 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007157 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007158 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007159 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007160 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007161
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007162 if (VT != MVT::i32)
7163 return SDValue();
7164
Evan Cheng30fb13f2010-12-13 20:32:54 +00007165 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007166
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007167 // The value and the mask need to be constants so we can verify this is
7168 // actually a bitfield set. If the mask is 0xffff, we can do better
7169 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007170 SDValue MaskOp = N0.getOperand(1);
7171 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7172 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007173 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007174 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007175 if (Mask == 0xffff)
7176 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007177 SDValue Res;
7178 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007179 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7180 if (N1C) {
7181 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007182 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007183 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007184
Evan Chenga9688c42010-12-11 04:11:38 +00007185 if (ARM::isBitFieldInvertedMask(Mask)) {
7186 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007187
Evan Cheng30fb13f2010-12-13 20:32:54 +00007188 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007189 DAG.getConstant(Val, MVT::i32),
7190 DAG.getConstant(Mask, MVT::i32));
7191
7192 // Do not add new nodes to DAG combiner worklist.
7193 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007194 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007195 }
Jim Grosbach54238562010-07-17 03:30:54 +00007196 } else if (N1.getOpcode() == ISD::AND) {
7197 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007198 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7199 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007200 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007201 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007202
Eric Christopher29aeed12011-03-26 01:21:03 +00007203 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7204 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007205 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007206 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007207 // The pack halfword instruction works better for masks that fit it,
7208 // so use that when it's available.
7209 if (Subtarget->hasT2ExtractPack() &&
7210 (Mask == 0xffff || Mask == 0xffff0000))
7211 return SDValue();
7212 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007213 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007214 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007215 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007216 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007217 DAG.getConstant(Mask, MVT::i32));
7218 // Do not add new nodes to DAG combiner worklist.
7219 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007220 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007221 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007222 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007223 // The pack halfword instruction works better for masks that fit it,
7224 // so use that when it's available.
7225 if (Subtarget->hasT2ExtractPack() &&
7226 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7227 return SDValue();
7228 // 2b
7229 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007230 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007231 DAG.getConstant(lsb, MVT::i32));
7232 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007233 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007234 // Do not add new nodes to DAG combiner worklist.
7235 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007236 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007237 }
7238 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007239
Evan Cheng30fb13f2010-12-13 20:32:54 +00007240 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7241 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7242 ARM::isBitFieldInvertedMask(~Mask)) {
7243 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7244 // where lsb(mask) == #shamt and masked bits of B are known zero.
7245 SDValue ShAmt = N00.getOperand(1);
7246 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7247 unsigned LSB = CountTrailingZeros_32(Mask);
7248 if (ShAmtC != LSB)
7249 return SDValue();
7250
7251 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7252 DAG.getConstant(~Mask, MVT::i32));
7253
7254 // Do not add new nodes to DAG combiner worklist.
7255 DCI.CombineTo(N, Res, false);
7256 }
7257
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007258 return SDValue();
7259}
7260
Evan Chengc892aeb2012-02-23 01:19:06 +00007261static SDValue PerformXORCombine(SDNode *N,
7262 TargetLowering::DAGCombinerInfo &DCI,
7263 const ARMSubtarget *Subtarget) {
7264 EVT VT = N->getValueType(0);
7265 SelectionDAG &DAG = DCI.DAG;
7266
7267 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7268 return SDValue();
7269
7270 if (!Subtarget->isThumb1Only()) {
7271 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7272 SDValue CXOR = formConditionalOp(N, DAG, true);
7273 if (CXOR.getNode())
7274 return CXOR;
7275 }
7276
7277 return SDValue();
7278}
7279
Evan Chengbf188ae2011-06-15 01:12:31 +00007280/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7281/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007282static SDValue PerformBFICombine(SDNode *N,
7283 TargetLowering::DAGCombinerInfo &DCI) {
7284 SDValue N1 = N->getOperand(1);
7285 if (N1.getOpcode() == ISD::AND) {
7286 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7287 if (!N11C)
7288 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007289 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7290 unsigned LSB = CountTrailingZeros_32(~InvMask);
7291 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7292 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007293 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007294 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007295 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7296 N->getOperand(0), N1.getOperand(0),
7297 N->getOperand(2));
7298 }
7299 return SDValue();
7300}
7301
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007302/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7303/// ARMISD::VMOVRRD.
7304static SDValue PerformVMOVRRDCombine(SDNode *N,
7305 TargetLowering::DAGCombinerInfo &DCI) {
7306 // vmovrrd(vmovdrr x, y) -> x,y
7307 SDValue InDouble = N->getOperand(0);
7308 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7309 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007310
7311 // vmovrrd(load f64) -> (load i32), (load i32)
7312 SDNode *InNode = InDouble.getNode();
7313 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7314 InNode->getValueType(0) == MVT::f64 &&
7315 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7316 !cast<LoadSDNode>(InNode)->isVolatile()) {
7317 // TODO: Should this be done for non-FrameIndex operands?
7318 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7319
7320 SelectionDAG &DAG = DCI.DAG;
7321 DebugLoc DL = LD->getDebugLoc();
7322 SDValue BasePtr = LD->getBasePtr();
7323 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7324 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007325 LD->isNonTemporal(), LD->isInvariant(),
7326 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007327
7328 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7329 DAG.getConstant(4, MVT::i32));
7330 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7331 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007332 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007333 std::min(4U, LD->getAlignment() / 2));
7334
7335 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7336 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7337 DCI.RemoveFromWorklist(LD);
7338 DAG.DeleteNode(LD);
7339 return Result;
7340 }
7341
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007342 return SDValue();
7343}
7344
7345/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7346/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7347static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7348 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7349 SDValue Op0 = N->getOperand(0);
7350 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007351 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007352 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007353 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007354 Op1 = Op1.getOperand(0);
7355 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7356 Op0.getNode() == Op1.getNode() &&
7357 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007358 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007359 N->getValueType(0), Op0.getOperand(0));
7360 return SDValue();
7361}
7362
Bob Wilson31600902010-12-21 06:43:19 +00007363/// PerformSTORECombine - Target-specific dag combine xforms for
7364/// ISD::STORE.
7365static SDValue PerformSTORECombine(SDNode *N,
7366 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007367 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007368 if (St->isVolatile())
7369 return SDValue();
7370
7371 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
7372 // pack all of the elements in one place. Next, store to memory in fewer
7373 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007374 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007375 EVT VT = StVal.getValueType();
7376 if (St->isTruncatingStore() && VT.isVector()) {
7377 SelectionDAG &DAG = DCI.DAG;
7378 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7379 EVT StVT = St->getMemoryVT();
7380 unsigned NumElems = VT.getVectorNumElements();
7381 assert(StVT != VT && "Cannot truncate to the same type");
7382 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7383 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7384
7385 // From, To sizes and ElemCount must be pow of two
7386 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7387
7388 // We are going to use the original vector elt for storing.
7389 // Accumulated smaller vector elements must be a multiple of the store size.
7390 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7391
7392 unsigned SizeRatio = FromEltSz / ToEltSz;
7393 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7394
7395 // Create a type on which we perform the shuffle.
7396 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7397 NumElems*SizeRatio);
7398 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7399
7400 DebugLoc DL = St->getDebugLoc();
7401 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7402 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7403 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7404
7405 // Can't shuffle using an illegal type.
7406 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7407
7408 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7409 DAG.getUNDEF(WideVec.getValueType()),
7410 ShuffleVec.data());
7411 // At this point all of the data is stored at the bottom of the
7412 // register. We now need to save it to mem.
7413
7414 // Find the largest store unit
7415 MVT StoreType = MVT::i8;
7416 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7417 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7418 MVT Tp = (MVT::SimpleValueType)tp;
7419 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7420 StoreType = Tp;
7421 }
7422 // Didn't find a legal store type.
7423 if (!TLI.isTypeLegal(StoreType))
7424 return SDValue();
7425
7426 // Bitcast the original vector into a vector of store-size units
7427 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7428 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7429 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7430 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7431 SmallVector<SDValue, 8> Chains;
7432 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7433 TLI.getPointerTy());
7434 SDValue BasePtr = St->getBasePtr();
7435
7436 // Perform one or more big stores into memory.
7437 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7438 for (unsigned I = 0; I < E; I++) {
7439 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7440 StoreType, ShuffWide,
7441 DAG.getIntPtrConstant(I));
7442 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7443 St->getPointerInfo(), St->isVolatile(),
7444 St->isNonTemporal(), St->getAlignment());
7445 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7446 Increment);
7447 Chains.push_back(Ch);
7448 }
7449 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7450 Chains.size());
7451 }
7452
7453 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007454 return SDValue();
7455
Chad Rosier96b66d62012-04-09 19:38:15 +00007456 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7457 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007458 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00007459 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007460 SelectionDAG &DAG = DCI.DAG;
7461 DebugLoc DL = St->getDebugLoc();
7462 SDValue BasePtr = St->getBasePtr();
7463 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7464 StVal.getNode()->getOperand(0), BasePtr,
7465 St->getPointerInfo(), St->isVolatile(),
7466 St->isNonTemporal(), St->getAlignment());
7467
7468 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7469 DAG.getConstant(4, MVT::i32));
7470 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7471 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7472 St->isNonTemporal(),
7473 std::min(4U, St->getAlignment() / 2));
7474 }
7475
7476 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007477 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7478 return SDValue();
7479
Chad Rosier96b66d62012-04-09 19:38:15 +00007480 // Bitcast an i64 store extracted from a vector to f64.
7481 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00007482 SelectionDAG &DAG = DCI.DAG;
7483 DebugLoc dl = StVal.getDebugLoc();
7484 SDValue IntVec = StVal.getOperand(0);
7485 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7486 IntVec.getValueType().getVectorNumElements());
7487 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7488 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7489 Vec, StVal.getOperand(1));
7490 dl = N->getDebugLoc();
7491 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7492 // Make the DAGCombiner fold the bitcasts.
7493 DCI.AddToWorklist(Vec.getNode());
7494 DCI.AddToWorklist(ExtElt.getNode());
7495 DCI.AddToWorklist(V.getNode());
7496 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7497 St->getPointerInfo(), St->isVolatile(),
7498 St->isNonTemporal(), St->getAlignment(),
7499 St->getTBAAInfo());
7500}
7501
7502/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7503/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7504/// i64 vector to have f64 elements, since the value can then be loaded
7505/// directly into a VFP register.
7506static bool hasNormalLoadOperand(SDNode *N) {
7507 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7508 for (unsigned i = 0; i < NumElts; ++i) {
7509 SDNode *Elt = N->getOperand(i).getNode();
7510 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7511 return true;
7512 }
7513 return false;
7514}
7515
Bob Wilson75f02882010-09-17 22:59:05 +00007516/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7517/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007518static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7519 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007520 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7521 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7522 // into a pair of GPRs, which is fine when the value is used as a scalar,
7523 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007524 SelectionDAG &DAG = DCI.DAG;
7525 if (N->getNumOperands() == 2) {
7526 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7527 if (RV.getNode())
7528 return RV;
7529 }
Bob Wilson75f02882010-09-17 22:59:05 +00007530
Bob Wilson31600902010-12-21 06:43:19 +00007531 // Load i64 elements as f64 values so that type legalization does not split
7532 // them up into i32 values.
7533 EVT VT = N->getValueType(0);
7534 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7535 return SDValue();
7536 DebugLoc dl = N->getDebugLoc();
7537 SmallVector<SDValue, 8> Ops;
7538 unsigned NumElts = VT.getVectorNumElements();
7539 for (unsigned i = 0; i < NumElts; ++i) {
7540 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7541 Ops.push_back(V);
7542 // Make the DAGCombiner fold the bitcast.
7543 DCI.AddToWorklist(V.getNode());
7544 }
7545 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7546 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7547 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7548}
7549
7550/// PerformInsertEltCombine - Target-specific dag combine xforms for
7551/// ISD::INSERT_VECTOR_ELT.
7552static SDValue PerformInsertEltCombine(SDNode *N,
7553 TargetLowering::DAGCombinerInfo &DCI) {
7554 // Bitcast an i64 load inserted into a vector to f64.
7555 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7556 EVT VT = N->getValueType(0);
7557 SDNode *Elt = N->getOperand(1).getNode();
7558 if (VT.getVectorElementType() != MVT::i64 ||
7559 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7560 return SDValue();
7561
7562 SelectionDAG &DAG = DCI.DAG;
7563 DebugLoc dl = N->getDebugLoc();
7564 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7565 VT.getVectorNumElements());
7566 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7567 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7568 // Make the DAGCombiner fold the bitcasts.
7569 DCI.AddToWorklist(Vec.getNode());
7570 DCI.AddToWorklist(V.getNode());
7571 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7572 Vec, V, N->getOperand(2));
7573 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007574}
7575
Bob Wilsonf20700c2010-10-27 20:38:28 +00007576/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7577/// ISD::VECTOR_SHUFFLE.
7578static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7579 // The LLVM shufflevector instruction does not require the shuffle mask
7580 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7581 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7582 // operands do not match the mask length, they are extended by concatenating
7583 // them with undef vectors. That is probably the right thing for other
7584 // targets, but for NEON it is better to concatenate two double-register
7585 // size vector operands into a single quad-register size vector. Do that
7586 // transformation here:
7587 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7588 // shuffle(concat(v1, v2), undef)
7589 SDValue Op0 = N->getOperand(0);
7590 SDValue Op1 = N->getOperand(1);
7591 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7592 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7593 Op0.getNumOperands() != 2 ||
7594 Op1.getNumOperands() != 2)
7595 return SDValue();
7596 SDValue Concat0Op1 = Op0.getOperand(1);
7597 SDValue Concat1Op1 = Op1.getOperand(1);
7598 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7599 Concat1Op1.getOpcode() != ISD::UNDEF)
7600 return SDValue();
7601 // Skip the transformation if any of the types are illegal.
7602 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7603 EVT VT = N->getValueType(0);
7604 if (!TLI.isTypeLegal(VT) ||
7605 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7606 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7607 return SDValue();
7608
7609 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7610 Op0.getOperand(0), Op1.getOperand(0));
7611 // Translate the shuffle mask.
7612 SmallVector<int, 16> NewMask;
7613 unsigned NumElts = VT.getVectorNumElements();
7614 unsigned HalfElts = NumElts/2;
7615 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7616 for (unsigned n = 0; n < NumElts; ++n) {
7617 int MaskElt = SVN->getMaskElt(n);
7618 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007619 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007620 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007621 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007622 NewElt = HalfElts + MaskElt - NumElts;
7623 NewMask.push_back(NewElt);
7624 }
7625 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7626 DAG.getUNDEF(VT), NewMask.data());
7627}
7628
Bob Wilson1c3ef902011-02-07 17:43:21 +00007629/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7630/// NEON load/store intrinsics to merge base address updates.
7631static SDValue CombineBaseUpdate(SDNode *N,
7632 TargetLowering::DAGCombinerInfo &DCI) {
7633 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7634 return SDValue();
7635
7636 SelectionDAG &DAG = DCI.DAG;
7637 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7638 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7639 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7640 SDValue Addr = N->getOperand(AddrOpIdx);
7641
7642 // Search for a use of the address operand that is an increment.
7643 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7644 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7645 SDNode *User = *UI;
7646 if (User->getOpcode() != ISD::ADD ||
7647 UI.getUse().getResNo() != Addr.getResNo())
7648 continue;
7649
7650 // Check that the add is independent of the load/store. Otherwise, folding
7651 // it would create a cycle.
7652 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7653 continue;
7654
7655 // Find the new opcode for the updating load/store.
7656 bool isLoad = true;
7657 bool isLaneOp = false;
7658 unsigned NewOpc = 0;
7659 unsigned NumVecs = 0;
7660 if (isIntrinsic) {
7661 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7662 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007663 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007664 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7665 NumVecs = 1; break;
7666 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7667 NumVecs = 2; break;
7668 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7669 NumVecs = 3; break;
7670 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7671 NumVecs = 4; break;
7672 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7673 NumVecs = 2; isLaneOp = true; break;
7674 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
7675 NumVecs = 3; isLaneOp = true; break;
7676 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
7677 NumVecs = 4; isLaneOp = true; break;
7678 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
7679 NumVecs = 1; isLoad = false; break;
7680 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
7681 NumVecs = 2; isLoad = false; break;
7682 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
7683 NumVecs = 3; isLoad = false; break;
7684 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
7685 NumVecs = 4; isLoad = false; break;
7686 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
7687 NumVecs = 2; isLoad = false; isLaneOp = true; break;
7688 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
7689 NumVecs = 3; isLoad = false; isLaneOp = true; break;
7690 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
7691 NumVecs = 4; isLoad = false; isLaneOp = true; break;
7692 }
7693 } else {
7694 isLaneOp = true;
7695 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00007696 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007697 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
7698 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
7699 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
7700 }
7701 }
7702
7703 // Find the size of memory referenced by the load/store.
7704 EVT VecTy;
7705 if (isLoad)
7706 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00007707 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00007708 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
7709 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
7710 if (isLaneOp)
7711 NumBytes /= VecTy.getVectorNumElements();
7712
7713 // If the increment is a constant, it must match the memory ref size.
7714 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
7715 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
7716 uint64_t IncVal = CInc->getZExtValue();
7717 if (IncVal != NumBytes)
7718 continue;
7719 } else if (NumBytes >= 3 * 16) {
7720 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
7721 // separate instructions that make it harder to use a non-constant update.
7722 continue;
7723 }
7724
7725 // Create the new updating load/store node.
7726 EVT Tys[6];
7727 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
7728 unsigned n;
7729 for (n = 0; n < NumResultVecs; ++n)
7730 Tys[n] = VecTy;
7731 Tys[n++] = MVT::i32;
7732 Tys[n] = MVT::Other;
7733 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
7734 SmallVector<SDValue, 8> Ops;
7735 Ops.push_back(N->getOperand(0)); // incoming chain
7736 Ops.push_back(N->getOperand(AddrOpIdx));
7737 Ops.push_back(Inc);
7738 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
7739 Ops.push_back(N->getOperand(i));
7740 }
7741 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
7742 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
7743 Ops.data(), Ops.size(),
7744 MemInt->getMemoryVT(),
7745 MemInt->getMemOperand());
7746
7747 // Update the uses.
7748 std::vector<SDValue> NewResults;
7749 for (unsigned i = 0; i < NumResultVecs; ++i) {
7750 NewResults.push_back(SDValue(UpdN.getNode(), i));
7751 }
7752 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
7753 DCI.CombineTo(N, NewResults);
7754 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
7755
7756 break;
Owen Anderson76706012011-04-05 21:48:57 +00007757 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00007758 return SDValue();
7759}
7760
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007761/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
7762/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
7763/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
7764/// return true.
7765static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
7766 SelectionDAG &DAG = DCI.DAG;
7767 EVT VT = N->getValueType(0);
7768 // vldN-dup instructions only support 64-bit vectors for N > 1.
7769 if (!VT.is64BitVector())
7770 return false;
7771
7772 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
7773 SDNode *VLD = N->getOperand(0).getNode();
7774 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
7775 return false;
7776 unsigned NumVecs = 0;
7777 unsigned NewOpc = 0;
7778 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
7779 if (IntNo == Intrinsic::arm_neon_vld2lane) {
7780 NumVecs = 2;
7781 NewOpc = ARMISD::VLD2DUP;
7782 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
7783 NumVecs = 3;
7784 NewOpc = ARMISD::VLD3DUP;
7785 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
7786 NumVecs = 4;
7787 NewOpc = ARMISD::VLD4DUP;
7788 } else {
7789 return false;
7790 }
7791
7792 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
7793 // numbers match the load.
7794 unsigned VLDLaneNo =
7795 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
7796 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7797 UI != UE; ++UI) {
7798 // Ignore uses of the chain result.
7799 if (UI.getUse().getResNo() == NumVecs)
7800 continue;
7801 SDNode *User = *UI;
7802 if (User->getOpcode() != ARMISD::VDUPLANE ||
7803 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
7804 return false;
7805 }
7806
7807 // Create the vldN-dup node.
7808 EVT Tys[5];
7809 unsigned n;
7810 for (n = 0; n < NumVecs; ++n)
7811 Tys[n] = VT;
7812 Tys[n] = MVT::Other;
7813 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
7814 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
7815 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
7816 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
7817 Ops, 2, VLDMemInt->getMemoryVT(),
7818 VLDMemInt->getMemOperand());
7819
7820 // Update the uses.
7821 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
7822 UI != UE; ++UI) {
7823 unsigned ResNo = UI.getUse().getResNo();
7824 // Ignore uses of the chain result.
7825 if (ResNo == NumVecs)
7826 continue;
7827 SDNode *User = *UI;
7828 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
7829 }
7830
7831 // Now the vldN-lane intrinsic is dead except for its chain result.
7832 // Update uses of the chain.
7833 std::vector<SDValue> VLDDupResults;
7834 for (unsigned n = 0; n < NumVecs; ++n)
7835 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
7836 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
7837 DCI.CombineTo(VLD, VLDDupResults);
7838
7839 return true;
7840}
7841
Bob Wilson9e82bf12010-07-14 01:22:12 +00007842/// PerformVDUPLANECombine - Target-specific dag combine xforms for
7843/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007844static SDValue PerformVDUPLANECombine(SDNode *N,
7845 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00007846 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007847
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007848 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
7849 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
7850 if (CombineVLDDUP(N, DCI))
7851 return SDValue(N, 0);
7852
7853 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
7854 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007855 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007856 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00007857 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00007858 return SDValue();
7859
7860 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
7861 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
7862 // The canonical VMOV for a zero vector uses a 32-bit element size.
7863 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7864 unsigned EltBits;
7865 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
7866 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007867 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007868 if (EltSize > VT.getVectorElementType().getSizeInBits())
7869 return SDValue();
7870
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00007871 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00007872}
7873
Eric Christopherfa6f5912011-06-29 21:10:36 +00007874// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00007875// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
7876static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
7877{
Chad Rosier118c9a02011-06-28 17:26:57 +00007878 integerPart cN;
7879 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00007880 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
7881 I != E; I++) {
7882 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
7883 if (!C)
7884 return false;
7885
Eric Christopherfa6f5912011-06-29 21:10:36 +00007886 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00007887 APFloat APF = C->getValueAPF();
7888 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
7889 != APFloat::opOK || !isExact)
7890 return false;
7891
7892 c0 = (I == 0) ? cN : c0;
7893 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
7894 return false;
7895 }
7896 C = c0;
7897 return true;
7898}
7899
7900/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
7901/// can replace combinations of VMUL and VCVT (floating-point to integer)
7902/// when the VMUL has a constant operand that is a power of 2.
7903///
7904/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7905/// vmul.f32 d16, d17, d16
7906/// vcvt.s32.f32 d16, d16
7907/// becomes:
7908/// vcvt.s32.f32 d16, d16, #3
7909static SDValue PerformVCVTCombine(SDNode *N,
7910 TargetLowering::DAGCombinerInfo &DCI,
7911 const ARMSubtarget *Subtarget) {
7912 SelectionDAG &DAG = DCI.DAG;
7913 SDValue Op = N->getOperand(0);
7914
7915 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
7916 Op.getOpcode() != ISD::FMUL)
7917 return SDValue();
7918
7919 uint64_t C;
7920 SDValue N0 = Op->getOperand(0);
7921 SDValue ConstVec = Op->getOperand(1);
7922 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
7923
Eric Christopherfa6f5912011-06-29 21:10:36 +00007924 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00007925 !isConstVecPow2(ConstVec, isSigned, C))
7926 return SDValue();
7927
7928 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
7929 Intrinsic::arm_neon_vcvtfp2fxu;
7930 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7931 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007932 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00007933 DAG.getConstant(Log2_64(C), MVT::i32));
7934}
7935
7936/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
7937/// can replace combinations of VCVT (integer to floating-point) and VDIV
7938/// when the VDIV has a constant operand that is a power of 2.
7939///
7940/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
7941/// vcvt.f32.s32 d16, d16
7942/// vdiv.f32 d16, d17, d16
7943/// becomes:
7944/// vcvt.f32.s32 d16, d16, #3
7945static SDValue PerformVDIVCombine(SDNode *N,
7946 TargetLowering::DAGCombinerInfo &DCI,
7947 const ARMSubtarget *Subtarget) {
7948 SelectionDAG &DAG = DCI.DAG;
7949 SDValue Op = N->getOperand(0);
7950 unsigned OpOpcode = Op.getNode()->getOpcode();
7951
7952 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
7953 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
7954 return SDValue();
7955
7956 uint64_t C;
7957 SDValue ConstVec = N->getOperand(1);
7958 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
7959
7960 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
7961 !isConstVecPow2(ConstVec, isSigned, C))
7962 return SDValue();
7963
Eric Christopherfa6f5912011-06-29 21:10:36 +00007964 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00007965 Intrinsic::arm_neon_vcvtfxu2fp;
7966 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7967 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00007968 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00007969 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
7970}
7971
7972/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00007973/// operand of a vector shift operation, where all the elements of the
7974/// build_vector must have the same constant integer value.
7975static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
7976 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007977 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00007978 Op = Op.getOperand(0);
7979 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
7980 APInt SplatBits, SplatUndef;
7981 unsigned SplatBitSize;
7982 bool HasAnyUndefs;
7983 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
7984 HasAnyUndefs, ElementBits) ||
7985 SplatBitSize > ElementBits)
7986 return false;
7987 Cnt = SplatBits.getSExtValue();
7988 return true;
7989}
7990
7991/// isVShiftLImm - Check if this is a valid build_vector for the immediate
7992/// operand of a vector shift left operation. That value must be in the range:
7993/// 0 <= Value < ElementBits for a left shift; or
7994/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00007995static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00007996 assert(VT.isVector() && "vector shift count is not a vector type");
7997 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
7998 if (! getVShiftImm(Op, ElementBits, Cnt))
7999 return false;
8000 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8001}
8002
8003/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8004/// operand of a vector shift right operation. For a shift opcode, the value
8005/// is positive, but for an intrinsic the value count must be negative. The
8006/// absolute value must be in the range:
8007/// 1 <= |Value| <= ElementBits for a right shift; or
8008/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008009static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008010 int64_t &Cnt) {
8011 assert(VT.isVector() && "vector shift count is not a vector type");
8012 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8013 if (! getVShiftImm(Op, ElementBits, Cnt))
8014 return false;
8015 if (isIntrinsic)
8016 Cnt = -Cnt;
8017 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8018}
8019
8020/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8021static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8022 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8023 switch (IntNo) {
8024 default:
8025 // Don't do anything for most intrinsics.
8026 break;
8027
8028 // Vector shifts: check for immediate versions and lower them.
8029 // Note: This is done during DAG combining instead of DAG legalizing because
8030 // the build_vectors for 64-bit vector element shift counts are generally
8031 // not legal, and it is hard to see their values after they get legalized to
8032 // loads from a constant pool.
8033 case Intrinsic::arm_neon_vshifts:
8034 case Intrinsic::arm_neon_vshiftu:
8035 case Intrinsic::arm_neon_vshiftls:
8036 case Intrinsic::arm_neon_vshiftlu:
8037 case Intrinsic::arm_neon_vshiftn:
8038 case Intrinsic::arm_neon_vrshifts:
8039 case Intrinsic::arm_neon_vrshiftu:
8040 case Intrinsic::arm_neon_vrshiftn:
8041 case Intrinsic::arm_neon_vqshifts:
8042 case Intrinsic::arm_neon_vqshiftu:
8043 case Intrinsic::arm_neon_vqshiftsu:
8044 case Intrinsic::arm_neon_vqshiftns:
8045 case Intrinsic::arm_neon_vqshiftnu:
8046 case Intrinsic::arm_neon_vqshiftnsu:
8047 case Intrinsic::arm_neon_vqrshiftns:
8048 case Intrinsic::arm_neon_vqrshiftnu:
8049 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008050 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008051 int64_t Cnt;
8052 unsigned VShiftOpc = 0;
8053
8054 switch (IntNo) {
8055 case Intrinsic::arm_neon_vshifts:
8056 case Intrinsic::arm_neon_vshiftu:
8057 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8058 VShiftOpc = ARMISD::VSHL;
8059 break;
8060 }
8061 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8062 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8063 ARMISD::VSHRs : ARMISD::VSHRu);
8064 break;
8065 }
8066 return SDValue();
8067
8068 case Intrinsic::arm_neon_vshiftls:
8069 case Intrinsic::arm_neon_vshiftlu:
8070 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8071 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008072 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008073
8074 case Intrinsic::arm_neon_vrshifts:
8075 case Intrinsic::arm_neon_vrshiftu:
8076 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8077 break;
8078 return SDValue();
8079
8080 case Intrinsic::arm_neon_vqshifts:
8081 case Intrinsic::arm_neon_vqshiftu:
8082 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8083 break;
8084 return SDValue();
8085
8086 case Intrinsic::arm_neon_vqshiftsu:
8087 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8088 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008089 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008090
8091 case Intrinsic::arm_neon_vshiftn:
8092 case Intrinsic::arm_neon_vrshiftn:
8093 case Intrinsic::arm_neon_vqshiftns:
8094 case Intrinsic::arm_neon_vqshiftnu:
8095 case Intrinsic::arm_neon_vqshiftnsu:
8096 case Intrinsic::arm_neon_vqrshiftns:
8097 case Intrinsic::arm_neon_vqrshiftnu:
8098 case Intrinsic::arm_neon_vqrshiftnsu:
8099 // Narrowing shifts require an immediate right shift.
8100 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8101 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008102 llvm_unreachable("invalid shift count for narrowing vector shift "
8103 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008104
8105 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008106 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008107 }
8108
8109 switch (IntNo) {
8110 case Intrinsic::arm_neon_vshifts:
8111 case Intrinsic::arm_neon_vshiftu:
8112 // Opcode already set above.
8113 break;
8114 case Intrinsic::arm_neon_vshiftls:
8115 case Intrinsic::arm_neon_vshiftlu:
8116 if (Cnt == VT.getVectorElementType().getSizeInBits())
8117 VShiftOpc = ARMISD::VSHLLi;
8118 else
8119 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8120 ARMISD::VSHLLs : ARMISD::VSHLLu);
8121 break;
8122 case Intrinsic::arm_neon_vshiftn:
8123 VShiftOpc = ARMISD::VSHRN; break;
8124 case Intrinsic::arm_neon_vrshifts:
8125 VShiftOpc = ARMISD::VRSHRs; break;
8126 case Intrinsic::arm_neon_vrshiftu:
8127 VShiftOpc = ARMISD::VRSHRu; break;
8128 case Intrinsic::arm_neon_vrshiftn:
8129 VShiftOpc = ARMISD::VRSHRN; break;
8130 case Intrinsic::arm_neon_vqshifts:
8131 VShiftOpc = ARMISD::VQSHLs; break;
8132 case Intrinsic::arm_neon_vqshiftu:
8133 VShiftOpc = ARMISD::VQSHLu; break;
8134 case Intrinsic::arm_neon_vqshiftsu:
8135 VShiftOpc = ARMISD::VQSHLsu; break;
8136 case Intrinsic::arm_neon_vqshiftns:
8137 VShiftOpc = ARMISD::VQSHRNs; break;
8138 case Intrinsic::arm_neon_vqshiftnu:
8139 VShiftOpc = ARMISD::VQSHRNu; break;
8140 case Intrinsic::arm_neon_vqshiftnsu:
8141 VShiftOpc = ARMISD::VQSHRNsu; break;
8142 case Intrinsic::arm_neon_vqrshiftns:
8143 VShiftOpc = ARMISD::VQRSHRNs; break;
8144 case Intrinsic::arm_neon_vqrshiftnu:
8145 VShiftOpc = ARMISD::VQRSHRNu; break;
8146 case Intrinsic::arm_neon_vqrshiftnsu:
8147 VShiftOpc = ARMISD::VQRSHRNsu; break;
8148 }
8149
8150 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008151 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008152 }
8153
8154 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008155 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008156 int64_t Cnt;
8157 unsigned VShiftOpc = 0;
8158
8159 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8160 VShiftOpc = ARMISD::VSLI;
8161 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8162 VShiftOpc = ARMISD::VSRI;
8163 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008164 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008165 }
8166
8167 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8168 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008169 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008170 }
8171
8172 case Intrinsic::arm_neon_vqrshifts:
8173 case Intrinsic::arm_neon_vqrshiftu:
8174 // No immediate versions of these to check for.
8175 break;
8176 }
8177
8178 return SDValue();
8179}
8180
8181/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8182/// lowers them. As with the vector shift intrinsics, this is done during DAG
8183/// combining instead of DAG legalizing because the build_vectors for 64-bit
8184/// vector element shift counts are generally not legal, and it is hard to see
8185/// their values after they get legalized to loads from a constant pool.
8186static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8187 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008188 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008189 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8190 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8191 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8192 SDValue N1 = N->getOperand(1);
8193 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8194 SDValue N0 = N->getOperand(0);
8195 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8196 DAG.MaskedValueIsZero(N0.getOperand(0),
8197 APInt::getHighBitsSet(32, 16)))
8198 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8199 }
8200 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008201
8202 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008203 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8204 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008205 return SDValue();
8206
8207 assert(ST->hasNEON() && "unexpected vector shift");
8208 int64_t Cnt;
8209
8210 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008211 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008212
8213 case ISD::SHL:
8214 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8215 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008216 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008217 break;
8218
8219 case ISD::SRA:
8220 case ISD::SRL:
8221 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8222 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8223 ARMISD::VSHRs : ARMISD::VSHRu);
8224 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008225 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008226 }
8227 }
8228 return SDValue();
8229}
8230
8231/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8232/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8233static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8234 const ARMSubtarget *ST) {
8235 SDValue N0 = N->getOperand(0);
8236
8237 // Check for sign- and zero-extensions of vector extract operations of 8-
8238 // and 16-bit vector elements. NEON supports these directly. They are
8239 // handled during DAG combining because type legalization will promote them
8240 // to 32-bit types and it is messy to recognize the operations after that.
8241 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8242 SDValue Vec = N0.getOperand(0);
8243 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008244 EVT VT = N->getValueType(0);
8245 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008246 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8247
Owen Anderson825b72b2009-08-11 20:47:22 +00008248 if (VT == MVT::i32 &&
8249 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008250 TLI.isTypeLegal(Vec.getValueType()) &&
8251 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008252
8253 unsigned Opc = 0;
8254 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008255 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008256 case ISD::SIGN_EXTEND:
8257 Opc = ARMISD::VGETLANEs;
8258 break;
8259 case ISD::ZERO_EXTEND:
8260 case ISD::ANY_EXTEND:
8261 Opc = ARMISD::VGETLANEu;
8262 break;
8263 }
8264 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8265 }
8266 }
8267
8268 return SDValue();
8269}
8270
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008271/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8272/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8273static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8274 const ARMSubtarget *ST) {
8275 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008276 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008277 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8278 // a NaN; only do the transformation when it matches that behavior.
8279
8280 // For now only do this when using NEON for FP operations; if using VFP, it
8281 // is not obvious that the benefit outweighs the cost of switching to the
8282 // NEON pipeline.
8283 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8284 N->getValueType(0) != MVT::f32)
8285 return SDValue();
8286
8287 SDValue CondLHS = N->getOperand(0);
8288 SDValue CondRHS = N->getOperand(1);
8289 SDValue LHS = N->getOperand(2);
8290 SDValue RHS = N->getOperand(3);
8291 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8292
8293 unsigned Opcode = 0;
8294 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008295 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008296 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008297 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008298 IsReversed = true ; // x CC y ? y : x
8299 } else {
8300 return SDValue();
8301 }
8302
Bob Wilsone742bb52010-02-24 22:15:53 +00008303 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008304 switch (CC) {
8305 default: break;
8306 case ISD::SETOLT:
8307 case ISD::SETOLE:
8308 case ISD::SETLT:
8309 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008310 case ISD::SETULT:
8311 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008312 // If LHS is NaN, an ordered comparison will be false and the result will
8313 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8314 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8315 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8316 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8317 break;
8318 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8319 // will return -0, so vmin can only be used for unsafe math or if one of
8320 // the operands is known to be nonzero.
8321 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008322 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008323 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8324 break;
8325 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008326 break;
8327
8328 case ISD::SETOGT:
8329 case ISD::SETOGE:
8330 case ISD::SETGT:
8331 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008332 case ISD::SETUGT:
8333 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008334 // If LHS is NaN, an ordered comparison will be false and the result will
8335 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8336 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8337 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8338 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8339 break;
8340 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8341 // will return +0, so vmax can only be used for unsafe math or if one of
8342 // the operands is known to be nonzero.
8343 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008344 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008345 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8346 break;
8347 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008348 break;
8349 }
8350
8351 if (!Opcode)
8352 return SDValue();
8353 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8354}
8355
Evan Chenge721f5c2011-07-13 00:42:17 +00008356/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8357SDValue
8358ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8359 SDValue Cmp = N->getOperand(4);
8360 if (Cmp.getOpcode() != ARMISD::CMPZ)
8361 // Only looking at EQ and NE cases.
8362 return SDValue();
8363
8364 EVT VT = N->getValueType(0);
8365 DebugLoc dl = N->getDebugLoc();
8366 SDValue LHS = Cmp.getOperand(0);
8367 SDValue RHS = Cmp.getOperand(1);
8368 SDValue FalseVal = N->getOperand(0);
8369 SDValue TrueVal = N->getOperand(1);
8370 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008371 ARMCC::CondCodes CC =
8372 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008373
8374 // Simplify
8375 // mov r1, r0
8376 // cmp r1, x
8377 // mov r0, y
8378 // moveq r0, x
8379 // to
8380 // cmp r0, x
8381 // movne r0, y
8382 //
8383 // mov r1, r0
8384 // cmp r1, x
8385 // mov r0, x
8386 // movne r0, y
8387 // to
8388 // cmp r0, x
8389 // movne r0, y
8390 /// FIXME: Turn this into a target neutral optimization?
8391 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008392 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008393 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8394 N->getOperand(3), Cmp);
8395 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8396 SDValue ARMcc;
8397 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8398 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8399 N->getOperand(3), NewCmp);
8400 }
8401
8402 if (Res.getNode()) {
8403 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008404 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008405 // Capture demanded bits information that would be otherwise lost.
8406 if (KnownZero == 0xfffffffe)
8407 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8408 DAG.getValueType(MVT::i1));
8409 else if (KnownZero == 0xffffff00)
8410 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8411 DAG.getValueType(MVT::i8));
8412 else if (KnownZero == 0xffff0000)
8413 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8414 DAG.getValueType(MVT::i16));
8415 }
8416
8417 return Res;
8418}
8419
Dan Gohman475871a2008-07-27 21:46:04 +00008420SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008421 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008422 switch (N->getOpcode()) {
8423 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008424 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008425 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008426 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008427 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008428 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8429 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008430 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008431 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008432 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008433 case ISD::STORE: return PerformSTORECombine(N, DCI);
8434 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8435 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008436 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008437 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008438 case ISD::FP_TO_SINT:
8439 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8440 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008441 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008442 case ISD::SHL:
8443 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008444 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008445 case ISD::SIGN_EXTEND:
8446 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008447 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8448 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008449 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008450 case ARMISD::VLD2DUP:
8451 case ARMISD::VLD3DUP:
8452 case ARMISD::VLD4DUP:
8453 return CombineBaseUpdate(N, DCI);
8454 case ISD::INTRINSIC_VOID:
8455 case ISD::INTRINSIC_W_CHAIN:
8456 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8457 case Intrinsic::arm_neon_vld1:
8458 case Intrinsic::arm_neon_vld2:
8459 case Intrinsic::arm_neon_vld3:
8460 case Intrinsic::arm_neon_vld4:
8461 case Intrinsic::arm_neon_vld2lane:
8462 case Intrinsic::arm_neon_vld3lane:
8463 case Intrinsic::arm_neon_vld4lane:
8464 case Intrinsic::arm_neon_vst1:
8465 case Intrinsic::arm_neon_vst2:
8466 case Intrinsic::arm_neon_vst3:
8467 case Intrinsic::arm_neon_vst4:
8468 case Intrinsic::arm_neon_vst2lane:
8469 case Intrinsic::arm_neon_vst3lane:
8470 case Intrinsic::arm_neon_vst4lane:
8471 return CombineBaseUpdate(N, DCI);
8472 default: break;
8473 }
8474 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008475 }
Dan Gohman475871a2008-07-27 21:46:04 +00008476 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008477}
8478
Evan Cheng31959b12011-02-02 01:06:55 +00008479bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8480 EVT VT) const {
8481 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8482}
8483
Bill Wendlingaf566342009-08-15 21:21:19 +00008484bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008485 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008486 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008487
8488 switch (VT.getSimpleVT().SimpleTy) {
8489 default:
8490 return false;
8491 case MVT::i8:
8492 case MVT::i16:
8493 case MVT::i32:
8494 return true;
8495 // FIXME: VLD1 etc with standard alignment is legal.
8496 }
8497}
8498
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008499static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8500 unsigned AlignCheck) {
8501 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8502 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8503}
8504
8505EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8506 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008507 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008508 bool MemcpyStrSrc,
8509 MachineFunction &MF) const {
8510 const Function *F = MF.getFunction();
8511
8512 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008513 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008514 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8515 Subtarget->hasNEON()) {
8516 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8517 return MVT::v4i32;
8518 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8519 return MVT::v2i32;
8520 }
8521 }
8522
Lang Hames5207bf22011-11-08 18:56:23 +00008523 // Lowering to i32/i16 if the size permits.
8524 if (Size >= 4) {
8525 return MVT::i32;
8526 } else if (Size >= 2) {
8527 return MVT::i16;
8528 }
8529
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008530 // Let the target-independent logic figure it out.
8531 return MVT::Other;
8532}
8533
Evan Chenge6c835f2009-08-14 20:09:37 +00008534static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8535 if (V < 0)
8536 return false;
8537
8538 unsigned Scale = 1;
8539 switch (VT.getSimpleVT().SimpleTy) {
8540 default: return false;
8541 case MVT::i1:
8542 case MVT::i8:
8543 // Scale == 1;
8544 break;
8545 case MVT::i16:
8546 // Scale == 2;
8547 Scale = 2;
8548 break;
8549 case MVT::i32:
8550 // Scale == 4;
8551 Scale = 4;
8552 break;
8553 }
8554
8555 if ((V & (Scale - 1)) != 0)
8556 return false;
8557 V /= Scale;
8558 return V == (V & ((1LL << 5) - 1));
8559}
8560
8561static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8562 const ARMSubtarget *Subtarget) {
8563 bool isNeg = false;
8564 if (V < 0) {
8565 isNeg = true;
8566 V = - V;
8567 }
8568
8569 switch (VT.getSimpleVT().SimpleTy) {
8570 default: return false;
8571 case MVT::i1:
8572 case MVT::i8:
8573 case MVT::i16:
8574 case MVT::i32:
8575 // + imm12 or - imm8
8576 if (isNeg)
8577 return V == (V & ((1LL << 8) - 1));
8578 return V == (V & ((1LL << 12) - 1));
8579 case MVT::f32:
8580 case MVT::f64:
8581 // Same as ARM mode. FIXME: NEON?
8582 if (!Subtarget->hasVFP2())
8583 return false;
8584 if ((V & 3) != 0)
8585 return false;
8586 V >>= 2;
8587 return V == (V & ((1LL << 8) - 1));
8588 }
8589}
8590
Evan Chengb01fad62007-03-12 23:30:29 +00008591/// isLegalAddressImmediate - Return true if the integer value can be used
8592/// as the offset of the target addressing mode for load / store of the
8593/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008594static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008595 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008596 if (V == 0)
8597 return true;
8598
Evan Cheng65011532009-03-09 19:15:00 +00008599 if (!VT.isSimple())
8600 return false;
8601
Evan Chenge6c835f2009-08-14 20:09:37 +00008602 if (Subtarget->isThumb1Only())
8603 return isLegalT1AddressImmediate(V, VT);
8604 else if (Subtarget->isThumb2())
8605 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008606
Evan Chenge6c835f2009-08-14 20:09:37 +00008607 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008608 if (V < 0)
8609 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008610 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008611 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008612 case MVT::i1:
8613 case MVT::i8:
8614 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008615 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008616 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008617 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008618 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008619 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008620 case MVT::f32:
8621 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008622 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008623 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008624 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008625 return false;
8626 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008627 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008628 }
Evan Chenga8e29892007-01-19 07:51:42 +00008629}
8630
Evan Chenge6c835f2009-08-14 20:09:37 +00008631bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8632 EVT VT) const {
8633 int Scale = AM.Scale;
8634 if (Scale < 0)
8635 return false;
8636
8637 switch (VT.getSimpleVT().SimpleTy) {
8638 default: return false;
8639 case MVT::i1:
8640 case MVT::i8:
8641 case MVT::i16:
8642 case MVT::i32:
8643 if (Scale == 1)
8644 return true;
8645 // r + r << imm
8646 Scale = Scale & ~1;
8647 return Scale == 2 || Scale == 4 || Scale == 8;
8648 case MVT::i64:
8649 // r + r
8650 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8651 return true;
8652 return false;
8653 case MVT::isVoid:
8654 // Note, we allow "void" uses (basically, uses that aren't loads or
8655 // stores), because arm allows folding a scale into many arithmetic
8656 // operations. This should be made more precise and revisited later.
8657
8658 // Allow r << imm, but the imm has to be a multiple of two.
8659 if (Scale & 1) return false;
8660 return isPowerOf2_32(Scale);
8661 }
8662}
8663
Chris Lattner37caf8c2007-04-09 23:33:39 +00008664/// isLegalAddressingMode - Return true if the addressing mode represented
8665/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008666bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008667 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008668 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008669 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008670 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008671
Chris Lattner37caf8c2007-04-09 23:33:39 +00008672 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008673 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008674 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008675
Chris Lattner37caf8c2007-04-09 23:33:39 +00008676 switch (AM.Scale) {
8677 case 0: // no scale reg, must be "r+i" or "r", or "i".
8678 break;
8679 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00008680 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00008681 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008682 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00008683 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00008684 // ARM doesn't support any R+R*scale+imm addr modes.
8685 if (AM.BaseOffs)
8686 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008687
Bob Wilson2c7dab12009-04-08 17:55:28 +00008688 if (!VT.isSimple())
8689 return false;
8690
Evan Chenge6c835f2009-08-14 20:09:37 +00008691 if (Subtarget->isThumb2())
8692 return isLegalT2ScaledAddressingMode(AM, VT);
8693
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008694 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00008695 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00008696 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008697 case MVT::i1:
8698 case MVT::i8:
8699 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008700 if (Scale < 0) Scale = -Scale;
8701 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008702 return true;
8703 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00008704 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008705 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00008706 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008707 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00008708 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00008709 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00008710 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008711
Owen Anderson825b72b2009-08-11 20:47:22 +00008712 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00008713 // Note, we allow "void" uses (basically, uses that aren't loads or
8714 // stores), because arm allows folding a scale into many arithmetic
8715 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008716
Chris Lattner37caf8c2007-04-09 23:33:39 +00008717 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00008718 if (Scale & 1) return false;
8719 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00008720 }
Evan Chengb01fad62007-03-12 23:30:29 +00008721 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00008722 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00008723}
8724
Evan Cheng77e47512009-11-11 19:05:52 +00008725/// isLegalICmpImmediate - Return true if the specified immediate is legal
8726/// icmp immediate, that is the target has icmp instructions which can compare
8727/// a register against the immediate without having to materialize the
8728/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00008729bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00008730 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00008731 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00008732 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00008733 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00008734 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00008735 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00008736 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00008737}
8738
Dan Gohmancca82142011-05-03 00:46:49 +00008739/// isLegalAddImmediate - Return true if the specified immediate is legal
8740/// add immediate, that is the target has add instructions which can add
8741/// a register with the immediate without having to materialize the
8742/// immediate into a register.
8743bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8744 return ARM_AM::getSOImmVal(Imm) != -1;
8745}
8746
Owen Andersone50ed302009-08-10 22:56:29 +00008747static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008748 bool isSEXTLoad, SDValue &Base,
8749 SDValue &Offset, bool &isInc,
8750 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00008751 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8752 return false;
8753
Owen Anderson825b72b2009-08-11 20:47:22 +00008754 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00008755 // AddressingMode 3
8756 Base = Ptr->getOperand(0);
8757 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008758 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008759 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008760 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008761 isInc = false;
8762 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8763 return true;
8764 }
8765 }
8766 isInc = (Ptr->getOpcode() == ISD::ADD);
8767 Offset = Ptr->getOperand(1);
8768 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00008769 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00008770 // AddressingMode 2
8771 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008772 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00008773 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008774 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00008775 isInc = false;
8776 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8777 Base = Ptr->getOperand(0);
8778 return true;
8779 }
8780 }
8781
8782 if (Ptr->getOpcode() == ISD::ADD) {
8783 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00008784 ARM_AM::ShiftOpc ShOpcVal=
8785 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00008786 if (ShOpcVal != ARM_AM::no_shift) {
8787 Base = Ptr->getOperand(1);
8788 Offset = Ptr->getOperand(0);
8789 } else {
8790 Base = Ptr->getOperand(0);
8791 Offset = Ptr->getOperand(1);
8792 }
8793 return true;
8794 }
8795
8796 isInc = (Ptr->getOpcode() == ISD::ADD);
8797 Base = Ptr->getOperand(0);
8798 Offset = Ptr->getOperand(1);
8799 return true;
8800 }
8801
Jim Grosbache5165492009-11-09 00:11:35 +00008802 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00008803 return false;
8804}
8805
Owen Andersone50ed302009-08-10 22:56:29 +00008806static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00008807 bool isSEXTLoad, SDValue &Base,
8808 SDValue &Offset, bool &isInc,
8809 SelectionDAG &DAG) {
8810 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
8811 return false;
8812
8813 Base = Ptr->getOperand(0);
8814 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
8815 int RHSC = (int)RHS->getZExtValue();
8816 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
8817 assert(Ptr->getOpcode() == ISD::ADD);
8818 isInc = false;
8819 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
8820 return true;
8821 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
8822 isInc = Ptr->getOpcode() == ISD::ADD;
8823 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
8824 return true;
8825 }
8826 }
8827
8828 return false;
8829}
8830
Evan Chenga8e29892007-01-19 07:51:42 +00008831/// getPreIndexedAddressParts - returns true by value, base pointer and
8832/// offset pointer and addressing mode by reference if the node's address
8833/// can be legally represented as pre-indexed load / store address.
8834bool
Dan Gohman475871a2008-07-27 21:46:04 +00008835ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
8836 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008837 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008838 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008839 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008840 return false;
8841
Owen Andersone50ed302009-08-10 22:56:29 +00008842 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008843 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008844 bool isSEXTLoad = false;
8845 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
8846 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008847 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008848 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8849 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
8850 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008851 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00008852 } else
8853 return false;
8854
8855 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008856 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008857 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008858 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
8859 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008860 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008861 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00008862 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00008863 if (!isLegal)
8864 return false;
8865
8866 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
8867 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008868}
8869
8870/// getPostIndexedAddressParts - returns true by value, base pointer and
8871/// offset pointer and addressing mode by reference if this node can be
8872/// combined with a load / store to form a post-indexed load / store.
8873bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00008874 SDValue &Base,
8875 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00008876 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00008877 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00008878 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00008879 return false;
8880
Owen Andersone50ed302009-08-10 22:56:29 +00008881 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00008882 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00008883 bool isSEXTLoad = false;
8884 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008885 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008886 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008887 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
8888 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008889 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00008890 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00008891 } else
8892 return false;
8893
8894 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00008895 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00008896 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00008897 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00008898 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00008899 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00008900 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
8901 isInc, DAG);
8902 if (!isLegal)
8903 return false;
8904
Evan Cheng28dad2a2010-05-18 21:31:17 +00008905 if (Ptr != Base) {
8906 // Swap base ptr and offset to catch more post-index load / store when
8907 // it's legal. In Thumb2 mode, offset must be an immediate.
8908 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
8909 !Subtarget->isThumb2())
8910 std::swap(Base, Offset);
8911
8912 // Post-indexed load / store update the base pointer.
8913 if (Ptr != Base)
8914 return false;
8915 }
8916
Evan Chenge88d5ce2009-07-02 07:28:31 +00008917 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
8918 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00008919}
8920
Dan Gohman475871a2008-07-27 21:46:04 +00008921void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008922 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008923 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008924 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00008925 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008926 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00008927 switch (Op.getOpcode()) {
8928 default: break;
8929 case ARMISD::CMOV: {
8930 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008931 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008932 if (KnownZero == 0 && KnownOne == 0) return;
8933
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008934 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008935 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00008936 KnownZero &= KnownZeroRHS;
8937 KnownOne &= KnownOneRHS;
8938 return;
8939 }
8940 }
8941}
8942
8943//===----------------------------------------------------------------------===//
8944// ARM Inline Assembly Support
8945//===----------------------------------------------------------------------===//
8946
Evan Cheng55d42002011-01-08 01:24:27 +00008947bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
8948 // Looking for "rev" which is V6+.
8949 if (!Subtarget->hasV6Ops())
8950 return false;
8951
8952 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8953 std::string AsmStr = IA->getAsmString();
8954 SmallVector<StringRef, 4> AsmPieces;
8955 SplitString(AsmStr, AsmPieces, ";\n");
8956
8957 switch (AsmPieces.size()) {
8958 default: return false;
8959 case 1:
8960 AsmStr = AsmPieces[0];
8961 AsmPieces.clear();
8962 SplitString(AsmStr, AsmPieces, " \t,");
8963
8964 // rev $0, $1
8965 if (AsmPieces.size() == 3 &&
8966 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
8967 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008968 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00008969 if (Ty && Ty->getBitWidth() == 32)
8970 return IntrinsicLowering::LowerToByteSwap(CI);
8971 }
8972 break;
8973 }
8974
8975 return false;
8976}
8977
Evan Chenga8e29892007-01-19 07:51:42 +00008978/// getConstraintType - Given a constraint letter, return the type of
8979/// constraint it is for this target.
8980ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008981ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
8982 if (Constraint.size() == 1) {
8983 switch (Constraint[0]) {
8984 default: break;
8985 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00008986 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00008987 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00008988 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00008989 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00008990 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00008991 // An address with a single base register. Due to the way we
8992 // currently handle addresses it is the same as an 'r' memory constraint.
8993 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00008994 }
Eric Christopher1312ca82011-06-21 22:10:57 +00008995 } else if (Constraint.size() == 2) {
8996 switch (Constraint[0]) {
8997 default: break;
8998 // All 'U+' constraints are addresses.
8999 case 'U': return C_Memory;
9000 }
Evan Chenga8e29892007-01-19 07:51:42 +00009001 }
Chris Lattner4234f572007-03-25 02:14:49 +00009002 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009003}
9004
John Thompson44ab89e2010-10-29 17:29:13 +00009005/// Examine constraint type and operand type and determine a weight value.
9006/// This object must already have been set up with the operand type
9007/// and the current alternative constraint selected.
9008TargetLowering::ConstraintWeight
9009ARMTargetLowering::getSingleConstraintMatchWeight(
9010 AsmOperandInfo &info, const char *constraint) const {
9011 ConstraintWeight weight = CW_Invalid;
9012 Value *CallOperandVal = info.CallOperandVal;
9013 // If we don't have a value, we can't do a match,
9014 // but allow it at the lowest weight.
9015 if (CallOperandVal == NULL)
9016 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009017 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009018 // Look at the constraint type.
9019 switch (*constraint) {
9020 default:
9021 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9022 break;
9023 case 'l':
9024 if (type->isIntegerTy()) {
9025 if (Subtarget->isThumb())
9026 weight = CW_SpecificReg;
9027 else
9028 weight = CW_Register;
9029 }
9030 break;
9031 case 'w':
9032 if (type->isFloatingPointTy())
9033 weight = CW_Register;
9034 break;
9035 }
9036 return weight;
9037}
9038
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009039typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9040RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009041ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009042 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009043 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009044 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009045 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009046 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009047 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009048 return RCPair(0U, &ARM::tGPRRegClass);
9049 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009050 case 'h': // High regs or no regs.
9051 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009052 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009053 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009054 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009055 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009056 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009057 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009058 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009059 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009060 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009061 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009062 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009063 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009064 case 'x':
9065 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009066 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009067 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009068 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009069 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009070 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009071 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009072 case 't':
9073 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009074 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009075 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009076 }
9077 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009078 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009079 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009080
Evan Chenga8e29892007-01-19 07:51:42 +00009081 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9082}
9083
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009084/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9085/// vector. If it is invalid, don't add anything to Ops.
9086void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009087 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009088 std::vector<SDValue>&Ops,
9089 SelectionDAG &DAG) const {
9090 SDValue Result(0, 0);
9091
Eric Christopher100c8332011-06-02 23:16:42 +00009092 // Currently only support length 1 constraints.
9093 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009094
Eric Christopher100c8332011-06-02 23:16:42 +00009095 char ConstraintLetter = Constraint[0];
9096 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009097 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009098 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009099 case 'I': case 'J': case 'K': case 'L':
9100 case 'M': case 'N': case 'O':
9101 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9102 if (!C)
9103 return;
9104
9105 int64_t CVal64 = C->getSExtValue();
9106 int CVal = (int) CVal64;
9107 // None of these constraints allow values larger than 32 bits. Check
9108 // that the value fits in an int.
9109 if (CVal != CVal64)
9110 return;
9111
Eric Christopher100c8332011-06-02 23:16:42 +00009112 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009113 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009114 // Constant suitable for movw, must be between 0 and
9115 // 65535.
9116 if (Subtarget->hasV6T2Ops())
9117 if (CVal >= 0 && CVal <= 65535)
9118 break;
9119 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009120 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009121 if (Subtarget->isThumb1Only()) {
9122 // This must be a constant between 0 and 255, for ADD
9123 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009124 if (CVal >= 0 && CVal <= 255)
9125 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009126 } else if (Subtarget->isThumb2()) {
9127 // A constant that can be used as an immediate value in a
9128 // data-processing instruction.
9129 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9130 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009131 } else {
9132 // A constant that can be used as an immediate value in a
9133 // data-processing instruction.
9134 if (ARM_AM::getSOImmVal(CVal) != -1)
9135 break;
9136 }
9137 return;
9138
9139 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009140 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009141 // This must be a constant between -255 and -1, for negated ADD
9142 // immediates. This can be used in GCC with an "n" modifier that
9143 // prints the negated value, for use with SUB instructions. It is
9144 // not useful otherwise but is implemented for compatibility.
9145 if (CVal >= -255 && CVal <= -1)
9146 break;
9147 } else {
9148 // This must be a constant between -4095 and 4095. It is not clear
9149 // what this constraint is intended for. Implemented for
9150 // compatibility with GCC.
9151 if (CVal >= -4095 && CVal <= 4095)
9152 break;
9153 }
9154 return;
9155
9156 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009157 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009158 // A 32-bit value where only one byte has a nonzero value. Exclude
9159 // zero to match GCC. This constraint is used by GCC internally for
9160 // constants that can be loaded with a move/shift combination.
9161 // It is not useful otherwise but is implemented for compatibility.
9162 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9163 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009164 } else if (Subtarget->isThumb2()) {
9165 // A constant whose bitwise inverse can be used as an immediate
9166 // value in a data-processing instruction. This can be used in GCC
9167 // with a "B" modifier that prints the inverted value, for use with
9168 // BIC and MVN instructions. It is not useful otherwise but is
9169 // implemented for compatibility.
9170 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9171 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009172 } else {
9173 // A constant whose bitwise inverse can be used as an immediate
9174 // value in a data-processing instruction. This can be used in GCC
9175 // with a "B" modifier that prints the inverted value, for use with
9176 // BIC and MVN instructions. It is not useful otherwise but is
9177 // implemented for compatibility.
9178 if (ARM_AM::getSOImmVal(~CVal) != -1)
9179 break;
9180 }
9181 return;
9182
9183 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009184 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009185 // This must be a constant between -7 and 7,
9186 // for 3-operand ADD/SUB immediate instructions.
9187 if (CVal >= -7 && CVal < 7)
9188 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009189 } else if (Subtarget->isThumb2()) {
9190 // A constant whose negation can be used as an immediate value in a
9191 // data-processing instruction. This can be used in GCC with an "n"
9192 // modifier that prints the negated value, for use with SUB
9193 // instructions. It is not useful otherwise but is implemented for
9194 // compatibility.
9195 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9196 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009197 } else {
9198 // A constant whose negation can be used as an immediate value in a
9199 // data-processing instruction. This can be used in GCC with an "n"
9200 // modifier that prints the negated value, for use with SUB
9201 // instructions. It is not useful otherwise but is implemented for
9202 // compatibility.
9203 if (ARM_AM::getSOImmVal(-CVal) != -1)
9204 break;
9205 }
9206 return;
9207
9208 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009209 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009210 // This must be a multiple of 4 between 0 and 1020, for
9211 // ADD sp + immediate.
9212 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9213 break;
9214 } else {
9215 // A power of two or a constant between 0 and 32. This is used in
9216 // GCC for the shift amount on shifted register operands, but it is
9217 // useful in general for any shift amounts.
9218 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9219 break;
9220 }
9221 return;
9222
9223 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009224 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009225 // This must be a constant between 0 and 31, for shift amounts.
9226 if (CVal >= 0 && CVal <= 31)
9227 break;
9228 }
9229 return;
9230
9231 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009232 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009233 // This must be a multiple of 4 between -508 and 508, for
9234 // ADD/SUB sp = sp + immediate.
9235 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9236 break;
9237 }
9238 return;
9239 }
9240 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9241 break;
9242 }
9243
9244 if (Result.getNode()) {
9245 Ops.push_back(Result);
9246 return;
9247 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009248 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009249}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009250
9251bool
9252ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9253 // The ARM target isn't yet aware of offsets.
9254 return false;
9255}
Evan Cheng39382422009-10-28 01:44:26 +00009256
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009257bool ARM::isBitFieldInvertedMask(unsigned v) {
9258 if (v == 0xffffffff)
9259 return 0;
9260 // there can be 1's on either or both "outsides", all the "inside"
9261 // bits must be 0's
9262 unsigned int lsb = 0, msb = 31;
9263 while (v & (1 << msb)) --msb;
9264 while (v & (1 << lsb)) ++lsb;
9265 for (unsigned int i = lsb; i <= msb; ++i) {
9266 if (v & (1 << i))
9267 return 0;
9268 }
9269 return 1;
9270}
9271
Evan Cheng39382422009-10-28 01:44:26 +00009272/// isFPImmLegal - Returns true if the target can instruction select the
9273/// specified FP immediate natively. If false, the legalizer will
9274/// materialize the FP immediate as a load from a constant pool.
9275bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9276 if (!Subtarget->hasVFP3())
9277 return false;
9278 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009279 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009280 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009281 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009282 return false;
9283}
Bob Wilson65ffec42010-09-21 17:56:22 +00009284
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009285/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009286/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9287/// specified in the intrinsic calls.
9288bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9289 const CallInst &I,
9290 unsigned Intrinsic) const {
9291 switch (Intrinsic) {
9292 case Intrinsic::arm_neon_vld1:
9293 case Intrinsic::arm_neon_vld2:
9294 case Intrinsic::arm_neon_vld3:
9295 case Intrinsic::arm_neon_vld4:
9296 case Intrinsic::arm_neon_vld2lane:
9297 case Intrinsic::arm_neon_vld3lane:
9298 case Intrinsic::arm_neon_vld4lane: {
9299 Info.opc = ISD::INTRINSIC_W_CHAIN;
9300 // Conservatively set memVT to the entire set of vectors loaded.
9301 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9302 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9303 Info.ptrVal = I.getArgOperand(0);
9304 Info.offset = 0;
9305 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9306 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9307 Info.vol = false; // volatile loads with NEON intrinsics not supported
9308 Info.readMem = true;
9309 Info.writeMem = false;
9310 return true;
9311 }
9312 case Intrinsic::arm_neon_vst1:
9313 case Intrinsic::arm_neon_vst2:
9314 case Intrinsic::arm_neon_vst3:
9315 case Intrinsic::arm_neon_vst4:
9316 case Intrinsic::arm_neon_vst2lane:
9317 case Intrinsic::arm_neon_vst3lane:
9318 case Intrinsic::arm_neon_vst4lane: {
9319 Info.opc = ISD::INTRINSIC_VOID;
9320 // Conservatively set memVT to the entire set of vectors stored.
9321 unsigned NumElts = 0;
9322 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009323 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009324 if (!ArgTy->isVectorTy())
9325 break;
9326 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9327 }
9328 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9329 Info.ptrVal = I.getArgOperand(0);
9330 Info.offset = 0;
9331 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9332 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9333 Info.vol = false; // volatile stores with NEON intrinsics not supported
9334 Info.readMem = false;
9335 Info.writeMem = true;
9336 return true;
9337 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009338 case Intrinsic::arm_strexd: {
9339 Info.opc = ISD::INTRINSIC_W_CHAIN;
9340 Info.memVT = MVT::i64;
9341 Info.ptrVal = I.getArgOperand(2);
9342 Info.offset = 0;
9343 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009344 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009345 Info.readMem = false;
9346 Info.writeMem = true;
9347 return true;
9348 }
9349 case Intrinsic::arm_ldrexd: {
9350 Info.opc = ISD::INTRINSIC_W_CHAIN;
9351 Info.memVT = MVT::i64;
9352 Info.ptrVal = I.getArgOperand(0);
9353 Info.offset = 0;
9354 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009355 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009356 Info.readMem = true;
9357 Info.writeMem = false;
9358 return true;
9359 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009360 default:
9361 break;
9362 }
9363
9364 return false;
9365}