blob: 190ca076dae53bc784062a3ba39834e3a72f2868 [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");
Manman Ren763a75d2012-06-01 02:44:42 +000055STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesen51e28e62010-06-03 21:09:53 +000056
Bob Wilson703af3a2010-08-13 22:43:33 +000057// This option should go away when tail calls fully work.
58static cl::opt<bool>
59EnableARMTailCalls("arm-tail-calls", cl::Hidden,
60 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
61 cl::init(false));
62
Eric Christopher836c6242010-12-15 23:47:29 +000063cl::opt<bool>
Jim Grosbache7b52522010-04-14 22:28:31 +000064EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng515fe3a2010-07-08 02:08:50 +000065 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbache7b52522010-04-14 22:28:31 +000066 cl::init(false));
67
Evan Cheng46df4eb2010-06-16 07:35:02 +000068static cl::opt<bool>
69ARMInterworking("arm-interworking", cl::Hidden,
70 cl::desc("Enable / disable ARM interworking (for debugging only)"),
71 cl::init(true));
72
Benjamin Kramer0861f572011-11-26 23:01:57 +000073namespace {
Cameron Zwaricha86686e2011-06-10 20:59:24 +000074 class ARMCCState : public CCState {
75 public:
76 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
77 const TargetMachine &TM, SmallVector<CCValAssign, 16> &locs,
78 LLVMContext &C, ParmContext PC)
79 : CCState(CC, isVarArg, MF, TM, locs, C) {
80 assert(((PC == Call) || (PC == Prologue)) &&
81 "ARMCCState users must specify whether their context is call"
82 "or prologue generation.");
83 CallOrPrologue = PC;
84 }
85 };
86}
87
Stuart Hastingsc7315872011-04-20 16:47:52 +000088// The APCS parameter registers.
Craig Topperc5eaae42012-03-11 07:57:25 +000089static const uint16_t GPRArgRegs[] = {
Stuart Hastingsc7315872011-04-20 16:47:52 +000090 ARM::R0, ARM::R1, ARM::R2, ARM::R3
91};
92
Craig Topper0faf46c2012-08-12 03:16:37 +000093void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
94 MVT PromotedBitwiseVT) {
Bob Wilson5bafff32009-06-22 23:27:02 +000095 if (VT != PromotedLdStVT) {
Craig Topper0faf46c2012-08-12 03:16:37 +000096 setOperationAction(ISD::LOAD, VT, Promote);
97 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson5bafff32009-06-22 23:27:02 +000098
Craig Topper0faf46c2012-08-12 03:16:37 +000099 setOperationAction(ISD::STORE, VT, Promote);
100 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson5bafff32009-06-22 23:27:02 +0000101 }
102
Craig Topper0faf46c2012-08-12 03:16:37 +0000103 MVT ElemTy = VT.getVectorElementType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper0faf46c2012-08-12 03:16:37 +0000105 setOperationAction(ISD::SETCC, VT, Custom);
106 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
107 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000108 if (ElemTy == MVT::i32) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000109 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
110 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
111 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
112 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman14e809c2011-11-09 23:36:02 +0000113 } else {
Craig Topper0faf46c2012-08-12 03:16:37 +0000114 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
115 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
116 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
117 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson0696fdf2009-09-16 20:20:44 +0000118 }
Craig Topper0faf46c2012-08-12 03:16:37 +0000119 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
120 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
121 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
122 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
123 setOperationAction(ISD::SELECT, VT, Expand);
124 setOperationAction(ISD::SELECT_CC, VT, Expand);
125 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000126 if (VT.isInteger()) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000127 setOperationAction(ISD::SHL, VT, Custom);
128 setOperationAction(ISD::SRA, VT, Custom);
129 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson5bafff32009-06-22 23:27:02 +0000130 }
131
132 // Promote all bit-wise operations.
133 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper0faf46c2012-08-12 03:16:37 +0000134 setOperationAction(ISD::AND, VT, Promote);
135 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
136 setOperationAction(ISD::OR, VT, Promote);
137 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
138 setOperationAction(ISD::XOR, VT, Promote);
139 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson5bafff32009-06-22 23:27:02 +0000140 }
Bob Wilson16330762009-09-16 00:17:28 +0000141
142 // Neon does not support vector divide/remainder operations.
Craig Topper0faf46c2012-08-12 03:16:37 +0000143 setOperationAction(ISD::SDIV, VT, Expand);
144 setOperationAction(ISD::UDIV, VT, Expand);
145 setOperationAction(ISD::FDIV, VT, Expand);
146 setOperationAction(ISD::SREM, VT, Expand);
147 setOperationAction(ISD::UREM, VT, Expand);
148 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson5bafff32009-06-22 23:27:02 +0000149}
150
Craig Topper0faf46c2012-08-12 03:16:37 +0000151void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000152 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000154}
155
Craig Topper0faf46c2012-08-12 03:16:37 +0000156void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Craig Topper420761a2012-04-20 07:30:17 +0000157 addRegisterClass(VT, &ARM::QPRRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson5bafff32009-06-22 23:27:02 +0000159}
160
Chris Lattnerf0144122009-07-28 03:13:23 +0000161static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
162 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +0000163 return new TargetLoweringObjectFileMachO();
Bill Wendling94a1c632010-03-09 02:46:12 +0000164
Chris Lattner80ec2792009-08-02 00:34:36 +0000165 return new ARMElfTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +0000166}
167
Evan Chenga8e29892007-01-19 07:51:42 +0000168ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Chenge7e0d622009-11-06 22:24:13 +0000169 : TargetLowering(TM, createTLOF(TM)) {
Evan Chenga8e29892007-01-19 07:51:42 +0000170 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Cheng31446872010-07-23 22:39:59 +0000171 RegInfo = TM.getRegisterInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000172 Itins = TM.getInstrItineraryData();
Evan Chenga8e29892007-01-19 07:51:42 +0000173
Duncan Sands28b77e92011-09-06 19:07:46 +0000174 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
175
Evan Chengb1df8f22007-04-27 08:15:43 +0000176 if (Subtarget->isTargetDarwin()) {
Evan Chengb1df8f22007-04-27 08:15:43 +0000177 // Uses VFP for Thumb libfuncs if available.
178 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
179 // Single-precision floating-point arithmetic.
180 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
181 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
182 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
183 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000184
Evan Chengb1df8f22007-04-27 08:15:43 +0000185 // Double-precision floating-point arithmetic.
186 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
187 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
188 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
189 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng193f8502007-01-31 09:30:58 +0000190
Evan Chengb1df8f22007-04-27 08:15:43 +0000191 // Single-precision comparisons.
192 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
193 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
194 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
195 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
196 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
197 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
198 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
199 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000200
Evan Chengb1df8f22007-04-27 08:15:43 +0000201 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng193f8502007-01-31 09:30:58 +0000209
Evan Chengb1df8f22007-04-27 08:15:43 +0000210 // Double-precision comparisons.
211 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
212 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
213 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
214 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
215 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
216 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
217 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
218 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000219
Evan Chengb1df8f22007-04-27 08:15:43 +0000220 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Chenga8e29892007-01-19 07:51:42 +0000228
Evan Chengb1df8f22007-04-27 08:15:43 +0000229 // Floating-point to integer conversions.
230 // i64 conversions are done via library routines even when generating VFP
231 // instructions, so use the same ones.
232 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
233 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
234 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
235 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Chenga8e29892007-01-19 07:51:42 +0000236
Evan Chengb1df8f22007-04-27 08:15:43 +0000237 // Conversions between floating types.
238 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
239 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
240
241 // Integer to floating-point conversions.
242 // i64 conversions are done via library routines even when generating VFP
243 // instructions, so use the same ones.
Bob Wilson2a14c522009-03-20 23:16:43 +0000244 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
245 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengb1df8f22007-04-27 08:15:43 +0000246 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
247 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
248 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
249 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
250 }
Evan Chenga8e29892007-01-19 07:51:42 +0000251 }
252
Bob Wilson2f954612009-05-22 17:38:41 +0000253 // These libcalls are not available in 32-bit.
254 setLibcallName(RTLIB::SHL_I128, 0);
255 setLibcallName(RTLIB::SRL_I128, 0);
256 setLibcallName(RTLIB::SRA_I128, 0);
257
Evan Cheng07043272012-02-21 20:46:00 +0000258 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000259 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000260 // RTABI chapter 4.1.2, Table 2
261 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
262 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
263 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
264 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
265 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
266 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
267 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
268 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
269
270 // Double-precision floating-point comparison helper functions
271 // RTABI chapter 4.1.2, Table 3
272 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
273 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
274 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
275 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
276 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
277 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
278 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
279 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
280 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
281 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
282 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
283 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
284 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
285 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
286 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
287 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
288 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
292 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
296
297 // Single-precision floating-point arithmetic helper functions
298 // RTABI chapter 4.1.2, Table 4
299 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
300 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
301 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
302 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
303 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
304 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
305 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
306 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
307
308 // Single-precision floating-point comparison helper functions
309 // RTABI chapter 4.1.2, Table 5
310 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
311 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
312 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
313 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
314 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
315 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
316 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
317 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
318 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
319 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
320 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
321 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
322 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
323 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
324 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
325 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
326 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
327 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
329 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
334
335 // Floating-point to integer conversions.
336 // RTABI chapter 4.1.2, Table 6
337 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
338 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
339 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
340 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
341 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
342 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
343 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
344 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
345 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
346 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
348 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
349 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
353
354 // Conversions between floating types.
355 // RTABI chapter 4.1.2, Table 7
356 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
357 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
358 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000359 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000360
361 // Integer to floating-point conversions.
362 // RTABI chapter 4.1.2, Table 8
363 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
364 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
365 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
366 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
367 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
368 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
369 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
370 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
371 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
372 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
379
380 // Long long helper functions
381 // RTABI chapter 4.2, Table 9
382 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000383 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
384 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
385 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
386 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
387 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
392
393 // Integer division functions
394 // RTABI chapter 4.3.1
395 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
396 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
397 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000398 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000399 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
400 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
401 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000402 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000403 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
404 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
405 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000406 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov4f922f22010-09-28 21:39:26 +0000407 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
408 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000409 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000410 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000411
412 // Memory operations
413 // RTABI chapter 4.3.4
414 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
415 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
416 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikov6edd5882012-01-29 09:11:50 +0000417 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
418 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
419 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikov72977a42009-08-14 20:10:52 +0000420 }
421
Bob Wilson2fef4572011-10-07 16:59:21 +0000422 // Use divmod compiler-rt calls for iOS 5.0 and later.
423 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
424 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
425 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
426 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
427 }
428
David Goodwinf1daf7d2009-07-08 23:10:31 +0000429 if (Subtarget->isThumb1Only())
Craig Topper420761a2012-04-20 07:30:17 +0000430 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbach30eae3c2009-04-07 20:34:09 +0000431 else
Craig Topper420761a2012-04-20 07:30:17 +0000432 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000433 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
434 !Subtarget->isThumb1Only()) {
Craig Topper420761a2012-04-20 07:30:17 +0000435 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbachfcba5e62010-08-11 15:44:15 +0000436 if (!Subtarget->isFPOnlySP())
Craig Topper420761a2012-04-20 07:30:17 +0000437 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000438
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000440 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000441
Eli Friedman9f1f26a2011-11-08 01:43:53 +0000442 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
443 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
444 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
445 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
446 setTruncStoreAction((MVT::SimpleValueType)VT,
447 (MVT::SimpleValueType)InnerVT, Expand);
448 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
449 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
450 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 }
452
Lang Hames45b5f882012-03-15 18:49:02 +0000453 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
454
Bob Wilson5bafff32009-06-22 23:27:02 +0000455 if (Subtarget->hasNEON()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 addDRTypeForNEON(MVT::v2f32);
457 addDRTypeForNEON(MVT::v8i8);
458 addDRTypeForNEON(MVT::v4i16);
459 addDRTypeForNEON(MVT::v2i32);
460 addDRTypeForNEON(MVT::v1i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000461
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 addQRTypeForNEON(MVT::v4f32);
463 addQRTypeForNEON(MVT::v2f64);
464 addQRTypeForNEON(MVT::v16i8);
465 addQRTypeForNEON(MVT::v8i16);
466 addQRTypeForNEON(MVT::v4i32);
467 addQRTypeForNEON(MVT::v2i64);
Bob Wilson5bafff32009-06-22 23:27:02 +0000468
Bob Wilson74dc72e2009-09-15 23:55:57 +0000469 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
470 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000471 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
472 // supported for v4f32.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000473 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
474 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
475 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000476 // FIXME: Code duplication: FDIV and FREM are expanded always, see
477 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000478 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
479 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000480 // FIXME: Create unittest.
481 // In another words, find a way when "copysign" appears in DAG with vector
482 // operands.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000483 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000484 // FIXME: Code duplication: SETCC has custom operation action, see
485 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sands28b77e92011-09-06 19:07:46 +0000486 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000487 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000488 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
489 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
490 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
491 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
492 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
493 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
494 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
495 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
496 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
497 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
498 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
499 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000500 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson74dc72e2009-09-15 23:55:57 +0000501 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
502 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
503 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
504 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
505 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Lang Hamesc0a9f822012-03-29 21:56:11 +0000506
Stepan Dyatkovskiy3e0dc062011-12-11 14:35:48 +0000507 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
508 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
509 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
510 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
511 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
512 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
513 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
514 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
515 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
516 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Bob Wilson74dc72e2009-09-15 23:55:57 +0000517
Bob Wilson642b3292009-09-16 00:32:15 +0000518 // Neon does not support some operations on v1i64 and v2i64 types.
519 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilsond0b69cf2010-09-01 23:50:19 +0000520 // Custom handling for some quad-vector types to detect VMULL.
521 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
522 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
523 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begeman7973f352011-02-11 20:53:29 +0000524 // Custom handling for some vector types to avoid expensive expansions
525 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
526 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
527 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
528 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000529 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
530 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich3007d332011-03-29 21:41:55 +0000531 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy873fd5f2012-02-20 09:24:05 +0000532 // a destination type that is wider than the source, and nor does
533 // it have a FP_TO_[SU]INT instruction with a narrower destination than
534 // source.
Cameron Zwarich3007d332011-03-29 21:41:55 +0000535 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
536 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy873fd5f2012-02-20 09:24:05 +0000537 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
538 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson642b3292009-09-16 00:32:15 +0000539
Bob Wilson1c3ef902011-02-07 17:43:21 +0000540 setTargetDAGCombine(ISD::INTRINSIC_VOID);
541 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson5bafff32009-06-22 23:27:02 +0000542 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
543 setTargetDAGCombine(ISD::SHL);
544 setTargetDAGCombine(ISD::SRL);
545 setTargetDAGCombine(ISD::SRA);
546 setTargetDAGCombine(ISD::SIGN_EXTEND);
547 setTargetDAGCombine(ISD::ZERO_EXTEND);
548 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilson9f6c4c12010-02-18 06:05:53 +0000549 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilson75f02882010-09-17 22:59:05 +0000550 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonf20700c2010-10-27 20:38:28 +0000551 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson31600902010-12-21 06:43:19 +0000552 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
553 setTargetDAGCombine(ISD::STORE);
Chad Rosieref01edf2011-06-24 19:23:04 +0000554 setTargetDAGCombine(ISD::FP_TO_SINT);
555 setTargetDAGCombine(ISD::FP_TO_UINT);
556 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem004a24b2011-10-15 20:03:12 +0000557
James Molloy873fd5f2012-02-20 09:24:05 +0000558 // It is legal to extload from v4i8 to v4i16 or v4i32.
559 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
560 MVT::v4i16, MVT::v2i16,
561 MVT::v2i32};
562 for (unsigned i = 0; i < 6; ++i) {
563 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
564 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
565 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
566 }
Bob Wilson5bafff32009-06-22 23:27:02 +0000567 }
568
Evan Cheng9f8cbd12007-05-18 00:19:34 +0000569 computeRegisterProperties();
Evan Chenga8e29892007-01-19 07:51:42 +0000570
571 // ARM does not have f32 extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000573
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000574 // ARM does not have i1 sign extending load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000575 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sandsf9c98e62008-01-23 20:39:46 +0000576
Evan Chenga8e29892007-01-19 07:51:42 +0000577 // ARM supports all 4 flavors of integer indexed load / store.
Evan Chenge88d5ce2009-07-02 07:28:31 +0000578 if (!Subtarget->isThumb1Only()) {
579 for (unsigned im = (unsigned)ISD::PRE_INC;
580 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000581 setIndexedLoadAction(im, MVT::i1, Legal);
582 setIndexedLoadAction(im, MVT::i8, Legal);
583 setIndexedLoadAction(im, MVT::i16, Legal);
584 setIndexedLoadAction(im, MVT::i32, Legal);
585 setIndexedStoreAction(im, MVT::i1, Legal);
586 setIndexedStoreAction(im, MVT::i8, Legal);
587 setIndexedStoreAction(im, MVT::i16, Legal);
588 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Chenge88d5ce2009-07-02 07:28:31 +0000589 }
Evan Chenga8e29892007-01-19 07:51:42 +0000590 }
591
592 // i64 operation support.
Eric Christopher2cc40132011-04-19 18:49:19 +0000593 setOperationAction(ISD::MUL, MVT::i64, Expand);
594 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Cheng5b9fcd12009-07-07 01:17:28 +0000595 if (Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
597 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000598 }
Jim Grosbacha7603982011-07-01 21:12:19 +0000599 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
600 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopher2cc40132011-04-19 18:49:19 +0000601 setOperationAction(ISD::MULHS, MVT::i32, Expand);
602
Jim Grosbachc2b879f2009-10-31 19:38:01 +0000603 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbachb4a976c2009-10-31 21:00:56 +0000604 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +0000605 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 setOperationAction(ISD::SRL, MVT::i64, Custom);
607 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000608
Evan Cheng342e3162011-08-30 01:34:54 +0000609 if (!Subtarget->isThumb1Only()) {
610 // FIXME: We should do this for Thumb1 as well.
611 setOperationAction(ISD::ADDC, MVT::i32, Custom);
612 setOperationAction(ISD::ADDE, MVT::i32, Custom);
613 setOperationAction(ISD::SUBC, MVT::i32, Custom);
614 setOperationAction(ISD::SUBE, MVT::i32, Custom);
615 }
616
Evan Chenga8e29892007-01-19 07:51:42 +0000617 // ARM does not have ROTL.
Owen Anderson825b72b2009-08-11 20:47:22 +0000618 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach3482c802010-01-18 19:58:49 +0000619 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwin24062ac2009-06-26 20:47:43 +0000621 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000623
Chandler Carruth63974b22011-12-13 01:56:10 +0000624 // These just redirect to CTTZ and CTLZ on ARM.
625 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
626 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
627
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000628 // Only ARMv6 has BSWAP.
629 if (!Subtarget->hasV6Ops())
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio368f20f2007-03-16 22:54:16 +0000631
Evan Chenga8e29892007-01-19 07:51:42 +0000632 // These are expanded into libcalls.
Evan Cheng1f190c82010-11-19 06:28:11 +0000633 if (!Subtarget->hasDivide() || !Subtarget->isThumb2()) {
Jim Grosbachb1dc3932010-05-05 20:44:35 +0000634 // v7M has a hardware divider
635 setOperationAction(ISD::SDIV, MVT::i32, Expand);
636 setOperationAction(ISD::UDIV, MVT::i32, Expand);
637 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000638 setOperationAction(ISD::SREM, MVT::i32, Expand);
639 setOperationAction(ISD::UREM, MVT::i32, Expand);
640 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
641 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000642
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
644 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
645 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
646 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilsonddb16df2009-10-30 05:45:42 +0000647 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000648
Evan Cheng4da0c7c2011-04-08 21:37:21 +0000649 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Chengfb3611d2010-05-11 07:26:32 +0000650
Evan Chenga8e29892007-01-19 07:51:42 +0000651 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::VASTART, MVT::Other, Custom);
653 setOperationAction(ISD::VAARG, MVT::Other, Expand);
654 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
655 setOperationAction(ISD::VAEND, MVT::Other, Expand);
656 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
657 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendlingbdf9db62012-02-13 23:47:16 +0000658
659 if (!Subtarget->isTargetDarwin()) {
660 // Non-Darwin platforms may return values in these registers via the
661 // personality function.
662 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
663 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
664 setExceptionPointerRegister(ARM::R0);
665 setExceptionSelectorRegister(ARM::R1);
666 }
Anton Korobeynikov5899a602011-01-24 22:38:45 +0000667
Evan Cheng3a1588a2010-04-15 22:20:34 +0000668 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng11db0682010-08-11 06:22:01 +0000669 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
670 // the default expansion.
Eli Friedman4db5aca2011-08-29 18:23:02 +0000671 // FIXME: This should be checking for v6k, not just v6.
Evan Cheng11db0682010-08-11 06:22:01 +0000672 if (Subtarget->hasDataBarrier() ||
Bob Wilson54f92562010-11-09 22:50:44 +0000673 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
Jim Grosbach68741be2010-06-18 22:35:32 +0000674 // membarrier needs custom lowering; the rest are legal and handled
675 // normally.
676 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000677 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Eli Friedman2bdffe42011-08-31 00:31:29 +0000678 // Custom lowering for 64-bit ops
679 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
680 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
681 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
682 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
683 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
684 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Eli Friedman4d3f3292011-08-31 17:52:22 +0000685 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Eli Friedman26689ac2011-08-03 21:06:02 +0000686 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
687 setInsertFencesForAtomic(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000688 } else {
689 // Set them all for expansion, which will force libcalls.
690 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000691 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000692 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbachef6eb9c2010-06-18 23:03:10 +0000693 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000694 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000695 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000696 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000697 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000698 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach68741be2010-06-18 22:35:32 +0000699 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000700 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000701 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000702 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachf7da8822011-04-26 19:44:18 +0000703 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedman7cc15662011-09-15 22:18:49 +0000704 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
705 // Unordered/Monotonic case.
706 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
707 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach5def57a2010-06-23 16:08:49 +0000708 // Since the libcalls include locking, fold in the fences
709 setShouldFoldAtomicFences(true);
Jim Grosbach68741be2010-06-18 22:35:32 +0000710 }
Evan Chenga8e29892007-01-19 07:51:42 +0000711
Evan Cheng416941d2010-11-04 05:19:35 +0000712 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Chengbc7deb02010-11-03 05:14:24 +0000713
Eli Friedmana2c6f452010-06-26 04:36:50 +0000714 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
715 if (!Subtarget->hasV6Ops()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
717 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000718 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Chenga8e29892007-01-19 07:51:42 +0000720
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000721 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
722 !Subtarget->isThumb1Only()) {
Bob Wilsoncb9a6aa2010-01-19 22:56:26 +0000723 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
724 // iff target supports vfp2.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000725 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemand1fb5832010-08-03 21:31:55 +0000726 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
727 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000728
729 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbache97f9682010-07-07 00:07:57 +0000731 if (Subtarget->isTargetDarwin()) {
732 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
733 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall5f8fd542011-05-29 19:50:32 +0000734 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbache97f9682010-07-07 00:07:57 +0000735 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +0000736
Owen Anderson825b72b2009-08-11 20:47:22 +0000737 setOperationAction(ISD::SETCC, MVT::i32, Expand);
738 setOperationAction(ISD::SETCC, MVT::f32, Expand);
739 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendlingde2b1512010-08-11 08:43:16 +0000740 setOperationAction(ISD::SELECT, MVT::i32, Custom);
741 setOperationAction(ISD::SELECT, MVT::f32, Custom);
742 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
744 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
745 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000746
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
748 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
749 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
750 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
751 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Chenga8e29892007-01-19 07:51:42 +0000752
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000753 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 setOperationAction(ISD::FSIN, MVT::f64, Expand);
755 setOperationAction(ISD::FSIN, MVT::f32, Expand);
756 setOperationAction(ISD::FCOS, MVT::f32, Expand);
757 setOperationAction(ISD::FCOS, MVT::f64, Expand);
758 setOperationAction(ISD::FREM, MVT::f64, Expand);
759 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000760 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
761 !Subtarget->isThumb1Only()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
763 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng110cf482008-04-01 01:50:16 +0000764 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 setOperationAction(ISD::FPOW, MVT::f64, Expand);
766 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000767
Evan Cheng3aef2ff2012-04-10 21:40:28 +0000768 if (!Subtarget->hasVFP4()) {
769 setOperationAction(ISD::FMA, MVT::f64, Expand);
770 setOperationAction(ISD::FMA, MVT::f32, Expand);
771 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000772
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000773 // Various VFP goodness
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000774 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilson76a312b2010-03-19 22:51:32 +0000775 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
776 if (Subtarget->hasVFP2()) {
777 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
778 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
779 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
780 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
781 }
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000782 // Special handling for half-precision FP.
Anton Korobeynikovf0d50072010-03-18 22:35:37 +0000783 if (!Subtarget->hasFP16()) {
784 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
785 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovbec3dd22010-03-14 18:42:31 +0000786 }
Evan Cheng110cf482008-04-01 01:50:16 +0000787 }
Evan Chenga8e29892007-01-19 07:51:42 +0000788
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +0000789 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbache5165492009-11-09 00:11:35 +0000790 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattnerd1980a52009-03-12 06:52:53 +0000791 setTargetDAGCombine(ISD::ADD);
792 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikova9790d72010-05-15 18:16:59 +0000793 setTargetDAGCombine(ISD::MUL);
Bob Wilson2dc4f542009-03-20 22:42:55 +0000794
Evan Chengc892aeb2012-02-23 01:19:06 +0000795 if (Subtarget->hasV6T2Ops() || Subtarget->hasNEON()) {
Owen Anderson080c0922010-11-05 19:27:46 +0000796 setTargetDAGCombine(ISD::AND);
Evan Chengc892aeb2012-02-23 01:19:06 +0000797 setTargetDAGCombine(ISD::OR);
798 setTargetDAGCombine(ISD::XOR);
799 }
Jim Grosbach469bbdb2010-07-16 23:05:05 +0000800
Evan Cheng5fb468a2012-02-23 02:58:19 +0000801 if (Subtarget->hasV6Ops())
802 setTargetDAGCombine(ISD::SRL);
803
Evan Chenga8e29892007-01-19 07:51:42 +0000804 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng1cc39842010-05-20 23:26:43 +0000805
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000806 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
807 !Subtarget->hasVFP2())
Evan Chengf7d87ee2010-05-21 00:43:17 +0000808 setSchedulingPreference(Sched::RegPressure);
809 else
810 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen8dd86c12007-05-17 21:31:21 +0000811
Evan Cheng05219282011-01-06 06:52:41 +0000812 //// temporary - rewrite interface to use type
813 maxStoresPerMemcpy = maxStoresPerMemcpyOptSize = 1;
Lang Hames75757f92011-10-26 20:56:52 +0000814 maxStoresPerMemset = 16;
815 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengf6799392010-06-26 01:52:05 +0000816
Rafael Espindolacbeeae22010-07-11 04:01:49 +0000817 // On ARM arguments smaller than 4 bytes are extended, so all arguments
818 // are at least 4 bytes aligned.
819 setMinStackArgumentAlignment(4);
820
Evan Chengfff606d2010-09-24 19:07:23 +0000821 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000822
Benjamin Krameraaf723d2012-05-05 12:49:14 +0000823 // Prefer likely predicted branches to selects on out-of-order cores.
824 predictableSelectIsExpensive = Subtarget->isCortexA9();
825
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000826 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000827}
828
Andrew Trick32cec0a2011-01-19 02:35:27 +0000829// FIXME: It might make sense to define the representative register class as the
830// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
831// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
832// SPR's representative would be DPR_VFP2. This should work well if register
833// pressure tracking were modified such that a register use would increment the
834// pressure of the register class's representative and all of it's super
835// classes' representatives transitively. We have not implemented this because
836// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000837// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick32cec0a2011-01-19 02:35:27 +0000838// and extractions.
Evan Cheng4f6b4672010-07-21 06:09:07 +0000839std::pair<const TargetRegisterClass*, uint8_t>
840ARMTargetLowering::findRepresentativeClass(EVT VT) const{
841 const TargetRegisterClass *RRC = 0;
842 uint8_t Cost = 1;
843 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengd70f57b2010-07-19 22:15:08 +0000844 default:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000845 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng4a863e22010-07-21 23:53:58 +0000846 // Use DPR as representative register class for all floating point
847 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
848 // the cost is 1 for both f32 and f64.
849 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Cheng4f6b4672010-07-21 06:09:07 +0000850 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topper420761a2012-04-20 07:30:17 +0000851 RRC = &ARM::DPRRegClass;
Andrew Trick32cec0a2011-01-19 02:35:27 +0000852 // When NEON is used for SP, only half of the register file is available
853 // because operations that define both SP and DP results will be constrained
854 // to the VFP2 class (D0-D15). We currently model this constraint prior to
855 // coalescing by double-counting the SP regs. See the FIXME above.
856 if (Subtarget->useNEONForSinglePrecisionFP())
857 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000858 break;
859 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
860 case MVT::v4f32: case MVT::v2f64:
Craig Topper420761a2012-04-20 07:30:17 +0000861 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000862 Cost = 2;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000863 break;
864 case MVT::v4i64:
Craig Topper420761a2012-04-20 07:30:17 +0000865 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000866 Cost = 4;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000867 break;
868 case MVT::v8i64:
Craig Topper420761a2012-04-20 07:30:17 +0000869 RRC = &ARM::DPRRegClass;
Evan Cheng4a863e22010-07-21 23:53:58 +0000870 Cost = 8;
Evan Cheng4f6b4672010-07-21 06:09:07 +0000871 break;
Evan Chengd70f57b2010-07-19 22:15:08 +0000872 }
Evan Cheng4f6b4672010-07-21 06:09:07 +0000873 return std::make_pair(RRC, Cost);
Evan Chengd70f57b2010-07-19 22:15:08 +0000874}
875
Evan Chenga8e29892007-01-19 07:51:42 +0000876const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
877 switch (Opcode) {
878 default: return 0;
879 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Cheng53519f02011-01-21 18:55:51 +0000880 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000881 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Chenga8e29892007-01-19 07:51:42 +0000882 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
883 case ARMISD::CALL: return "ARMISD::CALL";
Evan Cheng277f0742007-06-19 21:05:09 +0000884 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Chenga8e29892007-01-19 07:51:42 +0000885 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
886 case ARMISD::tCALL: return "ARMISD::tCALL";
887 case ARMISD::BRCOND: return "ARMISD::BRCOND";
888 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Cheng5657c012009-07-29 02:18:14 +0000889 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Chenga8e29892007-01-19 07:51:42 +0000890 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
891 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
892 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendlingad5c8802012-06-11 08:07:26 +0000893 case ARMISD::CMN: return "ARMISD::CMN";
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 *
Bob Wilsond49edb72012-08-03 04:06:28 +00001039ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1040 const TargetLibraryInfo *libInfo) const {
1041 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopherab695882010-07-21 22:26:11 +00001042}
1043
Anton Korobeynikovcec36f42010-07-24 21:52:08 +00001044/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1045/// be used for loads / stores from the global.
1046unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1047 return (Subtarget->isThumb1Only() ? 127 : 4095);
1048}
1049
Evan Cheng1cc39842010-05-20 23:26:43 +00001050Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengc10f5432010-05-28 23:25:23 +00001051 unsigned NumVals = N->getNumValues();
1052 if (!NumVals)
1053 return Sched::RegPressure;
1054
1055 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng1cc39842010-05-20 23:26:43 +00001056 EVT VT = N->getValueType(i);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001057 if (VT == MVT::Glue || VT == MVT::Other)
Evan Chengd7e473c2010-10-29 18:07:31 +00001058 continue;
Evan Cheng1cc39842010-05-20 23:26:43 +00001059 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman692c1d82011-10-24 17:55:11 +00001060 return Sched::ILP;
Evan Cheng1cc39842010-05-20 23:26:43 +00001061 }
Evan Chengc10f5432010-05-28 23:25:23 +00001062
1063 if (!N->isMachineOpcode())
1064 return Sched::RegPressure;
1065
1066 // Load are scheduled for latency even if there instruction itinerary
1067 // is not available.
1068 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Chenge837dea2011-06-28 19:10:37 +00001069 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Chengd7e473c2010-10-29 18:07:31 +00001070
Evan Chenge837dea2011-06-28 19:10:37 +00001071 if (MCID.getNumDefs() == 0)
Evan Chengd7e473c2010-10-29 18:07:31 +00001072 return Sched::RegPressure;
1073 if (!Itins->isEmpty() &&
Evan Chenge837dea2011-06-28 19:10:37 +00001074 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman692c1d82011-10-24 17:55:11 +00001075 return Sched::ILP;
Evan Chengc10f5432010-05-28 23:25:23 +00001076
Evan Cheng1cc39842010-05-20 23:26:43 +00001077 return Sched::RegPressure;
1078}
1079
Evan Chenga8e29892007-01-19 07:51:42 +00001080//===----------------------------------------------------------------------===//
1081// Lowering Code
1082//===----------------------------------------------------------------------===//
1083
Evan Chenga8e29892007-01-19 07:51:42 +00001084/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1085static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1086 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001087 default: llvm_unreachable("Unknown condition code!");
Evan Chenga8e29892007-01-19 07:51:42 +00001088 case ISD::SETNE: return ARMCC::NE;
1089 case ISD::SETEQ: return ARMCC::EQ;
1090 case ISD::SETGT: return ARMCC::GT;
1091 case ISD::SETGE: return ARMCC::GE;
1092 case ISD::SETLT: return ARMCC::LT;
1093 case ISD::SETLE: return ARMCC::LE;
1094 case ISD::SETUGT: return ARMCC::HI;
1095 case ISD::SETUGE: return ARMCC::HS;
1096 case ISD::SETULT: return ARMCC::LO;
1097 case ISD::SETULE: return ARMCC::LS;
1098 }
1099}
1100
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001101/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1102static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Chenga8e29892007-01-19 07:51:42 +00001103 ARMCC::CondCodes &CondCode2) {
Evan Chenga8e29892007-01-19 07:51:42 +00001104 CondCode2 = ARMCC::AL;
1105 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001106 default: llvm_unreachable("Unknown FP condition!");
Evan Chenga8e29892007-01-19 07:51:42 +00001107 case ISD::SETEQ:
1108 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1109 case ISD::SETGT:
1110 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1111 case ISD::SETGE:
1112 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1113 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00001114 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Chenga8e29892007-01-19 07:51:42 +00001115 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1116 case ISD::SETO: CondCode = ARMCC::VC; break;
1117 case ISD::SETUO: CondCode = ARMCC::VS; break;
1118 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1119 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1120 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1121 case ISD::SETLT:
1122 case ISD::SETULT: CondCode = ARMCC::LT; break;
1123 case ISD::SETLE:
1124 case ISD::SETULE: CondCode = ARMCC::LE; break;
1125 case ISD::SETNE:
1126 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1127 }
Evan Chenga8e29892007-01-19 07:51:42 +00001128}
1129
Bob Wilson1f595bb2009-04-17 19:07:39 +00001130//===----------------------------------------------------------------------===//
1131// Calling Convention Implementation
Bob Wilson1f595bb2009-04-17 19:07:39 +00001132//===----------------------------------------------------------------------===//
1133
1134#include "ARMGenCallingConv.inc"
1135
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001136/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1137/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001138CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001139 bool Return,
1140 bool isVarArg) const {
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001141 switch (CC) {
1142 default:
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001143 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001144 case CallingConv::Fast:
Evan Cheng5c2d4282010-10-23 02:19:37 +00001145 if (Subtarget->hasVFP2() && !isVarArg) {
Evan Cheng76f920d2010-10-22 18:23:05 +00001146 if (!Subtarget->isAAPCS_ABI())
1147 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1148 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1149 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1150 }
1151 // Fallthrough
1152 case CallingConv::C: {
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001153 // Use target triple & subtarget features to do actual dispatch.
Evan Cheng76f920d2010-10-22 18:23:05 +00001154 if (!Subtarget->isAAPCS_ABI())
1155 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1156 else if (Subtarget->hasVFP2() &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001157 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1158 !isVarArg)
Evan Cheng76f920d2010-10-22 18:23:05 +00001159 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1160 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1161 }
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001162 case CallingConv::ARM_AAPCS_VFP:
Anton Korobeynikovf349cb82012-01-29 09:06:09 +00001163 if (!isVarArg)
1164 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1165 // Fallthrough
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001166 case CallingConv::ARM_AAPCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001167 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001168 case CallingConv::ARM_APCS:
Evan Cheng76f920d2010-10-22 18:23:05 +00001169 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Eric Christophere94ac882012-08-03 00:05:53 +00001170 case CallingConv::GHC:
1171 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001172 }
1173}
1174
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175/// LowerCallResult - Lower the result values of a call into the
1176/// appropriate copies out of appropriate physical registers.
1177SDValue
1178ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001179 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 const SmallVectorImpl<ISD::InputArg> &Ins,
1181 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001182 SmallVectorImpl<SDValue> &InVals) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001183
Bob Wilson1f595bb2009-04-17 19:07:39 +00001184 // Assign locations to each value returned by this call.
1185 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001186 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1187 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001188 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001189 CCAssignFnForNode(CallConv, /* Return*/ true,
1190 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001191
1192 // Copy all of the result registers out of their specified physreg.
1193 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1194 CCValAssign VA = RVLocs[i];
1195
Bob Wilson80915242009-04-25 00:33:20 +00001196 SDValue Val;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001197 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001198 // Handle f64 or half of a v2f64.
Owen Anderson825b72b2009-08-11 20:47:22 +00001199 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson1f595bb2009-04-17 19:07:39 +00001200 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001201 Chain = Lo.getValue(1);
1202 InFlag = Lo.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001203 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001204 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001205 InFlag);
1206 Chain = Hi.getValue(1);
1207 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001208 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson5bafff32009-06-22 23:27:02 +00001209
Owen Anderson825b72b2009-08-11 20:47:22 +00001210 if (VA.getLocVT() == MVT::v2f64) {
1211 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1212 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1213 DAG.getConstant(0, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001214
1215 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001216 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001217 Chain = Lo.getValue(1);
1218 InFlag = Lo.getValue(2);
1219 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson825b72b2009-08-11 20:47:22 +00001220 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson5bafff32009-06-22 23:27:02 +00001221 Chain = Hi.getValue(1);
1222 InFlag = Hi.getValue(2);
Jim Grosbache5165492009-11-09 00:11:35 +00001223 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson825b72b2009-08-11 20:47:22 +00001224 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1225 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001226 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00001227 } else {
Bob Wilson80915242009-04-25 00:33:20 +00001228 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1229 InFlag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001230 Chain = Val.getValue(1);
1231 InFlag = Val.getValue(2);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001232 }
Bob Wilson80915242009-04-25 00:33:20 +00001233
1234 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001235 default: llvm_unreachable("Unknown loc info!");
Bob Wilson80915242009-04-25 00:33:20 +00001236 case CCValAssign::Full: break;
1237 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001238 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson80915242009-04-25 00:33:20 +00001239 break;
1240 }
1241
Dan Gohman98ca4f22009-08-05 01:29:28 +00001242 InVals.push_back(Val);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001243 }
1244
Dan Gohman98ca4f22009-08-05 01:29:28 +00001245 return Chain;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001246}
1247
Bob Wilsondee46d72009-04-17 20:35:10 +00001248/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001249SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001250ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1251 SDValue StackPtr, SDValue Arg,
1252 DebugLoc dl, SelectionDAG &DAG,
1253 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001254 ISD::ArgFlagsTy Flags) const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001255 unsigned LocMemOffset = VA.getLocMemOffset();
1256 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1257 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001258 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001259 MachinePointerInfo::getStack(LocMemOffset),
David Greene1b58cab2010-02-15 16:55:24 +00001260 false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00001261}
1262
Dan Gohman98ca4f22009-08-05 01:29:28 +00001263void ARMTargetLowering::PassF64ArgInRegs(DebugLoc dl, SelectionDAG &DAG,
Bob Wilson5bafff32009-06-22 23:27:02 +00001264 SDValue Chain, SDValue &Arg,
1265 RegsToPassVector &RegsToPass,
1266 CCValAssign &VA, CCValAssign &NextVA,
1267 SDValue &StackPtr,
1268 SmallVector<SDValue, 8> &MemOpChains,
Dan Gohmand858e902010-04-17 15:26:15 +00001269 ISD::ArgFlagsTy Flags) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00001270
Jim Grosbache5165492009-11-09 00:11:35 +00001271 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001272 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Bob Wilson5bafff32009-06-22 23:27:02 +00001273 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1274
1275 if (NextVA.isRegLoc())
1276 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1277 else {
1278 assert(NextVA.isMemLoc());
1279 if (StackPtr.getNode() == 0)
1280 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1281
Dan Gohman98ca4f22009-08-05 01:29:28 +00001282 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1283 dl, DAG, NextVA,
1284 Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001285 }
1286}
1287
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288/// LowerCall - Lowering a call into a callseq_start <-
Evan Chengfc403422007-02-03 08:53:01 +00001289/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1290/// nodes.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001291SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001292ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00001293 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001294 SelectionDAG &DAG = CLI.DAG;
1295 DebugLoc &dl = CLI.DL;
1296 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
1297 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
1298 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
1299 SDValue Chain = CLI.Chain;
1300 SDValue Callee = CLI.Callee;
1301 bool &isTailCall = CLI.IsTailCall;
1302 CallingConv::ID CallConv = CLI.CallConv;
1303 bool doesNotRet = CLI.DoesNotReturn;
1304 bool isVarArg = CLI.IsVarArg;
1305
Dale Johannesen51e28e62010-06-03 21:09:53 +00001306 MachineFunction &MF = DAG.getMachineFunction();
1307 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1308 bool IsSibCall = false;
Bob Wilson6d2f9ce2011-10-07 17:17:49 +00001309 // Disable tail calls if they're not supported.
1310 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Bob Wilson703af3a2010-08-13 22:43:33 +00001311 isTailCall = false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001312 if (isTailCall) {
1313 // Check if it's really possible to do a tail call.
1314 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1315 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001316 Outs, OutVals, Ins, DAG);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001317 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1318 // detected sibcalls.
1319 if (isTailCall) {
1320 ++NumTailCalls;
1321 IsSibCall = true;
1322 }
1323 }
Evan Chenga8e29892007-01-19 07:51:42 +00001324
Bob Wilson1f595bb2009-04-17 19:07:39 +00001325 // Analyze operands of the call, assigning locations to each operand.
1326 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001327 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1328 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001329 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001330 CCAssignFnForNode(CallConv, /* Return*/ false,
1331 isVarArg));
Evan Chenga8e29892007-01-19 07:51:42 +00001332
Bob Wilson1f595bb2009-04-17 19:07:39 +00001333 // Get a count of how many bytes are to be pushed on the stack.
1334 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chenga8e29892007-01-19 07:51:42 +00001335
Dale Johannesen51e28e62010-06-03 21:09:53 +00001336 // For tail calls, memory operands are available in our caller's stack.
1337 if (IsSibCall)
1338 NumBytes = 0;
1339
Evan Chenga8e29892007-01-19 07:51:42 +00001340 // Adjust the stack pointer for the new arguments...
1341 // These operations are automatically eliminated by the prolog/epilog pass
Dale Johannesen51e28e62010-06-03 21:09:53 +00001342 if (!IsSibCall)
1343 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Evan Chenga8e29892007-01-19 07:51:42 +00001344
Jim Grosbachf9a4b762010-02-24 01:43:03 +00001345 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Chenga8e29892007-01-19 07:51:42 +00001346
Bob Wilson5bafff32009-06-22 23:27:02 +00001347 RegsToPassVector RegsToPass;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001348 SmallVector<SDValue, 8> MemOpChains;
Evan Chenga8e29892007-01-19 07:51:42 +00001349
Bob Wilson1f595bb2009-04-17 19:07:39 +00001350 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsondee46d72009-04-17 20:35:10 +00001351 // of tail call optimization, arguments are handled later.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001352 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1353 i != e;
1354 ++i, ++realArgIdx) {
1355 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00001356 SDValue Arg = OutVals[realArgIdx];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001357 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001358 bool isByVal = Flags.isByVal();
Evan Chenga8e29892007-01-19 07:51:42 +00001359
Bob Wilson1f595bb2009-04-17 19:07:39 +00001360 // Promote the value if needed.
1361 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001362 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001363 case CCValAssign::Full: break;
1364 case CCValAssign::SExt:
1365 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1366 break;
1367 case CCValAssign::ZExt:
1368 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1369 break;
1370 case CCValAssign::AExt:
1371 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1372 break;
1373 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001374 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001375 break;
Evan Chenga8e29892007-01-19 07:51:42 +00001376 }
1377
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001378 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilson1f595bb2009-04-17 19:07:39 +00001379 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001380 if (VA.getLocVT() == MVT::v2f64) {
1381 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1382 DAG.getConstant(0, MVT::i32));
1383 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1384 DAG.getConstant(1, MVT::i32));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001385
Dan Gohman98ca4f22009-08-05 01:29:28 +00001386 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001387 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1388
1389 VA = ArgLocs[++i]; // skip ahead to next loc
1390 if (VA.isRegLoc()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001391 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson5bafff32009-06-22 23:27:02 +00001392 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1393 } else {
1394 assert(VA.isMemLoc());
Bob Wilson5bafff32009-06-22 23:27:02 +00001395
Dan Gohman98ca4f22009-08-05 01:29:28 +00001396 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1397 dl, DAG, VA, Flags));
Bob Wilson5bafff32009-06-22 23:27:02 +00001398 }
1399 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001400 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson5bafff32009-06-22 23:27:02 +00001401 StackPtr, MemOpChains, Flags);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001402 }
1403 } else if (VA.isRegLoc()) {
1404 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastingsc7315872011-04-20 16:47:52 +00001405 } else if (isByVal) {
1406 assert(VA.isMemLoc());
1407 unsigned offset = 0;
1408
1409 // True if this byval aggregate will be split between registers
1410 // and memory.
1411 if (CCInfo.isFirstByValRegValid()) {
1412 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1413 unsigned int i, j;
1414 for (i = 0, j = CCInfo.getFirstByValReg(); j < ARM::R4; i++, j++) {
1415 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1416 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1417 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1418 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001419 false, false, false, 0);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001420 MemOpChains.push_back(Load.getValue(1));
1421 RegsToPass.push_back(std::make_pair(j, Load));
1422 }
1423 offset = ARM::R4 - CCInfo.getFirstByValReg();
1424 CCInfo.clearFirstByValReg();
1425 }
1426
Manman Ren763a75d2012-06-01 02:44:42 +00001427 if (Flags.getByValSize() - 4*offset > 0) {
1428 unsigned LocMemOffset = VA.getLocMemOffset();
1429 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1430 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1431 StkPtrOff);
1432 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1433 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1434 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1435 MVT::i32);
Manman Ren68f25572012-06-01 19:33:18 +00001436 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastingsc7315872011-04-20 16:47:52 +00001437
Manman Ren763a75d2012-06-01 02:44:42 +00001438 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Ren68f25572012-06-01 19:33:18 +00001439 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren763a75d2012-06-01 02:44:42 +00001440 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1441 Ops, array_lengthof(Ops)));
1442 }
Stuart Hastingsc7315872011-04-20 16:47:52 +00001443 } else if (!IsSibCall) {
Bob Wilson1f595bb2009-04-17 19:07:39 +00001444 assert(VA.isMemLoc());
Bob Wilson1f595bb2009-04-17 19:07:39 +00001445
Dan Gohman98ca4f22009-08-05 01:29:28 +00001446 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1447 dl, DAG, VA, Flags));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001448 }
Evan Chenga8e29892007-01-19 07:51:42 +00001449 }
1450
1451 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001452 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Evan Chenga8e29892007-01-19 07:51:42 +00001453 &MemOpChains[0], MemOpChains.size());
1454
1455 // Build a sequence of copy-to-reg nodes chained together with token chain
1456 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00001457 SDValue InFlag;
Dale Johannesen6470a112010-06-15 22:08:33 +00001458 // Tail call byval lowering might overwrite argument registers so in case of
1459 // tail call optimization the copies to registers are lowered later.
1460 if (!isTailCall)
1461 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1462 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1463 RegsToPass[i].second, InFlag);
1464 InFlag = Chain.getValue(1);
1465 }
Evan Chenga8e29892007-01-19 07:51:42 +00001466
Dale Johannesen51e28e62010-06-03 21:09:53 +00001467 // For tail calls lower the arguments to the 'real' stack slot.
1468 if (isTailCall) {
1469 // Force all the incoming stack arguments to be loaded from the stack
1470 // before any new outgoing arguments are stored to the stack, because the
1471 // outgoing stack slots may alias the incoming argument stack slots, and
1472 // the alias isn't otherwise explicit. This is slightly more conservative
1473 // than necessary, because it means that each store effectively depends
1474 // on every argument instead of just those arguments it would clobber.
1475
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001476 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001477 InFlag = SDValue();
1478 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1479 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1480 RegsToPass[i].second, InFlag);
1481 InFlag = Chain.getValue(1);
1482 }
1483 InFlag =SDValue();
1484 }
1485
Bill Wendling056292f2008-09-16 21:48:12 +00001486 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1487 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1488 // node so that legalize doesn't hack it.
Evan Chenga8e29892007-01-19 07:51:42 +00001489 bool isDirect = false;
1490 bool isARMFunc = false;
Evan Cheng277f0742007-06-19 21:05:09 +00001491 bool isLocalARMFunc = false;
Evan Chenge7e0d622009-11-06 22:24:13 +00001492 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbache7b52522010-04-14 22:28:31 +00001493
1494 if (EnableARMLongCalls) {
1495 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1496 && "long-calls with non-static relocation model!");
1497 // Handle a global address or an external symbol. If it's not one of
1498 // those, the target's already in a register, so we don't need to do
1499 // anything extra.
1500 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson0dbdca52010-04-15 03:11:28 +00001501 const GlobalValue *GV = G->getGlobal();
Jim Grosbache7b52522010-04-14 22:28:31 +00001502 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001503 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001504 ARMConstantPoolValue *CPV =
1505 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1506
Jim Grosbache7b52522010-04-14 22:28:31 +00001507 // Get the address of the callee into a register
1508 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1509 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1510 Callee = DAG.getLoad(getPointerTy(), dl,
1511 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001512 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001513 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001514 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1515 const char *Sym = S->getSymbol();
1516
1517 // Create a constant pool entry for the callee address
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001518 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001519 ARMConstantPoolValue *CPV =
1520 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1521 ARMPCLabelIndex, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001522 // Get the address of the callee into a register
1523 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1524 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1525 Callee = DAG.getLoad(getPointerTy(), dl,
1526 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001527 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001528 false, false, false, 0);
Jim Grosbache7b52522010-04-14 22:28:31 +00001529 }
1530 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohman46510a72010-04-15 01:51:59 +00001531 const GlobalValue *GV = G->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00001532 isDirect = true;
Chris Lattner4fb63d02009-07-15 04:12:33 +00001533 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Evan Cheng970a4192007-01-19 19:28:01 +00001534 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
Evan Chenga8e29892007-01-19 07:51:42 +00001535 getTargetMachine().getRelocationModel() != Reloc::Static;
1536 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng277f0742007-06-19 21:05:09 +00001537 // ARM call to a local ARM function is predicable.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001538 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Chengc60e76d2007-01-30 20:37:08 +00001539 // tBX takes a register source operand.
David Goodwinf1daf7d2009-07-08 23:10:31 +00001540 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001541 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00001542 ARMConstantPoolValue *CPV =
1543 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001544 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001545 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001546 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001547 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001548 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001549 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001550 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001551 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001552 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001553 } else {
1554 // On ELF targets for PIC code, direct calls should go through the PLT
1555 unsigned OpFlags = 0;
1556 if (Subtarget->isTargetELF() &&
1557 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1558 OpFlags = ARMII::MO_PLT;
1559 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1560 }
Bill Wendling056292f2008-09-16 21:48:12 +00001561 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Chenga8e29892007-01-19 07:51:42 +00001562 isDirect = true;
Evan Cheng970a4192007-01-19 19:28:01 +00001563 bool isStub = Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +00001564 getTargetMachine().getRelocationModel() != Reloc::Static;
1565 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc60e76d2007-01-30 20:37:08 +00001566 // tBX takes a register source operand.
1567 const char *Sym = S->getSymbol();
David Goodwinf1daf7d2009-07-08 23:10:31 +00001568 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001569 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingfe31e672011-10-01 08:58:29 +00001570 ARMConstantPoolValue *CPV =
1571 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1572 ARMPCLabelIndex, 4);
Evan Cheng1606e8e2009-03-13 07:51:59 +00001573 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00001574 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen33c960f2009-02-04 20:06:27 +00001575 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Cheng9eda6892009-10-31 03:39:36 +00001576 DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001577 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001578 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00001579 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson2dc4f542009-03-20 22:42:55 +00001580 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00001581 getPointerTy(), Callee, PICLabel);
Jim Grosbach637d89f2010-09-22 23:27:36 +00001582 } else {
1583 unsigned OpFlags = 0;
1584 // On ELF targets for PIC code, direct calls should go through the PLT
1585 if (Subtarget->isTargetELF() &&
1586 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1587 OpFlags = ARMII::MO_PLT;
1588 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1589 }
Evan Chenga8e29892007-01-19 07:51:42 +00001590 }
1591
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001592 // FIXME: handle tail calls differently.
1593 unsigned CallOpc;
Evan Chengb6207242009-08-01 00:16:10 +00001594 if (Subtarget->isThumb()) {
1595 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001596 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001597 else if (doesNotRet && isDirect && !isARMFunc &&
1598 Subtarget->hasRAS() && !Subtarget->isThumb1Only())
1599 // "mov lr, pc; b _foo" to avoid confusing the RSP
1600 CallOpc = ARMISD::CALL_NOLINK;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001601 else
1602 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1603 } else {
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001604 if (!isDirect && !Subtarget->hasV5TOps()) {
1605 CallOpc = ARMISD::CALL_NOLINK;
1606 } else if (doesNotRet && isDirect && Subtarget->hasRAS())
1607 // "mov lr, pc; b _foo" to avoid confusing the RSP
1608 CallOpc = ARMISD::CALL_NOLINK;
1609 else
1610 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001611 }
Lauro Ramos Venancio64c88d72007-03-20 17:57:23 +00001612
Dan Gohman475871a2008-07-27 21:46:04 +00001613 std::vector<SDValue> Ops;
Evan Chenga8e29892007-01-19 07:51:42 +00001614 Ops.push_back(Chain);
1615 Ops.push_back(Callee);
1616
1617 // Add argument registers to the end of the list so that they are known live
1618 // into the call.
1619 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1620 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1621 RegsToPass[i].second.getValueType()));
1622
Jakob Stoklund Olesenc54f6342012-02-24 01:19:29 +00001623 // Add a register mask operand representing the call-preserved registers.
1624 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1625 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
1626 assert(Mask && "Missing call preserved mask for calling convention");
1627 Ops.push_back(DAG.getRegisterMask(Mask));
1628
Gabor Greifba36cb52008-08-28 21:40:38 +00001629 if (InFlag.getNode())
Evan Chenga8e29892007-01-19 07:51:42 +00001630 Ops.push_back(InFlag);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001631
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001632 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesencf296fa2010-06-05 00:51:39 +00001633 if (isTailCall)
Dale Johannesen51e28e62010-06-03 21:09:53 +00001634 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Dale Johannesen51e28e62010-06-03 21:09:53 +00001635
Duncan Sands4bdcb612008-07-02 17:40:58 +00001636 // Returns a chain and a flag for retval copy to use.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001637 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Evan Chenga8e29892007-01-19 07:51:42 +00001638 InFlag = Chain.getValue(1);
1639
Chris Lattnere563bbc2008-10-11 22:08:30 +00001640 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1641 DAG.getIntPtrConstant(0, true), InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001642 if (!Ins.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001643 InFlag = Chain.getValue(1);
1644
Bob Wilson1f595bb2009-04-17 19:07:39 +00001645 // Handle result values, copying them out of physregs into vregs that we
1646 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001647 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins,
1648 dl, DAG, InVals);
Evan Chenga8e29892007-01-19 07:51:42 +00001649}
1650
Stuart Hastingsf222e592011-02-28 17:17:53 +00001651/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastingsc7315872011-04-20 16:47:52 +00001652/// on the stack. Remember the next parameter register to allocate,
1653/// and then confiscate the rest of the parameter registers to insure
Stuart Hastingsf222e592011-02-28 17:17:53 +00001654/// this.
1655void
Craig Topperc89c7442012-03-27 07:21:54 +00001656ARMTargetLowering::HandleByVal(CCState *State, unsigned &size) const {
Stuart Hastingsc7315872011-04-20 16:47:52 +00001657 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1658 assert((State->getCallOrPrologue() == Prologue ||
1659 State->getCallOrPrologue() == Call) &&
1660 "unhandled ParmContext");
1661 if ((!State->isFirstByValRegValid()) &&
1662 (ARM::R0 <= reg) && (reg <= ARM::R3)) {
1663 State->setFirstByValReg(reg);
1664 // At a call site, a byval parameter that is split between
1665 // registers and memory needs its size truncated here. In a
1666 // function prologue, such byval parameters are reassembled in
1667 // memory, and are not truncated.
1668 if (State->getCallOrPrologue() == Call) {
1669 unsigned excess = 4 * (ARM::R4 - reg);
1670 assert(size >= excess && "expected larger existing stack allocation");
1671 size -= excess;
1672 }
1673 }
1674 // Confiscate any remaining parameter registers to preclude their
1675 // assignment to subsequent parameters.
1676 while (State->AllocateReg(GPRArgRegs, 4))
1677 ;
Stuart Hastingsf222e592011-02-28 17:17:53 +00001678}
1679
Dale Johannesen51e28e62010-06-03 21:09:53 +00001680/// MatchingStackOffset - Return true if the given stack call argument is
1681/// already available in the same position (relatively) of the caller's
1682/// incoming argument stack.
1683static
1684bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1685 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topperacf20772012-03-25 23:49:58 +00001686 const TargetInstrInfo *TII) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001687 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1688 int FI = INT_MAX;
1689 if (Arg.getOpcode() == ISD::CopyFromReg) {
1690 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00001691 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesen51e28e62010-06-03 21:09:53 +00001692 return false;
1693 MachineInstr *Def = MRI->getVRegDef(VR);
1694 if (!Def)
1695 return false;
1696 if (!Flags.isByVal()) {
1697 if (!TII->isLoadFromStackSlot(Def, FI))
1698 return false;
1699 } else {
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001700 return false;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001701 }
1702 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1703 if (Flags.isByVal())
1704 // ByVal argument is passed in as a pointer but it's now being
1705 // dereferenced. e.g.
1706 // define @foo(%struct.X* %A) {
1707 // tail call @bar(%struct.X* byval %A)
1708 // }
1709 return false;
1710 SDValue Ptr = Ld->getBasePtr();
1711 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1712 if (!FINode)
1713 return false;
1714 FI = FINode->getIndex();
1715 } else
1716 return false;
1717
1718 assert(FI != INT_MAX);
1719 if (!MFI->isFixedObjectIndex(FI))
1720 return false;
1721 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1722}
1723
1724/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1725/// for tail call optimization. Targets which want to do tail call
1726/// optimization should implement this function.
1727bool
1728ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1729 CallingConv::ID CalleeCC,
1730 bool isVarArg,
1731 bool isCalleeStructRet,
1732 bool isCallerStructRet,
1733 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001734 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesen51e28e62010-06-03 21:09:53 +00001735 const SmallVectorImpl<ISD::InputArg> &Ins,
1736 SelectionDAG& DAG) const {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001737 const Function *CallerF = DAG.getMachineFunction().getFunction();
1738 CallingConv::ID CallerCC = CallerF->getCallingConv();
1739 bool CCMatch = CallerCC == CalleeCC;
1740
1741 // Look for obvious safe cases to perform tail call optimization that do not
1742 // require ABI changes. This is what gcc calls sibcall.
1743
Jim Grosbach7616b642010-06-16 23:45:49 +00001744 // Do not sibcall optimize vararg calls unless the call site is not passing
1745 // any arguments.
Dale Johannesen51e28e62010-06-03 21:09:53 +00001746 if (isVarArg && !Outs.empty())
1747 return false;
1748
1749 // Also avoid sibcall optimization if either caller or callee uses struct
1750 // return semantics.
1751 if (isCalleeStructRet || isCallerStructRet)
1752 return false;
1753
Dale Johannesene39fdbe2010-06-23 18:52:34 +00001754 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach8dc41f32011-07-08 20:18:11 +00001755 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1756 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1757 // support in the assembler and linker to be used. This would need to be
1758 // fixed to fully support tail calls in Thumb1.
1759 //
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001760 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1761 // LR. This means if we need to reload LR, it takes an extra instructions,
1762 // which outweighs the value of the tail call; but here we don't know yet
1763 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach4725ca72010-09-08 03:54:02 +00001764 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001765 // emitEpilogue if LR is used.
Dale Johannesen7835f1f2010-07-08 01:18:23 +00001766
1767 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1768 // but we need to make sure there are enough registers; the only valid
1769 // registers are the 4 used for parameters. We don't currently do this
1770 // case.
Evan Cheng3d2125c2010-11-30 23:55:39 +00001771 if (Subtarget->isThumb1Only())
1772 return false;
Dale Johannesendf50d7e2010-06-18 18:13:11 +00001773
Dale Johannesen51e28e62010-06-03 21:09:53 +00001774 // If the calling conventions do not match, then we'd better make sure the
1775 // results are returned in the same way as what the caller expects.
1776 if (!CCMatch) {
1777 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001778 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1779 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001780 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1781
1782 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001783 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1784 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001785 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1786
1787 if (RVLocs1.size() != RVLocs2.size())
1788 return false;
1789 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1790 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1791 return false;
1792 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1793 return false;
1794 if (RVLocs1[i].isRegLoc()) {
1795 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1796 return false;
1797 } else {
1798 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1799 return false;
1800 }
1801 }
1802 }
1803
1804 // If the callee takes no arguments then go on to check the results of the
1805 // call.
1806 if (!Outs.empty()) {
1807 // Check if stack adjustment is needed. For now, do not do this if any
1808 // argument is passed on the stack.
1809 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001810 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1811 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesen51e28e62010-06-03 21:09:53 +00001812 CCInfo.AnalyzeCallOperands(Outs,
1813 CCAssignFnForNode(CalleeCC, false, isVarArg));
1814 if (CCInfo.getNextStackOffset()) {
1815 MachineFunction &MF = DAG.getMachineFunction();
1816
1817 // Check if the arguments are already laid out in the right way as
1818 // the caller's fixed stack objects.
1819 MachineFrameInfo *MFI = MF.getFrameInfo();
1820 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topperacf20772012-03-25 23:49:58 +00001821 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesencf296fa2010-06-05 00:51:39 +00001822 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1823 i != e;
1824 ++i, ++realArgIdx) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001825 CCValAssign &VA = ArgLocs[i];
1826 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001827 SDValue Arg = OutVals[realArgIdx];
Dale Johannesencf296fa2010-06-05 00:51:39 +00001828 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesen51e28e62010-06-03 21:09:53 +00001829 if (VA.getLocInfo() == CCValAssign::Indirect)
1830 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001831 if (VA.needsCustom()) {
1832 // f64 and vector types are split into multiple registers or
1833 // register/stack-slot combinations. The types will not match
1834 // the registers; give up on memory f64 refs until we figure
1835 // out what to do about this.
1836 if (!VA.isRegLoc())
1837 return false;
1838 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach4725ca72010-09-08 03:54:02 +00001839 return false;
Dale Johannesencf296fa2010-06-05 00:51:39 +00001840 if (RegVT == MVT::v2f64) {
1841 if (!ArgLocs[++i].isRegLoc())
1842 return false;
1843 if (!ArgLocs[++i].isRegLoc())
1844 return false;
1845 }
1846 } else if (!VA.isRegLoc()) {
Dale Johannesen51e28e62010-06-03 21:09:53 +00001847 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
1848 MFI, MRI, TII))
1849 return false;
1850 }
1851 }
1852 }
1853 }
1854
1855 return true;
1856}
1857
Dan Gohman98ca4f22009-08-05 01:29:28 +00001858SDValue
1859ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001860 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001861 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001862 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001863 DebugLoc dl, SelectionDAG &DAG) const {
Bob Wilson2dc4f542009-03-20 22:42:55 +00001864
Bob Wilsondee46d72009-04-17 20:35:10 +00001865 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001866 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilson1f595bb2009-04-17 19:07:39 +00001867
Bob Wilsondee46d72009-04-17 20:35:10 +00001868 // CCState - Info about the registers and stack slots.
Cameron Zwaricha86686e2011-06-10 20:59:24 +00001869 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1870 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001871
Dan Gohman98ca4f22009-08-05 01:29:28 +00001872 // Analyze outgoing return values.
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001873 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
1874 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00001875
1876 // If this is the first return lowered for this function, add
1877 // the regs to the liveout set for the function.
1878 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1879 for (unsigned i = 0; i != RVLocs.size(); ++i)
1880 if (RVLocs[i].isRegLoc())
1881 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Evan Chenga8e29892007-01-19 07:51:42 +00001882 }
1883
Bob Wilson1f595bb2009-04-17 19:07:39 +00001884 SDValue Flag;
1885
1886 // Copy the result values into the output registers.
1887 for (unsigned i = 0, realRVLocIdx = 0;
1888 i != RVLocs.size();
1889 ++i, ++realRVLocIdx) {
1890 CCValAssign &VA = RVLocs[i];
1891 assert(VA.isRegLoc() && "Can only return in registers!");
1892
Dan Gohmanc9403652010-07-07 15:54:55 +00001893 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilson1f595bb2009-04-17 19:07:39 +00001894
1895 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001896 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00001897 case CCValAssign::Full: break;
1898 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001899 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001900 break;
1901 }
1902
Bob Wilson1f595bb2009-04-17 19:07:39 +00001903 if (VA.needsCustom()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00001905 // Extract the first half and return it in two registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001906 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1907 DAG.getConstant(0, MVT::i32));
Jim Grosbache5165492009-11-09 00:11:35 +00001908 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001909 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson5bafff32009-06-22 23:27:02 +00001910
1911 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
1912 Flag = Chain.getValue(1);
1913 VA = RVLocs[++i]; // skip ahead to next loc
1914 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1915 HalfGPRs.getValue(1), Flag);
1916 Flag = Chain.getValue(1);
1917 VA = RVLocs[++i]; // skip ahead to next loc
1918
1919 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00001920 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1921 DAG.getConstant(1, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00001922 }
1923 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
1924 // available.
Jim Grosbache5165492009-11-09 00:11:35 +00001925 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001927 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
Bob Wilson4d59e1d2009-04-24 17:00:36 +00001928 Flag = Chain.getValue(1);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001929 VA = RVLocs[++i]; // skip ahead to next loc
1930 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
1931 Flag);
1932 } else
1933 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
1934
Bob Wilsondee46d72009-04-17 20:35:10 +00001935 // Guarantee that all emitted copies are
1936 // stuck together, avoiding something bad.
Bob Wilson1f595bb2009-04-17 19:07:39 +00001937 Flag = Chain.getValue(1);
1938 }
1939
1940 SDValue result;
1941 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00001942 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001943 else // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 result = DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, Chain);
Bob Wilson1f595bb2009-04-17 19:07:39 +00001945
1946 return result;
Evan Chenga8e29892007-01-19 07:51:42 +00001947}
1948
Evan Chengbf010eb2012-04-10 01:51:00 +00001949bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001950 if (N->getNumValues() != 1)
1951 return false;
1952 if (!N->hasNUsesOfValue(1, 0))
1953 return false;
1954
Evan Chengbf010eb2012-04-10 01:51:00 +00001955 SDValue TCChain = Chain;
1956 SDNode *Copy = *N->use_begin();
1957 if (Copy->getOpcode() == ISD::CopyToReg) {
1958 // If the copy has a glue operand, we conservatively assume it isn't safe to
1959 // perform a tail call.
1960 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1961 return false;
1962 TCChain = Copy->getOperand(0);
1963 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
1964 SDNode *VMov = Copy;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001965 // f64 returned in a pair of GPRs.
Evan Chengbf010eb2012-04-10 01:51:00 +00001966 SmallPtrSet<SDNode*, 2> Copies;
1967 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Cheng3d2125c2010-11-30 23:55:39 +00001968 UI != UE; ++UI) {
1969 if (UI->getOpcode() != ISD::CopyToReg)
1970 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001971 Copies.insert(*UI);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001972 }
Evan Chengbf010eb2012-04-10 01:51:00 +00001973 if (Copies.size() > 2)
1974 return false;
1975
1976 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
1977 UI != UE; ++UI) {
1978 SDValue UseChain = UI->getOperand(0);
1979 if (Copies.count(UseChain.getNode()))
1980 // Second CopyToReg
1981 Copy = *UI;
1982 else
1983 // First CopyToReg
1984 TCChain = UseChain;
1985 }
1986 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001987 // f32 returned in a single GPR.
Evan Chengbf010eb2012-04-10 01:51:00 +00001988 if (!Copy->hasOneUse())
Evan Cheng3d2125c2010-11-30 23:55:39 +00001989 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001990 Copy = *Copy->use_begin();
1991 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Cheng3d2125c2010-11-30 23:55:39 +00001992 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001993 Chain = Copy->getOperand(0);
Evan Cheng3d2125c2010-11-30 23:55:39 +00001994 } else {
1995 return false;
1996 }
1997
Evan Cheng1bf891a2010-12-01 22:59:46 +00001998 bool HasRet = false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001999 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2000 UI != UE; ++UI) {
2001 if (UI->getOpcode() != ARMISD::RET_FLAG)
2002 return false;
2003 HasRet = true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002004 }
2005
Evan Chengbf010eb2012-04-10 01:51:00 +00002006 if (!HasRet)
2007 return false;
2008
2009 Chain = TCChain;
2010 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00002011}
2012
Evan Cheng485fafc2011-03-21 01:19:09 +00002013bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Evan Cheng1c80f562012-03-30 01:24:39 +00002014 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
Evan Cheng485fafc2011-03-21 01:19:09 +00002015 return false;
2016
2017 if (!CI->isTailCall())
2018 return false;
2019
2020 return !Subtarget->isThumb1Only();
2021}
2022
Bob Wilsonb62d2572009-11-03 00:02:05 +00002023// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2024// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2025// one of the above mentioned nodes. It has to be wrapped because otherwise
2026// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2027// be used to form addressing mode. These wrapped nodes will be selected
2028// into MOVi.
Dan Gohman475871a2008-07-27 21:46:04 +00002029static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002030 EVT PtrVT = Op.getValueType();
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002031 // FIXME there is no actual debug info here
2032 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002033 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00002034 SDValue Res;
Evan Chenga8e29892007-01-19 07:51:42 +00002035 if (CP->isMachineConstantPoolEntry())
2036 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2037 CP->getAlignment());
2038 else
2039 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2040 CP->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00002041 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Chenga8e29892007-01-19 07:51:42 +00002042}
2043
Jim Grosbache1102ca2010-07-19 17:20:38 +00002044unsigned ARMTargetLowering::getJumpTableEncoding() const {
2045 return MachineJumpTableInfo::EK_Inline;
2046}
2047
Dan Gohmand858e902010-04-17 15:26:15 +00002048SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2049 SelectionDAG &DAG) const {
Evan Chenge7e0d622009-11-06 22:24:13 +00002050 MachineFunction &MF = DAG.getMachineFunction();
2051 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2052 unsigned ARMPCLabelIndex = 0;
Bob Wilsonddb16df2009-10-30 05:45:42 +00002053 DebugLoc DL = Op.getDebugLoc();
Bob Wilson907eebd2009-11-02 20:59:23 +00002054 EVT PtrVT = getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +00002055 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson907eebd2009-11-02 20:59:23 +00002056 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2057 SDValue CPAddr;
2058 if (RelocM == Reloc::Static) {
2059 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2060 } else {
2061 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002062 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling5bb77992011-10-01 08:00:54 +00002063 ARMConstantPoolValue *CPV =
2064 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2065 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson907eebd2009-11-02 20:59:23 +00002066 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2067 }
2068 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2069 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002070 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002071 false, false, false, 0);
Bob Wilson907eebd2009-11-02 20:59:23 +00002072 if (RelocM == Reloc::Static)
2073 return Result;
Evan Chenge7e0d622009-11-06 22:24:13 +00002074 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson907eebd2009-11-02 20:59:23 +00002075 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilsonddb16df2009-10-30 05:45:42 +00002076}
2077
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002078// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman475871a2008-07-27 21:46:04 +00002079SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002080ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohmand858e902010-04-17 15:26:15 +00002081 SelectionDAG &DAG) const {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002082 DebugLoc dl = GA->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002083 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002084 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chenge7e0d622009-11-06 22:24:13 +00002085 MachineFunction &MF = DAG.getMachineFunction();
2086 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002087 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002088 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002089 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2090 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002091 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002092 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Cheng9eda6892009-10-31 03:39:36 +00002093 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002094 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002095 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002096 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002097
Evan Chenge7e0d622009-11-06 22:24:13 +00002098 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002099 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002100
2101 // call __tls_get_addr.
2102 ArgListTy Args;
2103 ArgListEntry Entry;
2104 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002105 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002106 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00002107 // FIXME: is there useful debug info available here?
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002108 TargetLowering::CallLoweringInfo CLI(Chain,
2109 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng59bc0602009-08-14 19:11:20 +00002110 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00002111 0, CallingConv::C, /*isTailCall=*/false,
2112 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling46ada192010-03-02 01:55:18 +00002113 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002114 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002115 return CallResult.first;
2116}
2117
2118// Lower ISD::GlobalTLSAddress using the "initial exec" or
2119// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00002120SDValue
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002121ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002122 SelectionDAG &DAG,
2123 TLSModel::Model model) const {
Dan Gohman46510a72010-04-15 01:51:59 +00002124 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002125 DebugLoc dl = GA->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002126 SDValue Offset;
2127 SDValue Chain = DAG.getEntryNode();
Owen Andersone50ed302009-08-10 22:56:29 +00002128 EVT PtrVT = getPointerTy();
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002129 // Get the Thread Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00002130 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002131
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002132 if (model == TLSModel::InitialExec) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002133 MachineFunction &MF = DAG.getMachineFunction();
2134 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002135 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge7e0d622009-11-06 22:24:13 +00002136 // Initial exec model.
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002137 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2138 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002139 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2140 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2141 true);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002142 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002143 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002144 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002145 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002146 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002147 Chain = Offset.getValue(1);
2148
Evan Chenge7e0d622009-11-06 22:24:13 +00002149 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002150 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002151
Evan Cheng9eda6892009-10-31 03:39:36 +00002152 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002153 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002154 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002155 } else {
2156 // local exec model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002157 assert(model == TLSModel::LocalExec);
Bill Wendling5bb77992011-10-01 08:00:54 +00002158 ARMConstantPoolValue *CPV =
2159 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002160 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002161 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Cheng9eda6892009-10-31 03:39:36 +00002162 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002163 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002164 false, false, false, 0);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002165 }
2166
2167 // The address of the thread local variable is the add of the thread
2168 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002169 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002170}
2171
Dan Gohman475871a2008-07-27 21:46:04 +00002172SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00002173ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002174 // TODO: implement the "local dynamic" model
2175 assert(Subtarget->isTargetELF() &&
2176 "TLS not implemented for non-ELF targets");
2177 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgfd5abd52012-05-04 09:40:39 +00002178
2179 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2180
2181 switch (model) {
2182 case TLSModel::GeneralDynamic:
2183 case TLSModel::LocalDynamic:
2184 return LowerToTLSGeneralDynamicModel(GA, DAG);
2185 case TLSModel::InitialExec:
2186 case TLSModel::LocalExec:
2187 return LowerToTLSExecModels(GA, DAG, model);
2188 }
Matt Beaumont-Gay39af9442012-05-04 18:34:27 +00002189 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +00002190}
2191
Dan Gohman475871a2008-07-27 21:46:04 +00002192SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002193 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002194 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002195 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002196 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002197 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2198 if (RelocM == Reloc::PIC_) {
Rafael Espindolabb46f522009-01-15 20:18:42 +00002199 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002200 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002201 ARMConstantPoolConstant::Create(GV,
2202 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002203 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002204 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002205 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002206 CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002207 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002208 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002209 SDValue Chain = Result.getValue(1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00002210 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002211 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002212 if (!UseGOTOFF)
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002213 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002214 MachinePointerInfo::getGOT(),
2215 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002216 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002217 }
2218
2219 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloy015cca62011-10-26 08:53:19 +00002220 // pair. This is always cheaper.
2221 if (Subtarget->useMovt()) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002222 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002223 // FIXME: Once remat is capable of dealing with instructions with register
2224 // operands, expand this into two nodes.
2225 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2226 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002227 } else {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002228 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2229 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2230 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2231 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002232 false, false, false, 0);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002233 }
2234}
2235
Dan Gohman475871a2008-07-27 21:46:04 +00002236SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002237 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002238 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002239 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00002240 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chenga8e29892007-01-19 07:51:42 +00002241 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002242 MachineFunction &MF = DAG.getMachineFunction();
2243 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2244
Jakob Stoklund Olesen8f37a242012-01-07 20:49:15 +00002245 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2246 // update ARMFastISel::ARMMaterializeGV.
Evan Chengf31151f2011-10-26 01:17:44 +00002247 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
Evan Chengfc8475b2011-01-19 02:16:49 +00002248 ++NumMovwMovt;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002249 // FIXME: Once remat is capable of dealing with instructions with register
2250 // operands, expand this into two nodes.
Evan Cheng53519f02011-01-21 18:55:51 +00002251 if (RelocM == Reloc::Static)
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002252 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2253 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2254
Evan Cheng53519f02011-01-21 18:55:51 +00002255 unsigned Wrapper = (RelocM == Reloc::PIC_)
2256 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2257 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
Evan Cheng9fe20092011-01-20 08:34:58 +00002258 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Evan Chengfc8475b2011-01-19 02:16:49 +00002259 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2260 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002261 MachinePointerInfo::getGOT(),
2262 false, false, false, 0);
Evan Chengfc8475b2011-01-19 02:16:49 +00002263 return Result;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002264 }
2265
2266 unsigned ARMPCLabelIndex = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002267 SDValue CPAddr;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002268 if (RelocM == Reloc::Static) {
Evan Cheng1606e8e2009-03-13 07:51:59 +00002269 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002270 } else {
2271 ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Chenge4e4ed32009-08-28 23:18:09 +00002272 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2273 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002274 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2275 PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002276 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Evan Chenga8e29892007-01-19 07:51:42 +00002277 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002278 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Evan Chenga8e29892007-01-19 07:51:42 +00002279
Evan Cheng9eda6892009-10-31 03:39:36 +00002280 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002281 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002282 false, false, false, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00002283 SDValue Chain = Result.getValue(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002284
2285 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002286 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002287 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Evan Chenga8e29892007-01-19 07:51:42 +00002288 }
Evan Chenge4e4ed32009-08-28 23:18:09 +00002289
Evan Cheng63476a82009-09-03 07:04:02 +00002290 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002291 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002292 false, false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002293
2294 return Result;
2295}
2296
Dan Gohman475871a2008-07-27 21:46:04 +00002297SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00002298 SelectionDAG &DAG) const {
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002299 assert(Subtarget->isTargetELF() &&
2300 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Chenge7e0d622009-11-06 22:24:13 +00002301 MachineFunction &MF = DAG.getMachineFunction();
2302 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002303 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Andersone50ed302009-08-10 22:56:29 +00002304 EVT PtrVT = getPointerTy();
Dale Johannesen33c960f2009-02-04 20:06:27 +00002305 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002306 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingfe31e672011-10-01 08:58:29 +00002307 ARMConstantPoolValue *CPV =
2308 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2309 ARMPCLabelIndex, PCAdj);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002310 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002311 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov249fb332009-10-07 00:06:35 +00002312 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002313 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002314 false, false, false, 0);
Evan Chenge7e0d622009-11-06 22:24:13 +00002315 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002316 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00002317}
2318
Jim Grosbach0e0da732009-05-12 23:59:14 +00002319SDValue
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002320ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2321 DebugLoc dl = Op.getDebugLoc();
Jim Grosbach0798edd2010-05-27 23:49:24 +00002322 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendlingce370cf2011-10-07 21:25:38 +00002323 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2324 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00002325 Op.getOperand(1), Val);
2326}
2327
2328SDValue
Jim Grosbach5eb19512010-05-22 01:06:18 +00002329ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2330 DebugLoc dl = Op.getDebugLoc();
2331 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2332 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2333}
2334
2335SDValue
Jim Grosbacha87ded22010-02-08 23:22:00 +00002336ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002337 const ARMSubtarget *Subtarget) const {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002338 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Jim Grosbach0e0da732009-05-12 23:59:14 +00002339 DebugLoc dl = Op.getDebugLoc();
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002340 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002341 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson916afdb2009-08-04 00:25:01 +00002342 case Intrinsic::arm_thread_pointer: {
Owen Andersone50ed302009-08-10 22:56:29 +00002343 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson916afdb2009-08-04 00:25:01 +00002344 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2345 }
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002346 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002347 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenge7e0d622009-11-06 22:24:13 +00002348 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5de5d4b2011-01-17 08:03:18 +00002349 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002350 EVT PtrVT = getPointerTy();
2351 DebugLoc dl = Op.getDebugLoc();
2352 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2353 SDValue CPAddr;
2354 unsigned PCAdj = (RelocM != Reloc::PIC_)
2355 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002356 ARMConstantPoolValue *CPV =
Bill Wendling5bb77992011-10-01 08:00:54 +00002357 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2358 ARMCP::CPLSDA, PCAdj);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002359 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002360 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002361 SDValue Result =
Evan Cheng9eda6892009-10-31 03:39:36 +00002362 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002363 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002364 false, false, false, 0);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002365
2366 if (RelocM == Reloc::PIC_) {
Evan Chenge7e0d622009-11-06 22:24:13 +00002367 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach1b747ad2009-08-11 00:09:57 +00002368 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2369 }
2370 return Result;
2371 }
Evan Cheng92e39162011-03-29 23:06:19 +00002372 case Intrinsic::arm_neon_vmulls:
2373 case Intrinsic::arm_neon_vmullu: {
2374 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2375 ? ARMISD::VMULLs : ARMISD::VMULLu;
2376 return DAG.getNode(NewOpc, Op.getDebugLoc(), Op.getValueType(),
2377 Op.getOperand(1), Op.getOperand(2));
2378 }
Lauro Ramos Venancioe0cb36b2007-11-08 17:20:05 +00002379 }
2380}
2381
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00002382static SDValue LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG,
Jim Grosbach7616b642010-06-16 23:45:49 +00002383 const ARMSubtarget *Subtarget) {
Jim Grosbach3728e962009-12-10 00:11:09 +00002384 DebugLoc dl = Op.getDebugLoc();
Bob Wilsonf74a4292010-10-30 00:54:37 +00002385 if (!Subtarget->hasDataBarrier()) {
2386 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2387 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2388 // here.
Bob Wilson54f92562010-11-09 22:50:44 +00002389 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Evan Cheng11db0682010-08-11 06:22:01 +00002390 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Bob Wilsonf74a4292010-10-30 00:54:37 +00002391 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Jim Grosbachc73993b2010-06-17 01:37:00 +00002392 DAG.getConstant(0, MVT::i32));
Evan Cheng11db0682010-08-11 06:22:01 +00002393 }
Bob Wilsonf74a4292010-10-30 00:54:37 +00002394
2395 SDValue Op5 = Op.getOperand(5);
2396 bool isDeviceBarrier = cast<ConstantSDNode>(Op5)->getZExtValue() != 0;
2397 unsigned isLL = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2398 unsigned isLS = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2399 bool isOnlyStoreBarrier = (isLL == 0 && isLS == 0);
2400
2401 ARM_MB::MemBOpt DMBOpt;
2402 if (isDeviceBarrier)
2403 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ST : ARM_MB::SY;
2404 else
2405 DMBOpt = isOnlyStoreBarrier ? ARM_MB::ISHST : ARM_MB::ISH;
2406 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2407 DAG.getConstant(DMBOpt, MVT::i32));
Jim Grosbach3728e962009-12-10 00:11:09 +00002408}
2409
Eli Friedman26689ac2011-08-03 21:06:02 +00002410
2411static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2412 const ARMSubtarget *Subtarget) {
2413 // FIXME: handle "fence singlethread" more efficiently.
2414 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14648462011-07-27 22:21:52 +00002415 if (!Subtarget->hasDataBarrier()) {
2416 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2417 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2418 // here.
2419 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2420 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
Eli Friedman26689ac2011-08-03 21:06:02 +00002421 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00002422 DAG.getConstant(0, MVT::i32));
2423 }
2424
Eli Friedman26689ac2011-08-03 21:06:02 +00002425 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
Eli Friedman989f61e2011-08-02 22:44:16 +00002426 DAG.getConstant(ARM_MB::ISH, MVT::i32));
Eli Friedman14648462011-07-27 22:21:52 +00002427}
2428
Evan Chengdfed19f2010-11-03 06:34:55 +00002429static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2430 const ARMSubtarget *Subtarget) {
2431 // ARM pre v5TE and Thumb1 does not have preload instructions.
2432 if (!(Subtarget->isThumb2() ||
2433 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2434 // Just preserve the chain.
2435 return Op.getOperand(0);
2436
2437 DebugLoc dl = Op.getDebugLoc();
Evan Cheng416941d2010-11-04 05:19:35 +00002438 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2439 if (!isRead &&
2440 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2441 // ARMv7 with MP extension has PLDW.
2442 return Op.getOperand(0);
Evan Chengdfed19f2010-11-03 06:34:55 +00002443
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002444 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2445 if (Subtarget->isThumb()) {
Evan Chengdfed19f2010-11-03 06:34:55 +00002446 // Invert the bits.
Evan Cheng416941d2010-11-04 05:19:35 +00002447 isRead = ~isRead & 1;
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00002448 isData = ~isData & 1;
2449 }
Evan Chengdfed19f2010-11-03 06:34:55 +00002450
2451 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng416941d2010-11-04 05:19:35 +00002452 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2453 DAG.getConstant(isData, MVT::i32));
Evan Chengdfed19f2010-11-03 06:34:55 +00002454}
2455
Dan Gohman1e93df62010-04-17 14:41:14 +00002456static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2457 MachineFunction &MF = DAG.getMachineFunction();
2458 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2459
Evan Chenga8e29892007-01-19 07:51:42 +00002460 // vastart just stores the address of the VarArgsFrameIndex slot into the
2461 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002462 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00002463 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00002464 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00002465 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002466 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2467 MachinePointerInfo(SV), false, false, 0);
Evan Chenga8e29892007-01-19 07:51:42 +00002468}
2469
Dan Gohman475871a2008-07-27 21:46:04 +00002470SDValue
Bob Wilson5bafff32009-06-22 23:27:02 +00002471ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2472 SDValue &Root, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002473 DebugLoc dl) const {
Bob Wilson5bafff32009-06-22 23:27:02 +00002474 MachineFunction &MF = DAG.getMachineFunction();
2475 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2476
Craig Topper44d23822012-02-22 05:59:10 +00002477 const TargetRegisterClass *RC;
David Goodwinf1daf7d2009-07-08 23:10:31 +00002478 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002479 RC = &ARM::tGPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002480 else
Craig Topper420761a2012-04-20 07:30:17 +00002481 RC = &ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002482
2483 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002484 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002485 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002486
2487 SDValue ArgValue2;
2488 if (NextVA.isMemLoc()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002489 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chenged2ae132010-07-03 00:40:23 +00002490 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson5bafff32009-06-22 23:27:02 +00002491
2492 // Create load node to retrieve arguments from the stack.
2493 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng9eda6892009-10-31 03:39:36 +00002494 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002495 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002496 false, false, false, 0);
Bob Wilson5bafff32009-06-22 23:27:02 +00002497 } else {
Devang Patel68e6bee2011-02-21 23:21:26 +00002498 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson825b72b2009-08-11 20:47:22 +00002499 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00002500 }
2501
Jim Grosbache5165492009-11-09 00:11:35 +00002502 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson5bafff32009-06-22 23:27:02 +00002503}
2504
Stuart Hastingsc7315872011-04-20 16:47:52 +00002505void
2506ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2507 unsigned &VARegSize, unsigned &VARegSaveSize)
2508 const {
2509 unsigned NumGPRs;
2510 if (CCInfo.isFirstByValRegValid())
2511 NumGPRs = ARM::R4 - CCInfo.getFirstByValReg();
2512 else {
2513 unsigned int firstUnalloced;
2514 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2515 sizeof(GPRArgRegs) /
2516 sizeof(GPRArgRegs[0]));
2517 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2518 }
2519
2520 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2521 VARegSize = NumGPRs * 4;
2522 VARegSaveSize = (VARegSize + Align - 1) & ~(Align - 1);
2523}
2524
2525// The remaining GPRs hold either the beginning of variable-argument
2526// data, or the beginning of an aggregate passed by value (usuall
2527// byval). Either way, we allocate stack slots adjacent to the data
2528// provided by our caller, and store the unallocated registers there.
2529// If this is a variadic function, the va_list pointer will begin with
2530// these values; otherwise, this reassembles a (byval) structure that
2531// was split between registers and memory.
2532void
2533ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2534 DebugLoc dl, SDValue &Chain,
2535 unsigned ArgOffset) const {
2536 MachineFunction &MF = DAG.getMachineFunction();
2537 MachineFrameInfo *MFI = MF.getFrameInfo();
2538 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2539 unsigned firstRegToSaveIndex;
2540 if (CCInfo.isFirstByValRegValid())
2541 firstRegToSaveIndex = CCInfo.getFirstByValReg() - ARM::R0;
2542 else {
2543 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2544 (GPRArgRegs, sizeof(GPRArgRegs) / sizeof(GPRArgRegs[0]));
2545 }
2546
2547 unsigned VARegSize, VARegSaveSize;
2548 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2549 if (VARegSaveSize) {
2550 // If this function is vararg, store any remaining integer argument regs
2551 // to their spots on the stack so that they may be loaded by deferencing
2552 // the result of va_next.
2553 AFI->setVarArgsRegSaveSize(VARegSaveSize);
Eric Christopher5ac179c2011-04-29 23:12:01 +00002554 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(VARegSaveSize,
2555 ArgOffset + VARegSaveSize
2556 - VARegSize,
Stuart Hastingsc7315872011-04-20 16:47:52 +00002557 false));
2558 SDValue FIN = DAG.getFrameIndex(AFI->getVarArgsFrameIndex(),
2559 getPointerTy());
2560
2561 SmallVector<SDValue, 4> MemOps;
2562 for (; firstRegToSaveIndex < 4; ++firstRegToSaveIndex) {
Craig Topper44d23822012-02-22 05:59:10 +00002563 const TargetRegisterClass *RC;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002564 if (AFI->isThumb1OnlyFunction())
Craig Topper420761a2012-04-20 07:30:17 +00002565 RC = &ARM::tGPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002566 else
Craig Topper420761a2012-04-20 07:30:17 +00002567 RC = &ARM::GPRRegClass;
Stuart Hastingsc7315872011-04-20 16:47:52 +00002568
2569 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2570 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2571 SDValue Store =
2572 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Eric Christopher5ac179c2011-04-29 23:12:01 +00002573 MachinePointerInfo::getFixedStack(AFI->getVarArgsFrameIndex()),
Stuart Hastingsc7315872011-04-20 16:47:52 +00002574 false, false, 0);
2575 MemOps.push_back(Store);
2576 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2577 DAG.getConstant(4, getPointerTy()));
2578 }
2579 if (!MemOps.empty())
2580 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2581 &MemOps[0], MemOps.size());
2582 } else
2583 // This will point to the next argument passed via stack.
2584 AFI->setVarArgsFrameIndex(MFI->CreateFixedObject(4, ArgOffset, true));
2585}
2586
Bob Wilson5bafff32009-06-22 23:27:02 +00002587SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002588ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002589 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002590 const SmallVectorImpl<ISD::InputArg>
2591 &Ins,
2592 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002593 SmallVectorImpl<SDValue> &InVals)
2594 const {
Bob Wilson1f595bb2009-04-17 19:07:39 +00002595 MachineFunction &MF = DAG.getMachineFunction();
2596 MachineFrameInfo *MFI = MF.getFrameInfo();
2597
Bob Wilson1f595bb2009-04-17 19:07:39 +00002598 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2599
2600 // Assign locations to all of the incoming arguments.
2601 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwaricha86686e2011-06-10 20:59:24 +00002602 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2603 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002604 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002605 CCAssignFnForNode(CallConv, /* Return*/ false,
2606 isVarArg));
Bob Wilson1f595bb2009-04-17 19:07:39 +00002607
2608 SmallVector<SDValue, 16> ArgValues;
Stuart Hastingsf222e592011-02-28 17:17:53 +00002609 int lastInsIndex = -1;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002610
Stuart Hastingsf222e592011-02-28 17:17:53 +00002611 SDValue ArgValue;
Bob Wilson1f595bb2009-04-17 19:07:39 +00002612 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2613 CCValAssign &VA = ArgLocs[i];
2614
Bob Wilsondee46d72009-04-17 20:35:10 +00002615 // Arguments stored in registers.
Bob Wilson1f595bb2009-04-17 19:07:39 +00002616 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002617 EVT RegVT = VA.getLocVT();
Bob Wilson1f595bb2009-04-17 19:07:39 +00002618
Bob Wilson1f595bb2009-04-17 19:07:39 +00002619 if (VA.needsCustom()) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002620 // f64 and vector types are split up into multiple registers or
2621 // combinations of registers and stack slots.
Owen Anderson825b72b2009-08-11 20:47:22 +00002622 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson5bafff32009-06-22 23:27:02 +00002623 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohman98ca4f22009-08-05 01:29:28 +00002624 Chain, DAG, dl);
Bob Wilson5bafff32009-06-22 23:27:02 +00002625 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson6a234f02010-04-13 22:03:22 +00002626 SDValue ArgValue2;
2627 if (VA.isMemLoc()) {
Evan Chenged2ae132010-07-03 00:40:23 +00002628 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson6a234f02010-04-13 22:03:22 +00002629 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2630 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002631 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002632 false, false, false, 0);
Bob Wilson6a234f02010-04-13 22:03:22 +00002633 } else {
2634 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2635 Chain, DAG, dl);
2636 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002637 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2638 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002639 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00002640 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson5bafff32009-06-22 23:27:02 +00002641 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2642 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002643 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002644
Bob Wilson5bafff32009-06-22 23:27:02 +00002645 } else {
Craig Topper44d23822012-02-22 05:59:10 +00002646 const TargetRegisterClass *RC;
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00002647
Owen Anderson825b72b2009-08-11 20:47:22 +00002648 if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002649 RC = &ARM::SPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002650 else if (RegVT == MVT::f64)
Craig Topper420761a2012-04-20 07:30:17 +00002651 RC = &ARM::DPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002652 else if (RegVT == MVT::v2f64)
Craig Topper420761a2012-04-20 07:30:17 +00002653 RC = &ARM::QPRRegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 else if (RegVT == MVT::i32)
Craig Topper420761a2012-04-20 07:30:17 +00002655 RC = AFI->isThumb1OnlyFunction() ?
2656 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2657 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson5bafff32009-06-22 23:27:02 +00002658 else
Anton Korobeynikov058c2512009-08-05 20:15:19 +00002659 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson5bafff32009-06-22 23:27:02 +00002660
2661 // Transform the arguments in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002662 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002663 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002664 }
2665
2666 // If this is an 8 or 16-bit value, it is really passed promoted
2667 // to 32 bits. Insert an assert[sz]ext to capture this, then
2668 // truncate to the right size.
2669 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002670 default: llvm_unreachable("Unknown loc info!");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002671 case CCValAssign::Full: break;
2672 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002673 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002674 break;
2675 case CCValAssign::SExt:
2676 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2677 DAG.getValueType(VA.getValVT()));
2678 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2679 break;
2680 case CCValAssign::ZExt:
2681 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2682 DAG.getValueType(VA.getValVT()));
2683 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2684 break;
2685 }
2686
Dan Gohman98ca4f22009-08-05 01:29:28 +00002687 InVals.push_back(ArgValue);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002688
2689 } else { // VA.isRegLoc()
2690
2691 // sanity check
2692 assert(VA.isMemLoc());
Owen Anderson825b72b2009-08-11 20:47:22 +00002693 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilson1f595bb2009-04-17 19:07:39 +00002694
Stuart Hastingsf222e592011-02-28 17:17:53 +00002695 int index = ArgLocs[i].getValNo();
Owen Anderson76706012011-04-05 21:48:57 +00002696
Stuart Hastingsf222e592011-02-28 17:17:53 +00002697 // Some Ins[] entries become multiple ArgLoc[] entries.
2698 // Process them only once.
2699 if (index != lastInsIndex)
2700 {
2701 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher471e4222011-06-08 23:55:35 +00002702 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christopher5ac179c2011-04-29 23:12:01 +00002703 // This can be changed with more analysis.
2704 // In case of tail call optimization mark all arguments mutable.
2705 // Since they could be overwritten by lowering of arguments in case of
2706 // a tail call.
Stuart Hastingsf222e592011-02-28 17:17:53 +00002707 if (Flags.isByVal()) {
Stuart Hastingsc7315872011-04-20 16:47:52 +00002708 unsigned VARegSize, VARegSaveSize;
2709 computeRegArea(CCInfo, MF, VARegSize, VARegSaveSize);
2710 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, 0);
2711 unsigned Bytes = Flags.getByValSize() - VARegSize;
Evan Chengee2e0e32011-03-30 23:44:13 +00002712 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
Stuart Hastingsc7315872011-04-20 16:47:52 +00002713 int FI = MFI->CreateFixedObject(Bytes,
2714 VA.getLocMemOffset(), false);
Stuart Hastingsf222e592011-02-28 17:17:53 +00002715 InVals.push_back(DAG.getFrameIndex(FI, getPointerTy()));
2716 } else {
2717 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
2718 VA.getLocMemOffset(), true);
Bob Wilson1f595bb2009-04-17 19:07:39 +00002719
Stuart Hastingsf222e592011-02-28 17:17:53 +00002720 // Create load nodes to retrieve arguments from the stack.
2721 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2722 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2723 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002724 false, false, false, 0));
Stuart Hastingsf222e592011-02-28 17:17:53 +00002725 }
2726 lastInsIndex = index;
2727 }
Bob Wilson1f595bb2009-04-17 19:07:39 +00002728 }
2729 }
2730
2731 // varargs
Stuart Hastingsc7315872011-04-20 16:47:52 +00002732 if (isVarArg)
2733 VarArgStyleRegisters(CCInfo, DAG, dl, Chain, CCInfo.getNextStackOffset());
Evan Chenga8e29892007-01-19 07:51:42 +00002734
Dan Gohman98ca4f22009-08-05 01:29:28 +00002735 return Chain;
Evan Chenga8e29892007-01-19 07:51:42 +00002736}
2737
2738/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002739static bool isFloatingPointZero(SDValue Op) {
Evan Chenga8e29892007-01-19 07:51:42 +00002740 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002741 return CFP->getValueAPF().isPosZero();
Gabor Greifba36cb52008-08-28 21:40:38 +00002742 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Chenga8e29892007-01-19 07:51:42 +00002743 // Maybe this has already been legalized into the constant pool?
2744 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman475871a2008-07-27 21:46:04 +00002745 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00002746 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohman46510a72010-04-15 01:51:59 +00002747 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +00002748 return CFP->getValueAPF().isPosZero();
Evan Chenga8e29892007-01-19 07:51:42 +00002749 }
2750 }
2751 return false;
2752}
2753
Evan Chenga8e29892007-01-19 07:51:42 +00002754/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
2755/// the given operands.
Evan Cheng06b53c02009-11-12 07:13:11 +00002756SDValue
2757ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng218977b2010-07-13 19:27:42 +00002758 SDValue &ARMcc, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002759 DebugLoc dl) const {
Gabor Greifba36cb52008-08-28 21:40:38 +00002760 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002761 unsigned C = RHSC->getZExtValue();
Evan Cheng06b53c02009-11-12 07:13:11 +00002762 if (!isLegalICmpImmediate(C)) {
Evan Chenga8e29892007-01-19 07:51:42 +00002763 // Constant does not fit, try adjusting it by one?
2764 switch (CC) {
2765 default: break;
2766 case ISD::SETLT:
Evan Chenga8e29892007-01-19 07:51:42 +00002767 case ISD::SETGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002768 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002769 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002770 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002771 }
2772 break;
2773 case ISD::SETULT:
2774 case ISD::SETUGE:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002775 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002776 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson825b72b2009-08-11 20:47:22 +00002777 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002778 }
2779 break;
2780 case ISD::SETLE:
Evan Chenga8e29892007-01-19 07:51:42 +00002781 case ISD::SETGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002782 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002783 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002784 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng9a2ef952007-02-02 01:53:26 +00002785 }
2786 break;
2787 case ISD::SETULE:
2788 case ISD::SETUGT:
Daniel Dunbar3cc32832010-08-25 16:58:05 +00002789 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng9a2ef952007-02-02 01:53:26 +00002790 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson825b72b2009-08-11 20:47:22 +00002791 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002792 }
2793 break;
2794 }
2795 }
2796 }
2797
2798 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002799 ARMISD::NodeType CompareType;
2800 switch (CondCode) {
2801 default:
2802 CompareType = ARMISD::CMP;
2803 break;
2804 case ARMCC::EQ:
2805 case ARMCC::NE:
David Goodwinc0309b42009-06-29 15:33:01 +00002806 // Uses only Z Flag
2807 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio99966632007-04-02 01:30:03 +00002808 break;
2809 }
Evan Cheng218977b2010-07-13 19:27:42 +00002810 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002811 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002812}
2813
2814/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng515fe3a2010-07-08 02:08:50 +00002815SDValue
Evan Cheng218977b2010-07-13 19:27:42 +00002816ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Evan Cheng515fe3a2010-07-08 02:08:50 +00002817 DebugLoc dl) const {
Dan Gohman475871a2008-07-27 21:46:04 +00002818 SDValue Cmp;
Evan Chenga8e29892007-01-19 07:51:42 +00002819 if (!isFloatingPointZero(RHS))
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002820 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Chenga8e29892007-01-19 07:51:42 +00002821 else
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002822 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
2823 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002824}
2825
Bob Wilson79f56c92011-03-08 01:17:20 +00002826/// duplicateCmp - Glue values can have only one use, so this function
2827/// duplicates a comparison node.
2828SDValue
2829ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
2830 unsigned Opc = Cmp.getOpcode();
2831 DebugLoc DL = Cmp.getDebugLoc();
2832 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
2833 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2834
2835 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
2836 Cmp = Cmp.getOperand(0);
2837 Opc = Cmp.getOpcode();
2838 if (Opc == ARMISD::CMPFP)
2839 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
2840 else {
2841 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
2842 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
2843 }
2844 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
2845}
2846
Bill Wendlingde2b1512010-08-11 08:43:16 +00002847SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2848 SDValue Cond = Op.getOperand(0);
2849 SDValue SelectTrue = Op.getOperand(1);
2850 SDValue SelectFalse = Op.getOperand(2);
2851 DebugLoc dl = Op.getDebugLoc();
2852
2853 // Convert:
2854 //
2855 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
2856 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
2857 //
2858 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
2859 const ConstantSDNode *CMOVTrue =
2860 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
2861 const ConstantSDNode *CMOVFalse =
2862 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2863
2864 if (CMOVTrue && CMOVFalse) {
2865 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
2866 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
2867
2868 SDValue True;
2869 SDValue False;
2870 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
2871 True = SelectTrue;
2872 False = SelectFalse;
2873 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
2874 True = SelectFalse;
2875 False = SelectTrue;
2876 }
2877
2878 if (True.getNode() && False.getNode()) {
Evan Chengb936e302011-05-18 18:59:17 +00002879 EVT VT = Op.getValueType();
Bill Wendlingde2b1512010-08-11 08:43:16 +00002880 SDValue ARMcc = Cond.getOperand(2);
2881 SDValue CCR = Cond.getOperand(3);
Bob Wilson79f56c92011-03-08 01:17:20 +00002882 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Chengb936e302011-05-18 18:59:17 +00002883 assert(True.getValueType() == VT);
2884 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendlingde2b1512010-08-11 08:43:16 +00002885 }
2886 }
2887 }
2888
Dan Gohmandb953892012-02-24 00:09:36 +00002889 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
2890 // undefined bits before doing a full-word comparison with zero.
2891 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
2892 DAG.getConstant(1, Cond.getValueType()));
2893
Bill Wendlingde2b1512010-08-11 08:43:16 +00002894 return DAG.getSelectCC(dl, Cond,
2895 DAG.getConstant(0, Cond.getValueType()),
2896 SelectTrue, SelectFalse, ISD::SETNE);
2897}
2898
Dan Gohmand858e902010-04-17 15:26:15 +00002899SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002900 EVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002901 SDValue LHS = Op.getOperand(0);
2902 SDValue RHS = Op.getOperand(1);
Evan Chenga8e29892007-01-19 07:51:42 +00002903 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00002904 SDValue TrueVal = Op.getOperand(2);
2905 SDValue FalseVal = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00002906 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00002907
Owen Anderson825b72b2009-08-11 20:47:22 +00002908 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00002909 SDValue ARMcc;
Owen Anderson825b72b2009-08-11 20:47:22 +00002910 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00002911 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Jim Grosbachb04546f2011-09-13 20:30:37 +00002912 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002913 }
2914
2915 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00002916 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Chenga8e29892007-01-19 07:51:42 +00002917
Evan Cheng218977b2010-07-13 19:27:42 +00002918 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
2919 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00002920 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesende064702009-02-06 21:50:26 +00002921 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng218977b2010-07-13 19:27:42 +00002922 ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00002923 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00002924 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Chenga8e29892007-01-19 07:51:42 +00002925 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng218977b2010-07-13 19:27:42 +00002926 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson2dc4f542009-03-20 22:42:55 +00002927 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng218977b2010-07-13 19:27:42 +00002928 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Chenga8e29892007-01-19 07:51:42 +00002929 }
2930 return Result;
2931}
2932
Evan Cheng218977b2010-07-13 19:27:42 +00002933/// canChangeToInt - Given the fp compare operand, return true if it is suitable
2934/// to morph to an integer compare sequence.
2935static bool canChangeToInt(SDValue Op, bool &SeenZero,
2936 const ARMSubtarget *Subtarget) {
2937 SDNode *N = Op.getNode();
2938 if (!N->hasOneUse())
2939 // Otherwise it requires moving the value from fp to integer registers.
2940 return false;
2941 if (!N->getNumValues())
2942 return false;
2943 EVT VT = Op.getValueType();
2944 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
2945 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
2946 // vmrs are very slow, e.g. cortex-a8.
2947 return false;
2948
2949 if (isFloatingPointZero(Op)) {
2950 SeenZero = true;
2951 return true;
2952 }
2953 return ISD::isNormalLoad(N);
2954}
2955
2956static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
2957 if (isFloatingPointZero(Op))
2958 return DAG.getConstant(0, MVT::i32);
2959
2960 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
2961 return DAG.getLoad(MVT::i32, Op.getDebugLoc(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002962 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002963 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002964 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002965
2966 llvm_unreachable("Unknown VFP cmp argument!");
2967}
2968
2969static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
2970 SDValue &RetVal1, SDValue &RetVal2) {
2971 if (isFloatingPointZero(Op)) {
2972 RetVal1 = DAG.getConstant(0, MVT::i32);
2973 RetVal2 = DAG.getConstant(0, MVT::i32);
2974 return;
2975 }
2976
2977 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
2978 SDValue Ptr = Ld->getBasePtr();
2979 RetVal1 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2980 Ld->getChain(), Ptr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002981 Ld->getPointerInfo(),
Evan Cheng218977b2010-07-13 19:27:42 +00002982 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002983 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng218977b2010-07-13 19:27:42 +00002984
2985 EVT PtrType = Ptr.getValueType();
2986 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
2987 SDValue NewPtr = DAG.getNode(ISD::ADD, Op.getDebugLoc(),
2988 PtrType, Ptr, DAG.getConstant(4, PtrType));
2989 RetVal2 = DAG.getLoad(MVT::i32, Op.getDebugLoc(),
2990 Ld->getChain(), NewPtr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002991 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng218977b2010-07-13 19:27:42 +00002992 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002993 Ld->isInvariant(), NewAlign);
Evan Cheng218977b2010-07-13 19:27:42 +00002994 return;
2995 }
2996
2997 llvm_unreachable("Unknown VFP cmp argument!");
2998}
2999
3000/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3001/// f32 and even f64 comparisons to integer ones.
3002SDValue
3003ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3004 SDValue Chain = Op.getOperand(0);
Evan Chenga8e29892007-01-19 07:51:42 +00003005 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng218977b2010-07-13 19:27:42 +00003006 SDValue LHS = Op.getOperand(2);
3007 SDValue RHS = Op.getOperand(3);
3008 SDValue Dest = Op.getOperand(4);
3009 DebugLoc dl = Op.getDebugLoc();
3010
Evan Chengfc501a32012-03-01 23:27:13 +00003011 bool LHSSeenZero = false;
3012 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3013 bool RHSSeenZero = false;
3014 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3015 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson1b772f92011-03-08 01:17:16 +00003016 // If unsafe fp math optimization is enabled and there are no other uses of
3017 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng218977b2010-07-13 19:27:42 +00003018 // to an integer comparison.
3019 if (CC == ISD::SETOEQ)
3020 CC = ISD::SETEQ;
3021 else if (CC == ISD::SETUNE)
3022 CC = ISD::SETNE;
3023
Evan Chengfc501a32012-03-01 23:27:13 +00003024 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng218977b2010-07-13 19:27:42 +00003025 SDValue ARMcc;
3026 if (LHS.getValueType() == MVT::f32) {
Evan Chengfc501a32012-03-01 23:27:13 +00003027 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3028 bitcastf32Toi32(LHS, DAG), Mask);
3029 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3030 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003031 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3032 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3033 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3034 Chain, Dest, ARMcc, CCR, Cmp);
3035 }
3036
3037 SDValue LHS1, LHS2;
3038 SDValue RHS1, RHS2;
3039 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3040 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengfc501a32012-03-01 23:27:13 +00003041 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3042 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng218977b2010-07-13 19:27:42 +00003043 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3044 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003045 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003046 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3047 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3048 }
3049
3050 return SDValue();
3051}
3052
3053SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3054 SDValue Chain = Op.getOperand(0);
3055 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3056 SDValue LHS = Op.getOperand(2);
3057 SDValue RHS = Op.getOperand(3);
3058 SDValue Dest = Op.getOperand(4);
Dale Johannesende064702009-02-06 21:50:26 +00003059 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003060
Owen Anderson825b72b2009-08-11 20:47:22 +00003061 if (LHS.getValueType() == MVT::i32) {
Evan Cheng218977b2010-07-13 19:27:42 +00003062 SDValue ARMcc;
3063 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003064 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +00003065 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng218977b2010-07-13 19:27:42 +00003066 Chain, Dest, ARMcc, CCR, Cmp);
Evan Chenga8e29892007-01-19 07:51:42 +00003067 }
3068
Owen Anderson825b72b2009-08-11 20:47:22 +00003069 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng218977b2010-07-13 19:27:42 +00003070
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003071 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng218977b2010-07-13 19:27:42 +00003072 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3073 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3074 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3075 if (Result.getNode())
3076 return Result;
3077 }
3078
Evan Chenga8e29892007-01-19 07:51:42 +00003079 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsoncd3b9a42009-09-09 23:14:54 +00003080 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003081
Evan Cheng218977b2010-07-13 19:27:42 +00003082 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3083 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003084 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003085 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng218977b2010-07-13 19:27:42 +00003086 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Dale Johannesende064702009-02-06 21:50:26 +00003087 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003088 if (CondCode2 != ARMCC::AL) {
Evan Cheng218977b2010-07-13 19:27:42 +00003089 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3090 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Dale Johannesende064702009-02-06 21:50:26 +00003091 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
Evan Chenga8e29892007-01-19 07:51:42 +00003092 }
3093 return Res;
3094}
3095
Dan Gohmand858e902010-04-17 15:26:15 +00003096SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00003097 SDValue Chain = Op.getOperand(0);
3098 SDValue Table = Op.getOperand(1);
3099 SDValue Index = Op.getOperand(2);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003100 DebugLoc dl = Op.getDebugLoc();
Evan Chenga8e29892007-01-19 07:51:42 +00003101
Owen Andersone50ed302009-08-10 22:56:29 +00003102 EVT PTy = getPointerTy();
Evan Chenga8e29892007-01-19 07:51:42 +00003103 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3104 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3eadf002009-07-14 18:44:34 +00003105 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman475871a2008-07-27 21:46:04 +00003106 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson825b72b2009-08-11 20:47:22 +00003107 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chenge7c329b2009-07-28 20:53:24 +00003108 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3109 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Cheng66ac5312009-07-25 00:33:29 +00003110 if (Subtarget->isThumb2()) {
3111 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3112 // which does another jump to the destination. This also makes it easier
3113 // to translate it to TBB / TBH later.
3114 // FIXME: This might not work if the function is extremely large.
Owen Anderson825b72b2009-08-11 20:47:22 +00003115 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Cheng5657c012009-07-29 02:18:14 +00003116 Addr, Op.getOperand(2), JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003117 }
Evan Cheng66ac5312009-07-25 00:33:29 +00003118 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng9eda6892009-10-31 03:39:36 +00003119 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003120 MachinePointerInfo::getJumpTable(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003121 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003122 Chain = Addr.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003123 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003125 } else {
Evan Cheng9eda6892009-10-31 03:39:36 +00003126 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003127 MachinePointerInfo::getJumpTable(),
3128 false, false, false, 0);
Evan Cheng66ac5312009-07-25 00:33:29 +00003129 Chain = Addr.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Cheng66ac5312009-07-25 00:33:29 +00003131 }
Evan Chenga8e29892007-01-19 07:51:42 +00003132}
3133
Eli Friedman14e809c2011-11-09 23:36:02 +00003134static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy873fd5f2012-02-20 09:24:05 +00003135 EVT VT = Op.getValueType();
3136 DebugLoc dl = Op.getDebugLoc();
Eli Friedman14e809c2011-11-09 23:36:02 +00003137
James Molloy873fd5f2012-02-20 09:24:05 +00003138 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3139 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3140 return Op;
3141 return DAG.UnrollVectorOp(Op.getNode());
3142 }
3143
3144 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3145 "Invalid type for custom lowering!");
3146 if (VT != MVT::v4i16)
3147 return DAG.UnrollVectorOp(Op.getNode());
3148
3149 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3150 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman14e809c2011-11-09 23:36:02 +00003151}
3152
Bob Wilson76a312b2010-03-19 22:51:32 +00003153static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman14e809c2011-11-09 23:36:02 +00003154 EVT VT = Op.getValueType();
3155 if (VT.isVector())
3156 return LowerVectorFP_TO_INT(Op, DAG);
3157
Bob Wilson76a312b2010-03-19 22:51:32 +00003158 DebugLoc dl = Op.getDebugLoc();
3159 unsigned Opc;
3160
3161 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003162 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003163 case ISD::FP_TO_SINT:
3164 Opc = ARMISD::FTOSI;
3165 break;
3166 case ISD::FP_TO_UINT:
3167 Opc = ARMISD::FTOUI;
3168 break;
3169 }
3170 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003171 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilson76a312b2010-03-19 22:51:32 +00003172}
3173
Cameron Zwarich3007d332011-03-29 21:41:55 +00003174static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3175 EVT VT = Op.getValueType();
3176 DebugLoc dl = Op.getDebugLoc();
3177
Eli Friedman14e809c2011-11-09 23:36:02 +00003178 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3179 if (VT.getVectorElementType() == MVT::f32)
3180 return Op;
3181 return DAG.UnrollVectorOp(Op.getNode());
3182 }
3183
Duncan Sands1f6a3292011-08-12 14:54:45 +00003184 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3185 "Invalid type for custom lowering!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003186 if (VT != MVT::v4f32)
3187 return DAG.UnrollVectorOp(Op.getNode());
3188
3189 unsigned CastOpc;
3190 unsigned Opc;
3191 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003192 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich3007d332011-03-29 21:41:55 +00003193 case ISD::SINT_TO_FP:
3194 CastOpc = ISD::SIGN_EXTEND;
3195 Opc = ISD::SINT_TO_FP;
3196 break;
3197 case ISD::UINT_TO_FP:
3198 CastOpc = ISD::ZERO_EXTEND;
3199 Opc = ISD::UINT_TO_FP;
3200 break;
3201 }
3202
3203 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3204 return DAG.getNode(Opc, dl, VT, Op);
3205}
3206
Bob Wilson76a312b2010-03-19 22:51:32 +00003207static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3208 EVT VT = Op.getValueType();
Cameron Zwarich3007d332011-03-29 21:41:55 +00003209 if (VT.isVector())
3210 return LowerVectorINT_TO_FP(Op, DAG);
3211
Bob Wilson76a312b2010-03-19 22:51:32 +00003212 DebugLoc dl = Op.getDebugLoc();
3213 unsigned Opc;
3214
3215 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00003216 default: llvm_unreachable("Invalid opcode!");
Bob Wilson76a312b2010-03-19 22:51:32 +00003217 case ISD::SINT_TO_FP:
3218 Opc = ARMISD::SITOF;
3219 break;
3220 case ISD::UINT_TO_FP:
3221 Opc = ARMISD::UITOF;
3222 break;
3223 }
3224
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003225 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilson76a312b2010-03-19 22:51:32 +00003226 return DAG.getNode(Opc, dl, VT, Op);
3227}
3228
Evan Cheng515fe3a2010-07-08 02:08:50 +00003229SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00003230 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman475871a2008-07-27 21:46:04 +00003231 SDValue Tmp0 = Op.getOperand(0);
3232 SDValue Tmp1 = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +00003233 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003234 EVT VT = Op.getValueType();
3235 EVT SrcVT = Tmp1.getValueType();
Evan Chenge573fb32011-02-23 02:24:55 +00003236 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3237 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3238 bool UseNEON = !InGPR && Subtarget->hasNEON();
3239
3240 if (UseNEON) {
3241 // Use VBSL to copy the sign bit.
3242 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3243 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3244 DAG.getTargetConstant(EncodedVal, MVT::i32));
3245 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3246 if (VT == MVT::f64)
3247 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3248 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3249 DAG.getConstant(32, MVT::i32));
3250 else /*if (VT == MVT::f32)*/
3251 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3252 if (SrcVT == MVT::f32) {
3253 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3254 if (VT == MVT::f64)
3255 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3256 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3257 DAG.getConstant(32, MVT::i32));
Evan Cheng9eec66e2011-04-15 01:31:00 +00003258 } else if (VT == MVT::f32)
3259 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3260 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3261 DAG.getConstant(32, MVT::i32));
Evan Chenge573fb32011-02-23 02:24:55 +00003262 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3263 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3264
3265 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3266 MVT::i32);
3267 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3268 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3269 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson76706012011-04-05 21:48:57 +00003270
Evan Chenge573fb32011-02-23 02:24:55 +00003271 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3272 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3273 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Chengc24ab5c2011-02-28 18:45:27 +00003274 if (VT == MVT::f32) {
Evan Chenge573fb32011-02-23 02:24:55 +00003275 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3276 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3277 DAG.getConstant(0, MVT::i32));
3278 } else {
3279 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3280 }
3281
3282 return Res;
3283 }
Evan Chengc143dd42011-02-11 02:28:55 +00003284
3285 // Bitcast operand 1 to i32.
3286 if (SrcVT == MVT::f64)
3287 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3288 &Tmp1, 1).getValue(1);
3289 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3290
Evan Chenge573fb32011-02-23 02:24:55 +00003291 // Or in the signbit with integer operations.
3292 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3293 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3294 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3295 if (VT == MVT::f32) {
3296 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3297 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3298 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3299 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Chengc143dd42011-02-11 02:28:55 +00003300 }
3301
Evan Chenge573fb32011-02-23 02:24:55 +00003302 // f64: Or the high part with signbit and then combine two parts.
3303 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3304 &Tmp0, 1);
3305 SDValue Lo = Tmp0.getValue(0);
3306 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3307 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3308 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Chenga8e29892007-01-19 07:51:42 +00003309}
3310
Evan Cheng2457f2c2010-05-22 01:47:14 +00003311SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3312 MachineFunction &MF = DAG.getMachineFunction();
3313 MachineFrameInfo *MFI = MF.getFrameInfo();
3314 MFI->setReturnAddressIsTaken(true);
3315
3316 EVT VT = Op.getValueType();
3317 DebugLoc dl = Op.getDebugLoc();
3318 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3319 if (Depth) {
3320 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3321 SDValue Offset = DAG.getConstant(4, MVT::i32);
3322 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3323 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003324 MachinePointerInfo(), false, false, false, 0);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003325 }
3326
3327 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patel68e6bee2011-02-21 23:21:26 +00003328 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng2457f2c2010-05-22 01:47:14 +00003329 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3330}
3331
Dan Gohmand858e902010-04-17 15:26:15 +00003332SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbach0e0da732009-05-12 23:59:14 +00003333 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3334 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00003335
Owen Andersone50ed302009-08-10 22:56:29 +00003336 EVT VT = Op.getValueType();
Jim Grosbach0e0da732009-05-12 23:59:14 +00003337 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
3338 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Chengcd828612009-06-18 23:14:30 +00003339 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
Jim Grosbach0e0da732009-05-12 23:59:14 +00003340 ? ARM::R7 : ARM::R11;
3341 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3342 while (Depth--)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003343 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3344 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003345 false, false, false, 0);
Jim Grosbach0e0da732009-05-12 23:59:14 +00003346 return FrameAddr;
3347}
3348
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003349/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson9f3f0612010-04-17 05:30:19 +00003350/// expand a bit convert where either the source or destination type is i64 to
3351/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3352/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3353/// vectors), since the legalizer won't know what to do with that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003354static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson9f3f0612010-04-17 05:30:19 +00003355 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3356 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003357 SDValue Op = N->getOperand(0);
Bob Wilson164cd8b2010-04-14 20:45:23 +00003358
Bob Wilson9f3f0612010-04-17 05:30:19 +00003359 // This function is only supposed to be called for i64 types, either as the
3360 // source or destination of the bit convert.
3361 EVT SrcVT = Op.getValueType();
3362 EVT DstVT = N->getValueType(0);
3363 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003364 "ExpandBITCAST called for non-i64 type");
Bob Wilson164cd8b2010-04-14 20:45:23 +00003365
Bob Wilson9f3f0612010-04-17 05:30:19 +00003366 // Turn i64->f64 into VMOVDRR.
3367 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003368 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3369 DAG.getConstant(0, MVT::i32));
3370 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3371 DAG.getConstant(1, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003372 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilson1114f562010-06-11 22:45:25 +00003373 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Chengc7c77292008-11-04 19:57:48 +00003374 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003375
Jim Grosbache5165492009-11-09 00:11:35 +00003376 // Turn f64->i64 into VMOVRRD.
Bob Wilson9f3f0612010-04-17 05:30:19 +00003377 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3378 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3379 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3380 // Merge the pieces into a single i64 value.
3381 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3382 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00003383
Bob Wilson9f3f0612010-04-17 05:30:19 +00003384 return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00003385}
3386
Bob Wilson5bafff32009-06-22 23:27:02 +00003387/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003388/// Zero vectors are used to represent vector negation and in those cases
3389/// will be implemented with the NEON VNEG instruction. However, VNEG does
3390/// not support i64 elements, so sometimes the zero vectors will need to be
3391/// explicitly constructed. Regardless, use a canonical VMOV to create the
3392/// zero vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003393static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003394 assert(VT.isVector() && "Expected a vector type");
Bob Wilsoncba270d2010-07-13 21:16:48 +00003395 // The canonical modified immediate encoding of a zero vector is....0!
3396 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3397 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3398 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003399 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson5bafff32009-06-22 23:27:02 +00003400}
3401
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003402/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3403/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003404SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3405 SelectionDAG &DAG) const {
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003406 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3407 EVT VT = Op.getValueType();
3408 unsigned VTBits = VT.getSizeInBits();
3409 DebugLoc dl = Op.getDebugLoc();
3410 SDValue ShOpLo = Op.getOperand(0);
3411 SDValue ShOpHi = Op.getOperand(1);
3412 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003413 SDValue ARMcc;
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003414 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003415
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003416 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3417
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003418 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3419 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3420 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3421 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3422 DAG.getConstant(VTBits, MVT::i32));
3423 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3424 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003425 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003426
3427 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3428 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003429 ARMcc, DAG, dl);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00003430 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003431 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbachb4a976c2009-10-31 21:00:56 +00003432 CCR, Cmp);
3433
3434 SDValue Ops[2] = { Lo, Hi };
3435 return DAG.getMergeValues(Ops, 2, dl);
3436}
3437
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003438/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3439/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00003440SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3441 SelectionDAG &DAG) const {
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003442 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3443 EVT VT = Op.getValueType();
3444 unsigned VTBits = VT.getSizeInBits();
3445 DebugLoc dl = Op.getDebugLoc();
3446 SDValue ShOpLo = Op.getOperand(0);
3447 SDValue ShOpHi = Op.getOperand(1);
3448 SDValue ShAmt = Op.getOperand(2);
Evan Cheng218977b2010-07-13 19:27:42 +00003449 SDValue ARMcc;
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003450
3451 assert(Op.getOpcode() == ISD::SHL_PARTS);
3452 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3453 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3454 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3455 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3456 DAG.getConstant(VTBits, MVT::i32));
3457 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3458 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3459
3460 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3461 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3462 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng218977b2010-07-13 19:27:42 +00003463 ARMcc, DAG, dl);
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003464 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng218977b2010-07-13 19:27:42 +00003465 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbachc2b879f2009-10-31 19:38:01 +00003466 CCR, Cmp);
3467
3468 SDValue Ops[2] = { Lo, Hi };
3469 return DAG.getMergeValues(Ops, 2, dl);
3470}
3471
Jim Grosbach4725ca72010-09-08 03:54:02 +00003472SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemand1fb5832010-08-03 21:31:55 +00003473 SelectionDAG &DAG) const {
3474 // The rounding mode is in bits 23:22 of the FPSCR.
3475 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3476 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3477 // so that the shift + and get folded into a bitfield extract.
3478 DebugLoc dl = Op.getDebugLoc();
3479 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3480 DAG.getConstant(Intrinsic::arm_get_fpscr,
3481 MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003482 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemand1fb5832010-08-03 21:31:55 +00003483 DAG.getConstant(1U << 22, MVT::i32));
3484 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3485 DAG.getConstant(22, MVT::i32));
Jim Grosbach4725ca72010-09-08 03:54:02 +00003486 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemand1fb5832010-08-03 21:31:55 +00003487 DAG.getConstant(3, MVT::i32));
3488}
3489
Jim Grosbach3482c802010-01-18 19:58:49 +00003490static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3491 const ARMSubtarget *ST) {
3492 EVT VT = N->getValueType(0);
3493 DebugLoc dl = N->getDebugLoc();
3494
3495 if (!ST->hasV6T2Ops())
3496 return SDValue();
3497
3498 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3499 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3500}
3501
Bob Wilson5bafff32009-06-22 23:27:02 +00003502static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3503 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00003504 EVT VT = N->getValueType(0);
Bob Wilson5bafff32009-06-22 23:27:02 +00003505 DebugLoc dl = N->getDebugLoc();
3506
Bob Wilsond5448bb2010-11-18 21:16:28 +00003507 if (!VT.isVector())
3508 return SDValue();
3509
Bob Wilson5bafff32009-06-22 23:27:02 +00003510 // Lower vector shifts on NEON to use VSHL.
Bob Wilsond5448bb2010-11-18 21:16:28 +00003511 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00003512
Bob Wilsond5448bb2010-11-18 21:16:28 +00003513 // Left shifts translate directly to the vshiftu intrinsic.
3514 if (N->getOpcode() == ISD::SHL)
Bob Wilson5bafff32009-06-22 23:27:02 +00003515 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilsond5448bb2010-11-18 21:16:28 +00003516 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3517 N->getOperand(0), N->getOperand(1));
3518
3519 assert((N->getOpcode() == ISD::SRA ||
3520 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3521
3522 // NEON uses the same intrinsics for both left and right shifts. For
3523 // right shifts, the shift amounts are negative, so negate the vector of
3524 // shift amounts.
3525 EVT ShiftVT = N->getOperand(1).getValueType();
3526 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3527 getZeroVector(ShiftVT, DAG, dl),
3528 N->getOperand(1));
3529 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3530 Intrinsic::arm_neon_vshifts :
3531 Intrinsic::arm_neon_vshiftu);
3532 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3533 DAG.getConstant(vshiftInt, MVT::i32),
3534 N->getOperand(0), NegatedCount);
3535}
3536
3537static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3538 const ARMSubtarget *ST) {
3539 EVT VT = N->getValueType(0);
3540 DebugLoc dl = N->getDebugLoc();
Bob Wilson5bafff32009-06-22 23:27:02 +00003541
Eli Friedmance392eb2009-08-22 03:13:10 +00003542 // We can get here for a node like i32 = ISD::SHL i32, i64
3543 if (VT != MVT::i64)
3544 return SDValue();
3545
3546 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattner27a6c732007-11-24 07:07:01 +00003547 "Unknown shift to lower!");
Duncan Sands1607f052008-12-01 11:39:25 +00003548
Chris Lattner27a6c732007-11-24 07:07:01 +00003549 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3550 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003551 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands1607f052008-12-01 11:39:25 +00003552 return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003553
Chris Lattner27a6c732007-11-24 07:07:01 +00003554 // If we are in thumb mode, we don't have RRX.
David Goodwinf1daf7d2009-07-08 23:10:31 +00003555 if (ST->isThumb1Only()) return SDValue();
Bob Wilson2dc4f542009-03-20 22:42:55 +00003556
Chris Lattner27a6c732007-11-24 07:07:01 +00003557 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003559 DAG.getConstant(0, MVT::i32));
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilsonab3912e2010-05-25 03:36:52 +00003561 DAG.getConstant(1, MVT::i32));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003562
Chris Lattner27a6c732007-11-24 07:07:01 +00003563 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3564 // captures the result into a carry flag.
3565 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003566 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00003567
Chris Lattner27a6c732007-11-24 07:07:01 +00003568 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson2dc4f542009-03-20 22:42:55 +00003570
Chris Lattner27a6c732007-11-24 07:07:01 +00003571 // Merge the pieces into a single i64 value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattner27a6c732007-11-24 07:07:01 +00003573}
3574
Bob Wilson5bafff32009-06-22 23:27:02 +00003575static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
3576 SDValue TmpOp0, TmpOp1;
3577 bool Invert = false;
3578 bool Swap = false;
3579 unsigned Opc = 0;
3580
3581 SDValue Op0 = Op.getOperand(0);
3582 SDValue Op1 = Op.getOperand(1);
3583 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003584 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00003585 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3586 DebugLoc dl = Op.getDebugLoc();
3587
3588 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
3589 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003590 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003591 case ISD::SETUNE:
3592 case ISD::SETNE: Invert = true; // Fallthrough
3593 case ISD::SETOEQ:
3594 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3595 case ISD::SETOLT:
3596 case ISD::SETLT: Swap = true; // Fallthrough
3597 case ISD::SETOGT:
3598 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3599 case ISD::SETOLE:
3600 case ISD::SETLE: Swap = true; // Fallthrough
3601 case ISD::SETOGE:
3602 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3603 case ISD::SETUGE: Swap = true; // Fallthrough
3604 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
3605 case ISD::SETUGT: Swap = true; // Fallthrough
3606 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
3607 case ISD::SETUEQ: Invert = true; // Fallthrough
3608 case ISD::SETONE:
3609 // Expand this to (OLT | OGT).
3610 TmpOp0 = Op0;
3611 TmpOp1 = Op1;
3612 Opc = ISD::OR;
3613 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3614 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
3615 break;
3616 case ISD::SETUO: Invert = true; // Fallthrough
3617 case ISD::SETO:
3618 // Expand this to (OLT | OGE).
3619 TmpOp0 = Op0;
3620 TmpOp1 = Op1;
3621 Opc = ISD::OR;
3622 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
3623 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
3624 break;
3625 }
3626 } else {
3627 // Integer comparisons.
3628 switch (SetCCOpcode) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003629 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson5bafff32009-06-22 23:27:02 +00003630 case ISD::SETNE: Invert = true;
3631 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
3632 case ISD::SETLT: Swap = true;
3633 case ISD::SETGT: Opc = ARMISD::VCGT; break;
3634 case ISD::SETLE: Swap = true;
3635 case ISD::SETGE: Opc = ARMISD::VCGE; break;
3636 case ISD::SETULT: Swap = true;
3637 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
3638 case ISD::SETULE: Swap = true;
3639 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
3640 }
3641
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003642 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson5bafff32009-06-22 23:27:02 +00003643 if (Opc == ARMISD::VCEQ) {
3644
3645 SDValue AndOp;
3646 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3647 AndOp = Op0;
3648 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
3649 AndOp = Op1;
3650
3651 // Ignore bitconvert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003652 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00003653 AndOp = AndOp.getOperand(0);
3654
3655 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
3656 Opc = ARMISD::VTST;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003657 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
3658 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson5bafff32009-06-22 23:27:02 +00003659 Invert = !Invert;
3660 }
3661 }
3662 }
3663
3664 if (Swap)
3665 std::swap(Op0, Op1);
3666
Owen Andersonc24cb352010-11-08 23:21:22 +00003667 // If one of the operands is a constant vector zero, attempt to fold the
3668 // comparison to a specialized compare-against-zero form.
3669 SDValue SingleOp;
3670 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
3671 SingleOp = Op0;
3672 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
3673 if (Opc == ARMISD::VCGE)
3674 Opc = ARMISD::VCLEZ;
3675 else if (Opc == ARMISD::VCGT)
3676 Opc = ARMISD::VCLTZ;
3677 SingleOp = Op1;
3678 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003679
Owen Andersonc24cb352010-11-08 23:21:22 +00003680 SDValue Result;
3681 if (SingleOp.getNode()) {
3682 switch (Opc) {
3683 case ARMISD::VCEQ:
3684 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
3685 case ARMISD::VCGE:
3686 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
3687 case ARMISD::VCLEZ:
3688 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
3689 case ARMISD::VCGT:
3690 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
3691 case ARMISD::VCLTZ:
3692 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
3693 default:
3694 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3695 }
3696 } else {
3697 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
3698 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003699
3700 if (Invert)
3701 Result = DAG.getNOT(dl, Result, VT);
3702
3703 return Result;
3704}
3705
Bob Wilsond3c42842010-06-14 22:19:57 +00003706/// isNEONModifiedImm - Check if the specified splat value corresponds to a
3707/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsoncba270d2010-07-13 21:16:48 +00003708/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilsond3c42842010-06-14 22:19:57 +00003709static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
3710 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003711 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00003712 unsigned OpCmode, Imm;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003713
Bob Wilson827b2102010-06-15 19:05:35 +00003714 // SplatBitSize is set to the smallest size that splats the vector, so a
3715 // zero vector will always have SplatBitSize == 8. However, NEON modified
3716 // immediate instructions others than VMOV do not support the 8-bit encoding
3717 // of a zero vector, and the default encoding of zero is supposed to be the
3718 // 32-bit version.
3719 if (SplatBits == 0)
3720 SplatBitSize = 32;
3721
Bob Wilson5bafff32009-06-22 23:27:02 +00003722 switch (SplatBitSize) {
3723 case 8:
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003724 if (type != VMOVModImm)
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003725 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003726 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson5bafff32009-06-22 23:27:02 +00003727 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilson6dce00c2010-07-13 04:44:34 +00003728 OpCmode = 0xe;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003729 Imm = SplatBits;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003730 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003731 break;
Bob Wilson5bafff32009-06-22 23:27:02 +00003732
3733 case 16:
3734 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003735 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003736 if ((SplatBits & ~0xff) == 0) {
3737 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003738 OpCmode = 0x8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003739 Imm = SplatBits;
3740 break;
3741 }
3742 if ((SplatBits & ~0xff00) == 0) {
3743 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003744 OpCmode = 0xa;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003745 Imm = SplatBits >> 8;
3746 break;
3747 }
3748 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003749
3750 case 32:
3751 // NEON's 32-bit VMOV supports splat values where:
3752 // * only one byte is nonzero, or
3753 // * the least significant byte is 0xff and the second byte is nonzero, or
3754 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsoncba270d2010-07-13 21:16:48 +00003755 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003756 if ((SplatBits & ~0xff) == 0) {
3757 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003758 OpCmode = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003759 Imm = SplatBits;
3760 break;
3761 }
3762 if ((SplatBits & ~0xff00) == 0) {
3763 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003764 OpCmode = 0x2;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003765 Imm = SplatBits >> 8;
3766 break;
3767 }
3768 if ((SplatBits & ~0xff0000) == 0) {
3769 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003770 OpCmode = 0x4;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003771 Imm = SplatBits >> 16;
3772 break;
3773 }
3774 if ((SplatBits & ~0xff000000) == 0) {
3775 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003776 OpCmode = 0x6;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003777 Imm = SplatBits >> 24;
3778 break;
3779 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003780
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003781 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
3782 if (type == OtherModImm) return SDValue();
3783
Bob Wilson5bafff32009-06-22 23:27:02 +00003784 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003785 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
3786 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003787 OpCmode = 0xc;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003788 Imm = SplatBits >> 8;
3789 SplatBits |= 0xff;
3790 break;
3791 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003792
3793 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson1a913ed2010-06-11 21:34:50 +00003794 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
3795 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003796 OpCmode = 0xd;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003797 Imm = SplatBits >> 16;
3798 SplatBits |= 0xffff;
3799 break;
3800 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003801
3802 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
3803 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
3804 // VMOV.I32. A (very) minor optimization would be to replicate the value
3805 // and fall through here to test for a valid 64-bit splat. But, then the
3806 // caller would also need to check and handle the change in size.
Bob Wilson1a913ed2010-06-11 21:34:50 +00003807 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00003808
3809 case 64: {
Owen Anderson36fa3ea2010-11-05 21:57:54 +00003810 if (type != VMOVModImm)
Bob Wilson827b2102010-06-15 19:05:35 +00003811 return SDValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00003812 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson5bafff32009-06-22 23:27:02 +00003813 uint64_t BitMask = 0xff;
3814 uint64_t Val = 0;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003815 unsigned ImmMask = 1;
3816 Imm = 0;
Bob Wilson5bafff32009-06-22 23:27:02 +00003817 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson1a913ed2010-06-11 21:34:50 +00003818 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003819 Val |= BitMask;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003820 Imm |= ImmMask;
3821 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson5bafff32009-06-22 23:27:02 +00003822 return SDValue();
Bob Wilson1a913ed2010-06-11 21:34:50 +00003823 }
Bob Wilson5bafff32009-06-22 23:27:02 +00003824 BitMask <<= 8;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003825 ImmMask <<= 1;
Bob Wilson5bafff32009-06-22 23:27:02 +00003826 }
Bob Wilson1a913ed2010-06-11 21:34:50 +00003827 // Op=1, Cmode=1110.
Bob Wilson6dce00c2010-07-13 04:44:34 +00003828 OpCmode = 0x1e;
Bob Wilson1a913ed2010-06-11 21:34:50 +00003829 SplatBits = Val;
Bob Wilsoncba270d2010-07-13 21:16:48 +00003830 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson5bafff32009-06-22 23:27:02 +00003831 break;
3832 }
3833
Bob Wilson1a913ed2010-06-11 21:34:50 +00003834 default:
Bob Wilsondc076da2010-06-19 05:32:09 +00003835 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson1a913ed2010-06-11 21:34:50 +00003836 }
3837
Bob Wilsoncba270d2010-07-13 21:16:48 +00003838 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
3839 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson5bafff32009-06-22 23:27:02 +00003840}
3841
Lang Hamesc0a9f822012-03-29 21:56:11 +00003842SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
3843 const ARMSubtarget *ST) const {
3844 if (!ST->useNEONForSinglePrecisionFP() || !ST->hasVFP3() || ST->hasD16())
3845 return SDValue();
3846
3847 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
3848 assert(Op.getValueType() == MVT::f32 &&
3849 "ConstantFP custom lowering should only occur for f32.");
3850
3851 // Try splatting with a VMOV.f32...
3852 APFloat FPVal = CFP->getValueAPF();
3853 int ImmVal = ARM_AM::getFP32Imm(FPVal);
3854 if (ImmVal != -1) {
3855 DebugLoc DL = Op.getDebugLoc();
3856 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
3857 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
3858 NewVal);
3859 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
3860 DAG.getConstant(0, MVT::i32));
3861 }
3862
3863 // If that fails, try a VMOV.i32
3864 EVT VMovVT;
3865 unsigned iVal = FPVal.bitcastToAPInt().getZExtValue();
3866 SDValue NewVal = isNEONModifiedImm(iVal, 0, 32, DAG, VMovVT, false,
3867 VMOVModImm);
3868 if (NewVal != SDValue()) {
3869 DebugLoc DL = Op.getDebugLoc();
3870 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
3871 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 // Finally, try a VMVN.i32
3879 NewVal = isNEONModifiedImm(~iVal & 0xffffffff, 0, 32, DAG, VMovVT, false,
3880 VMVNModImm);
3881 if (NewVal != SDValue()) {
3882 DebugLoc DL = Op.getDebugLoc();
3883 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
3884 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
3885 VecConstant);
3886 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
3887 DAG.getConstant(0, MVT::i32));
3888 }
3889
3890 return SDValue();
3891}
3892
3893
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003894static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003895 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003896 unsigned NumElts = VT.getVectorNumElements();
3897 ReverseVEXT = false;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003898
3899 // Assume that the first shuffle index is not UNDEF. Fail if it is.
3900 if (M[0] < 0)
3901 return false;
3902
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003903 Imm = M[0];
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003904
3905 // If this is a VEXT shuffle, the immediate value is the index of the first
3906 // element. The other shuffle indices must be the successive elements after
3907 // the first one.
3908 unsigned ExpectedElt = Imm;
3909 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003910 // Increment the expected index. If it wraps around, it may still be
3911 // a VEXT but the source vectors must be swapped.
3912 ExpectedElt += 1;
3913 if (ExpectedElt == NumElts * 2) {
3914 ExpectedElt = 0;
3915 ReverseVEXT = true;
3916 }
3917
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003918 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003919 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003920 return false;
3921 }
3922
3923 // Adjust the index value if the source operands will be swapped.
3924 if (ReverseVEXT)
3925 Imm -= NumElts;
3926
Bob Wilsonde95c1b82009-08-19 17:03:43 +00003927 return true;
3928}
3929
Bob Wilson8bb9e482009-07-26 00:39:34 +00003930/// isVREVMask - Check if a vector shuffle corresponds to a VREV
3931/// instruction with the specified blocksize. (The order of the elements
3932/// within each block of the vector is reversed.)
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003933static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8bb9e482009-07-26 00:39:34 +00003934 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
3935 "Only possible block sizes for VREV are: 16, 32, 64");
3936
Bob Wilson8bb9e482009-07-26 00:39:34 +00003937 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson20d10812009-10-21 21:36:27 +00003938 if (EltSz == 64)
3939 return false;
3940
3941 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00003942 unsigned BlockElts = M[0] + 1;
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003943 // If the first shuffle index is UNDEF, be optimistic.
3944 if (M[0] < 0)
3945 BlockElts = BlockSize / EltSz;
Bob Wilson8bb9e482009-07-26 00:39:34 +00003946
3947 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
3948 return false;
3949
3950 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003951 if (M[i] < 0) continue; // ignore UNDEF indices
3952 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8bb9e482009-07-26 00:39:34 +00003953 return false;
3954 }
3955
3956 return true;
3957}
3958
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003959static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling0d4c9d92011-03-15 21:15:20 +00003960 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
3961 // range, then 0 is placed into the resulting vector. So pretty much any mask
3962 // of 8 elements can work here.
3963 return VT == MVT::v8i8 && M.size() == 8;
3964}
3965
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003966static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00003967 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3968 if (EltSz == 64)
3969 return false;
3970
Bob Wilsonc692cb72009-08-21 20:54:19 +00003971 unsigned NumElts = VT.getVectorNumElements();
3972 WhichResult = (M[0] == 0 ? 0 : 1);
3973 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003974 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3975 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsonc692cb72009-08-21 20:54:19 +00003976 return false;
3977 }
3978 return true;
3979}
3980
Bob Wilson324f4f12009-12-03 06:40:55 +00003981/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
3982/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
3983/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003984static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00003985 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
3986 if (EltSz == 64)
3987 return false;
3988
3989 unsigned NumElts = VT.getVectorNumElements();
3990 WhichResult = (M[0] == 0 ? 0 : 1);
3991 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00003992 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
3993 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson324f4f12009-12-03 06:40:55 +00003994 return false;
3995 }
3996 return true;
3997}
3998
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003999static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004000 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4001 if (EltSz == 64)
4002 return false;
4003
Bob Wilsonc692cb72009-08-21 20:54:19 +00004004 unsigned NumElts = VT.getVectorNumElements();
4005 WhichResult = (M[0] == 0 ? 0 : 1);
4006 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004007 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsonc692cb72009-08-21 20:54:19 +00004008 if ((unsigned) M[i] != 2 * i + WhichResult)
4009 return false;
4010 }
4011
4012 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004013 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004014 return false;
4015
4016 return true;
4017}
4018
Bob Wilson324f4f12009-12-03 06:40:55 +00004019/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4020/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4021/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004022static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004023 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4024 if (EltSz == 64)
4025 return false;
4026
4027 unsigned Half = VT.getVectorNumElements() / 2;
4028 WhichResult = (M[0] == 0 ? 0 : 1);
4029 for (unsigned j = 0; j != 2; ++j) {
4030 unsigned Idx = WhichResult;
4031 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004032 int MIdx = M[i + j * Half];
4033 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson324f4f12009-12-03 06:40:55 +00004034 return false;
4035 Idx += 2;
4036 }
4037 }
4038
4039 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4040 if (VT.is64BitVector() && EltSz == 32)
4041 return false;
4042
4043 return true;
4044}
4045
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004046static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson20d10812009-10-21 21:36:27 +00004047 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4048 if (EltSz == 64)
4049 return false;
4050
Bob Wilsonc692cb72009-08-21 20:54:19 +00004051 unsigned NumElts = VT.getVectorNumElements();
4052 WhichResult = (M[0] == 0 ? 0 : 1);
4053 unsigned Idx = WhichResult * NumElts / 2;
4054 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004055 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4056 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsonc692cb72009-08-21 20:54:19 +00004057 return false;
4058 Idx += 1;
4059 }
4060
4061 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson20d10812009-10-21 21:36:27 +00004062 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsonc692cb72009-08-21 20:54:19 +00004063 return false;
4064
4065 return true;
4066}
4067
Bob Wilson324f4f12009-12-03 06:40:55 +00004068/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4069/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4070/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004071static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson324f4f12009-12-03 06:40:55 +00004072 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4073 if (EltSz == 64)
4074 return false;
4075
4076 unsigned NumElts = VT.getVectorNumElements();
4077 WhichResult = (M[0] == 0 ? 0 : 1);
4078 unsigned Idx = WhichResult * NumElts / 2;
4079 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson7aaf5bf2010-08-17 05:54:34 +00004080 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4081 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson324f4f12009-12-03 06:40:55 +00004082 return false;
4083 Idx += 1;
4084 }
4085
4086 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4087 if (VT.is64BitVector() && EltSz == 32)
4088 return false;
4089
4090 return true;
4091}
4092
Dale Johannesenf630c712010-07-29 20:10:08 +00004093// If N is an integer constant that can be moved into a register in one
4094// instruction, return an SDValue of such a constant (will become a MOV
4095// instruction). Otherwise return null.
4096static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4097 const ARMSubtarget *ST, DebugLoc dl) {
4098 uint64_t Val;
4099 if (!isa<ConstantSDNode>(N))
4100 return SDValue();
4101 Val = cast<ConstantSDNode>(N)->getZExtValue();
4102
4103 if (ST->isThumb1Only()) {
4104 if (Val <= 255 || ~Val <= 255)
4105 return DAG.getConstant(Val, MVT::i32);
4106 } else {
4107 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4108 return DAG.getConstant(Val, MVT::i32);
4109 }
4110 return SDValue();
4111}
4112
Bob Wilson5bafff32009-06-22 23:27:02 +00004113// If this is a case we can't handle, return null and let the default
4114// expansion code take care of it.
Bob Wilson11a1dff2011-01-07 21:37:30 +00004115SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4116 const ARMSubtarget *ST) const {
Bob Wilsond06791f2009-08-13 01:57:47 +00004117 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Bob Wilson5bafff32009-06-22 23:27:02 +00004118 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004119 EVT VT = Op.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00004120
4121 APInt SplatBits, SplatUndef;
4122 unsigned SplatBitSize;
4123 bool HasAnyUndefs;
4124 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004125 if (SplatBitSize <= 64) {
Bob Wilsond3c42842010-06-14 22:19:57 +00004126 // Check if an immediate VMOV works.
Bob Wilsoncba270d2010-07-13 21:16:48 +00004127 EVT VmovVT;
Bob Wilsond3c42842010-06-14 22:19:57 +00004128 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsoncba270d2010-07-13 21:16:48 +00004129 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004130 DAG, VmovVT, VT.is128BitVector(),
4131 VMOVModImm);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004132 if (Val.getNode()) {
4133 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004134 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsoncba270d2010-07-13 21:16:48 +00004135 }
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004136
4137 // Try an immediate VMVN.
Eli Friedman8e4d0422011-10-13 22:40:23 +00004138 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004139 Val = isNEONModifiedImm(NegatedImm,
4140 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004141 DAG, VmovVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00004142 VMVNModImm);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004143 if (Val.getNode()) {
4144 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004145 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00004146 }
Evan Chengeaa192a2011-11-15 02:12:34 +00004147
4148 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedman2f21e8c2011-12-15 22:56:53 +00004149 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedmaneffab8f2011-12-09 23:54:42 +00004150 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Chengeaa192a2011-11-15 02:12:34 +00004151 if (ImmVal != -1) {
4152 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4153 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4154 }
4155 }
Anton Korobeynikov71624cc2009-08-29 00:08:18 +00004156 }
Bob Wilsoncf661e22009-07-30 00:31:25 +00004157 }
4158
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004159 // Scan through the operands to see if only one value is used.
4160 unsigned NumElts = VT.getVectorNumElements();
4161 bool isOnlyLowElement = true;
4162 bool usesOnlyOneValue = true;
4163 bool isConstant = true;
4164 SDValue Value;
4165 for (unsigned i = 0; i < NumElts; ++i) {
4166 SDValue V = Op.getOperand(i);
4167 if (V.getOpcode() == ISD::UNDEF)
4168 continue;
4169 if (i > 0)
4170 isOnlyLowElement = false;
4171 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4172 isConstant = false;
4173
4174 if (!Value.getNode())
4175 Value = V;
4176 else if (V != Value)
4177 usesOnlyOneValue = false;
4178 }
4179
4180 if (!Value.getNode())
4181 return DAG.getUNDEF(VT);
4182
4183 if (isOnlyLowElement)
4184 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4185
Dale Johannesenf630c712010-07-29 20:10:08 +00004186 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4187
Dale Johannesen575cd142010-10-19 20:00:17 +00004188 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4189 // i32 and try again.
4190 if (usesOnlyOneValue && EltSize <= 32) {
4191 if (!isConstant)
4192 return DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4193 if (VT.getVectorElementType().isFloatingPoint()) {
4194 SmallVector<SDValue, 8> Ops;
4195 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004196 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen575cd142010-10-19 20:00:17 +00004197 Op.getOperand(i)));
Nate Begemanbf5be262010-11-10 21:35:41 +00004198 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4199 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
Dale Johannesene4d31592010-10-20 22:03:37 +00004200 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4201 if (Val.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004202 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004203 }
Dale Johannesen575cd142010-10-19 20:00:17 +00004204 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4205 if (Val.getNode())
4206 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
Dale Johannesenf630c712010-07-29 20:10:08 +00004207 }
4208
4209 // If all elements are constants and the case above didn't get hit, fall back
4210 // to the default expansion, which will generate a load from the constant
4211 // pool.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004212 if (isConstant)
4213 return SDValue();
4214
Bob Wilson11a1dff2011-01-07 21:37:30 +00004215 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4216 if (NumElts >= 4) {
4217 SDValue shuffle = ReconstructShuffle(Op, DAG);
4218 if (shuffle != SDValue())
4219 return shuffle;
4220 }
4221
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004222 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004223 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4224 // will be legalized.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004225 if (EltSize >= 32) {
4226 // Do the expansion with floating-point types, since that is what the VFP
4227 // registers are defined to use, and since i64 is not legal.
4228 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4229 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004230 SmallVector<SDValue, 8> Ops;
4231 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004232 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004233 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004234 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004235 }
4236
4237 return SDValue();
4238}
4239
Bob Wilson11a1dff2011-01-07 21:37:30 +00004240// Gather data to see if the operation can be modelled as a
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004241// shuffle in combination with VEXTs.
Eric Christopher41262da2011-01-14 23:50:53 +00004242SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4243 SelectionDAG &DAG) const {
Bob Wilson11a1dff2011-01-07 21:37:30 +00004244 DebugLoc dl = Op.getDebugLoc();
4245 EVT VT = Op.getValueType();
4246 unsigned NumElts = VT.getVectorNumElements();
4247
4248 SmallVector<SDValue, 2> SourceVecs;
4249 SmallVector<unsigned, 2> MinElts;
4250 SmallVector<unsigned, 2> MaxElts;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004251
Bob Wilson11a1dff2011-01-07 21:37:30 +00004252 for (unsigned i = 0; i < NumElts; ++i) {
4253 SDValue V = Op.getOperand(i);
4254 if (V.getOpcode() == ISD::UNDEF)
4255 continue;
4256 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4257 // A shuffle can only come from building a vector from various
4258 // elements of other vectors.
4259 return SDValue();
Eli Friedman46995fa2011-10-14 23:58:49 +00004260 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4261 VT.getVectorElementType()) {
4262 // This code doesn't know how to handle shuffles where the vector
4263 // element types do not match (this happens because type legalization
4264 // promotes the return type of EXTRACT_VECTOR_ELT).
4265 // FIXME: It might be appropriate to extend this code to handle
4266 // mismatched types.
4267 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004268 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004269
Bob Wilson11a1dff2011-01-07 21:37:30 +00004270 // Record this extraction against the appropriate vector if possible...
4271 SDValue SourceVec = V.getOperand(0);
Jim Grosbach24220472012-07-25 17:02:47 +00004272 // If the element number isn't a constant, we can't effectively
4273 // analyze what's going on.
4274 if (!isa<ConstantSDNode>(V.getOperand(1)))
4275 return SDValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004276 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4277 bool FoundSource = false;
4278 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4279 if (SourceVecs[j] == SourceVec) {
4280 if (MinElts[j] > EltNo)
4281 MinElts[j] = EltNo;
4282 if (MaxElts[j] < EltNo)
4283 MaxElts[j] = EltNo;
4284 FoundSource = true;
4285 break;
4286 }
4287 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004288
Bob Wilson11a1dff2011-01-07 21:37:30 +00004289 // Or record a new source if not...
4290 if (!FoundSource) {
4291 SourceVecs.push_back(SourceVec);
4292 MinElts.push_back(EltNo);
4293 MaxElts.push_back(EltNo);
4294 }
4295 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004296
Bob Wilson11a1dff2011-01-07 21:37:30 +00004297 // Currently only do something sane when at most two source vectors
4298 // involved.
4299 if (SourceVecs.size() > 2)
4300 return SDValue();
4301
4302 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4303 int VEXTOffsets[2] = {0, 0};
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004304
Bob Wilson11a1dff2011-01-07 21:37:30 +00004305 // This loop extracts the usage patterns of the source vectors
4306 // and prepares appropriate SDValues for a shuffle if possible.
4307 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4308 if (SourceVecs[i].getValueType() == VT) {
4309 // No VEXT necessary
4310 ShuffleSrcs[i] = SourceVecs[i];
4311 VEXTOffsets[i] = 0;
4312 continue;
4313 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4314 // It probably isn't worth padding out a smaller vector just to
4315 // break it down again in a shuffle.
4316 return SDValue();
4317 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004318
Bob Wilson11a1dff2011-01-07 21:37:30 +00004319 // Since only 64-bit and 128-bit vectors are legal on ARM and
4320 // we've eliminated the other cases...
Bob Wilson70f85732011-01-07 23:40:46 +00004321 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4322 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004323
Bob Wilson11a1dff2011-01-07 21:37:30 +00004324 if (MaxElts[i] - MinElts[i] >= NumElts) {
4325 // Span too large for a VEXT to cope
4326 return SDValue();
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004327 }
4328
Bob Wilson11a1dff2011-01-07 21:37:30 +00004329 if (MinElts[i] >= NumElts) {
4330 // The extraction can just take the second half
4331 VEXTOffsets[i] = NumElts;
Eric Christopher41262da2011-01-14 23:50:53 +00004332 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4333 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004334 DAG.getIntPtrConstant(NumElts));
4335 } else if (MaxElts[i] < NumElts) {
4336 // The extraction can just take the first half
4337 VEXTOffsets[i] = 0;
Eric Christopher41262da2011-01-14 23:50:53 +00004338 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4339 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004340 DAG.getIntPtrConstant(0));
4341 } else {
4342 // An actual VEXT is needed
4343 VEXTOffsets[i] = MinElts[i];
Eric Christopher41262da2011-01-14 23:50:53 +00004344 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4345 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004346 DAG.getIntPtrConstant(0));
Eric Christopher41262da2011-01-14 23:50:53 +00004347 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4348 SourceVecs[i],
Bob Wilson11a1dff2011-01-07 21:37:30 +00004349 DAG.getIntPtrConstant(NumElts));
4350 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4351 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4352 }
4353 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004354
Bob Wilson11a1dff2011-01-07 21:37:30 +00004355 SmallVector<int, 8> Mask;
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004356
Bob Wilson11a1dff2011-01-07 21:37:30 +00004357 for (unsigned i = 0; i < NumElts; ++i) {
4358 SDValue Entry = Op.getOperand(i);
4359 if (Entry.getOpcode() == ISD::UNDEF) {
4360 Mask.push_back(-1);
4361 continue;
4362 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004363
Bob Wilson11a1dff2011-01-07 21:37:30 +00004364 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher41262da2011-01-14 23:50:53 +00004365 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4366 .getOperand(1))->getSExtValue();
Bob Wilson11a1dff2011-01-07 21:37:30 +00004367 if (ExtractVec == SourceVecs[0]) {
4368 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4369 } else {
4370 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4371 }
4372 }
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004373
Bob Wilson11a1dff2011-01-07 21:37:30 +00004374 // Final check before we try to produce nonsense...
4375 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher41262da2011-01-14 23:50:53 +00004376 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4377 &Mask[0]);
Andrew Trick7fa75ce2011-01-19 02:26:13 +00004378
Bob Wilson11a1dff2011-01-07 21:37:30 +00004379 return SDValue();
4380}
4381
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004382/// isShuffleMaskLegal - Targets can use this to indicate that they only
4383/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4384/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4385/// are assumed to be legal.
4386bool
4387ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4388 EVT VT) const {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004389 if (VT.getVectorNumElements() == 4 &&
4390 (VT.is128BitVector() || VT.is64BitVector())) {
4391 unsigned PFIndexes[4];
4392 for (unsigned i = 0; i != 4; ++i) {
4393 if (M[i] < 0)
4394 PFIndexes[i] = 8;
4395 else
4396 PFIndexes[i] = M[i];
4397 }
4398
4399 // Compute the index in the perfect shuffle table.
4400 unsigned PFTableIndex =
4401 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4402 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4403 unsigned Cost = (PFEntry >> 30);
4404
4405 if (Cost <= 4)
4406 return true;
4407 }
4408
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004409 bool ReverseVEXT;
Bob Wilsonc692cb72009-08-21 20:54:19 +00004410 unsigned Imm, WhichResult;
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004411
Bob Wilson53dd2452010-06-07 23:53:38 +00004412 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4413 return (EltSize >= 32 ||
4414 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004415 isVREVMask(M, VT, 64) ||
4416 isVREVMask(M, VT, 32) ||
4417 isVREVMask(M, VT, 16) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004418 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling0d4c9d92011-03-15 21:15:20 +00004419 isVTBLMask(M, VT) ||
Bob Wilsonc692cb72009-08-21 20:54:19 +00004420 isVTRNMask(M, VT, WhichResult) ||
4421 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson324f4f12009-12-03 06:40:55 +00004422 isVZIPMask(M, VT, WhichResult) ||
4423 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
4424 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
4425 isVZIP_v_undef_Mask(M, VT, WhichResult));
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004426}
4427
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004428/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4429/// the specified operations to build the shuffle.
4430static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
4431 SDValue RHS, SelectionDAG &DAG,
4432 DebugLoc dl) {
4433 unsigned OpNum = (PFEntry >> 26) & 0x0F;
4434 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
4435 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
4436
4437 enum {
4438 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
4439 OP_VREV,
4440 OP_VDUP0,
4441 OP_VDUP1,
4442 OP_VDUP2,
4443 OP_VDUP3,
4444 OP_VEXT1,
4445 OP_VEXT2,
4446 OP_VEXT3,
4447 OP_VUZPL, // VUZP, left result
4448 OP_VUZPR, // VUZP, right result
4449 OP_VZIPL, // VZIP, left result
4450 OP_VZIPR, // VZIP, right result
4451 OP_VTRNL, // VTRN, left result
4452 OP_VTRNR // VTRN, right result
4453 };
4454
4455 if (OpNum == OP_COPY) {
4456 if (LHSID == (1*9+2)*9+3) return LHS;
4457 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4458 return RHS;
4459 }
4460
4461 SDValue OpLHS, OpRHS;
4462 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4463 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
4464 EVT VT = OpLHS.getValueType();
4465
4466 switch (OpNum) {
4467 default: llvm_unreachable("Unknown shuffle opcode!");
4468 case OP_VREV:
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004469 // VREV divides the vector in half and swaps within the half.
Tanya Lattnerdb282472011-05-18 21:44:54 +00004470 if (VT.getVectorElementType() == MVT::i32 ||
4471 VT.getVectorElementType() == MVT::f32)
Tanya Lattner2a8eb722011-05-18 06:42:21 +00004472 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
4473 // vrev <4 x i16> -> VREV32
4474 if (VT.getVectorElementType() == MVT::i16)
4475 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
4476 // vrev <4 x i8> -> VREV16
4477 assert(VT.getVectorElementType() == MVT::i8);
4478 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004479 case OP_VDUP0:
4480 case OP_VDUP1:
4481 case OP_VDUP2:
4482 case OP_VDUP3:
4483 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004484 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004485 case OP_VEXT1:
4486 case OP_VEXT2:
4487 case OP_VEXT3:
4488 return DAG.getNode(ARMISD::VEXT, dl, VT,
4489 OpLHS, OpRHS,
4490 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
4491 case OP_VUZPL:
4492 case OP_VUZPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004493 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004494 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
4495 case OP_VZIPL:
4496 case OP_VZIPR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004497 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004498 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
4499 case OP_VTRNL:
4500 case OP_VTRNR:
Anton Korobeynikov051cfd62009-08-21 12:41:42 +00004501 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4502 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004503 }
4504}
4505
Bill Wendling69a05a72011-03-14 23:02:38 +00004506static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004507 ArrayRef<int> ShuffleMask,
Bill Wendling69a05a72011-03-14 23:02:38 +00004508 SelectionDAG &DAG) {
4509 // Check to see if we can use the VTBL instruction.
4510 SDValue V1 = Op.getOperand(0);
4511 SDValue V2 = Op.getOperand(1);
4512 DebugLoc DL = Op.getDebugLoc();
4513
4514 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004515 for (ArrayRef<int>::iterator
Bill Wendling69a05a72011-03-14 23:02:38 +00004516 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
4517 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
4518
4519 if (V2.getNode()->getOpcode() == ISD::UNDEF)
4520 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
4521 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4522 &VTBLMask[0], 8));
Bill Wendlinga24cb402011-03-15 20:47:26 +00004523
Owen Anderson76706012011-04-05 21:48:57 +00004524 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Bill Wendlinga24cb402011-03-15 20:47:26 +00004525 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
4526 &VTBLMask[0], 8));
Bill Wendling69a05a72011-03-14 23:02:38 +00004527}
4528
Bob Wilson5bafff32009-06-22 23:27:02 +00004529static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004530 SDValue V1 = Op.getOperand(0);
4531 SDValue V2 = Op.getOperand(1);
Bob Wilsond8e17572009-08-12 22:31:50 +00004532 DebugLoc dl = Op.getDebugLoc();
4533 EVT VT = Op.getValueType();
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004534 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsond8e17572009-08-12 22:31:50 +00004535
Bob Wilson28865062009-08-13 02:13:04 +00004536 // Convert shuffles that are directly supported on NEON to target-specific
4537 // DAG nodes, instead of keeping them as shuffles and matching them again
4538 // during code selection. This is more efficient and avoids the possibility
4539 // of inconsistencies between legalization and selection.
Bob Wilsonbfcbb502009-08-13 06:01:30 +00004540 // FIXME: floating-point vectors should be canonicalized to integer vectors
4541 // of the same time so that they get CSEd properly.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00004542 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovd0ac2342009-08-21 12:40:07 +00004543
Bob Wilson53dd2452010-06-07 23:53:38 +00004544 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4545 if (EltSize <= 32) {
4546 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
4547 int Lane = SVN->getSplatIndex();
4548 // If this is undef splat, generate it via "just" vdup, if possible.
4549 if (Lane == -1) Lane = 0;
Anton Korobeynikov2ae0eec2009-11-02 00:12:06 +00004550
Dan Gohman65fd6562011-11-03 21:49:52 +00004551 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson53dd2452010-06-07 23:53:38 +00004552 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4553 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4554 }
Dan Gohman65fd6562011-11-03 21:49:52 +00004555 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
4556 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
4557 // reaches it).
4558 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
4559 !isa<ConstantSDNode>(V1.getOperand(0))) {
4560 bool IsScalarToVector = true;
4561 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
4562 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
4563 IsScalarToVector = false;
4564 break;
4565 }
4566 if (IsScalarToVector)
4567 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
4568 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004569 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
4570 DAG.getConstant(Lane, MVT::i32));
Bob Wilsonc1d287b2009-08-14 05:13:08 +00004571 }
Bob Wilson53dd2452010-06-07 23:53:38 +00004572
4573 bool ReverseVEXT;
4574 unsigned Imm;
4575 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
4576 if (ReverseVEXT)
4577 std::swap(V1, V2);
4578 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
4579 DAG.getConstant(Imm, MVT::i32));
4580 }
4581
4582 if (isVREVMask(ShuffleMask, VT, 64))
4583 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
4584 if (isVREVMask(ShuffleMask, VT, 32))
4585 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
4586 if (isVREVMask(ShuffleMask, VT, 16))
4587 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
4588
4589 // Check for Neon shuffles that modify both input vectors in place.
4590 // If both results are used, i.e., if there are two shuffles with the same
4591 // source operands and with masks corresponding to both results of one of
4592 // these operations, DAG memoization will ensure that a single node is
4593 // used for both shuffles.
4594 unsigned WhichResult;
4595 if (isVTRNMask(ShuffleMask, VT, WhichResult))
4596 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4597 V1, V2).getValue(WhichResult);
4598 if (isVUZPMask(ShuffleMask, VT, WhichResult))
4599 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4600 V1, V2).getValue(WhichResult);
4601 if (isVZIPMask(ShuffleMask, VT, WhichResult))
4602 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4603 V1, V2).getValue(WhichResult);
4604
4605 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
4606 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
4607 V1, V1).getValue(WhichResult);
4608 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4609 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
4610 V1, V1).getValue(WhichResult);
4611 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
4612 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
4613 V1, V1).getValue(WhichResult);
Bob Wilson0ce37102009-08-14 05:08:32 +00004614 }
Bob Wilsonde95c1b82009-08-19 17:03:43 +00004615
Bob Wilsonc692cb72009-08-21 20:54:19 +00004616 // If the shuffle is not directly supported and it has 4 elements, use
4617 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004618 unsigned NumElts = VT.getVectorNumElements();
4619 if (NumElts == 4) {
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004620 unsigned PFIndexes[4];
4621 for (unsigned i = 0; i != 4; ++i) {
4622 if (ShuffleMask[i] < 0)
4623 PFIndexes[i] = 8;
4624 else
4625 PFIndexes[i] = ShuffleMask[i];
4626 }
4627
4628 // Compute the index in the perfect shuffle table.
4629 unsigned PFTableIndex =
4630 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov1c8e5812009-08-21 12:41:24 +00004631 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4632 unsigned Cost = (PFEntry >> 30);
4633
4634 if (Cost <= 4)
4635 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
4636 }
Bob Wilsond8e17572009-08-12 22:31:50 +00004637
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004638 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004639 if (EltSize >= 32) {
4640 // Do the expansion with floating-point types, since that is what the VFP
4641 // registers are defined to use, and since i64 is not legal.
4642 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4643 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004644 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
4645 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004646 SmallVector<SDValue, 8> Ops;
Bob Wilsonbe751cf2010-05-22 00:23:12 +00004647 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson63b88452010-05-20 18:39:53 +00004648 if (ShuffleMask[i] < 0)
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004649 Ops.push_back(DAG.getUNDEF(EltVT));
4650 else
4651 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
4652 ShuffleMask[i] < (int)NumElts ? V1 : V2,
4653 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
4654 MVT::i32)));
Bob Wilson63b88452010-05-20 18:39:53 +00004655 }
Bob Wilson40cbe7d2010-06-04 00:04:02 +00004656 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004657 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson63b88452010-05-20 18:39:53 +00004658 }
4659
Bill Wendling69a05a72011-03-14 23:02:38 +00004660 if (VT == MVT::v8i8) {
4661 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
4662 if (NewOp.getNode())
4663 return NewOp;
4664 }
4665
Bob Wilson22cac0d2009-08-14 05:16:33 +00004666 return SDValue();
Bob Wilson5bafff32009-06-22 23:27:02 +00004667}
4668
Eli Friedman5c89cb82011-10-24 23:08:52 +00004669static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
4670 // INSERT_VECTOR_ELT is legal only for immediate indexes.
4671 SDValue Lane = Op.getOperand(2);
4672 if (!isa<ConstantSDNode>(Lane))
4673 return SDValue();
4674
4675 return Op;
4676}
4677
Bob Wilson5bafff32009-06-22 23:27:02 +00004678static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilson3468c2e2010-11-03 16:24:50 +00004679 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson5bafff32009-06-22 23:27:02 +00004680 SDValue Lane = Op.getOperand(1);
Bob Wilson3468c2e2010-11-03 16:24:50 +00004681 if (!isa<ConstantSDNode>(Lane))
4682 return SDValue();
4683
4684 SDValue Vec = Op.getOperand(0);
4685 if (Op.getValueType() == MVT::i32 &&
4686 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
4687 DebugLoc dl = Op.getDebugLoc();
4688 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
4689 }
4690
4691 return Op;
Bob Wilson5bafff32009-06-22 23:27:02 +00004692}
4693
Bob Wilsona6d65862009-08-03 20:36:38 +00004694static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
4695 // The only time a CONCAT_VECTORS operation can have legal types is when
4696 // two 64-bit vectors are concatenated to a 128-bit vector.
4697 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
4698 "unexpected CONCAT_VECTORS");
4699 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004700 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsona6d65862009-08-03 20:36:38 +00004701 SDValue Op0 = Op.getOperand(0);
4702 SDValue Op1 = Op.getOperand(1);
4703 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004704 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004705 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsona6d65862009-08-03 20:36:38 +00004706 DAG.getIntPtrConstant(0));
4707 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson825b72b2009-08-11 20:47:22 +00004708 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004709 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsona6d65862009-08-03 20:36:38 +00004710 DAG.getIntPtrConstant(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004711 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson5bafff32009-06-22 23:27:02 +00004712}
4713
Bob Wilson626613d2010-11-23 19:38:38 +00004714/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
4715/// element has been zero/sign-extended, depending on the isSigned parameter,
4716/// from an integer type half its size.
4717static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
4718 bool isSigned) {
4719 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
4720 EVT VT = N->getValueType(0);
4721 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
4722 SDNode *BVN = N->getOperand(0).getNode();
4723 if (BVN->getValueType(0) != MVT::v4i32 ||
4724 BVN->getOpcode() != ISD::BUILD_VECTOR)
4725 return false;
4726 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4727 unsigned HiElt = 1 - LoElt;
4728 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
4729 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
4730 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
4731 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
4732 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
4733 return false;
4734 if (isSigned) {
4735 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
4736 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
4737 return true;
4738 } else {
4739 if (Hi0->isNullValue() && Hi1->isNullValue())
4740 return true;
4741 }
4742 return false;
4743 }
4744
4745 if (N->getOpcode() != ISD::BUILD_VECTOR)
4746 return false;
4747
4748 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
4749 SDNode *Elt = N->getOperand(i).getNode();
4750 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
4751 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4752 unsigned HalfSize = EltSize / 2;
4753 if (isSigned) {
Bob Wilson9d45de22011-10-18 18:46:49 +00004754 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004755 return false;
4756 } else {
Bob Wilson9d45de22011-10-18 18:46:49 +00004757 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilson626613d2010-11-23 19:38:38 +00004758 return false;
4759 }
4760 continue;
4761 }
4762 return false;
4763 }
4764
4765 return true;
4766}
4767
4768/// isSignExtended - Check if a node is a vector value that is sign-extended
4769/// or a constant BUILD_VECTOR with sign-extended elements.
4770static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
4771 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
4772 return true;
4773 if (isExtendedBUILD_VECTOR(N, DAG, true))
4774 return true;
4775 return false;
4776}
4777
4778/// isZeroExtended - Check if a node is a vector value that is zero-extended
4779/// or a constant BUILD_VECTOR with zero-extended elements.
4780static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
4781 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
4782 return true;
4783 if (isExtendedBUILD_VECTOR(N, DAG, false))
4784 return true;
4785 return false;
4786}
4787
4788/// SkipExtension - For a node that is a SIGN_EXTEND, ZERO_EXTEND, extending
4789/// load, or BUILD_VECTOR with extended elements, return the unextended value.
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004790static SDValue SkipExtension(SDNode *N, SelectionDAG &DAG) {
4791 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
4792 return N->getOperand(0);
Bob Wilson626613d2010-11-23 19:38:38 +00004793 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
4794 return DAG.getLoad(LD->getMemoryVT(), N->getDebugLoc(), LD->getChain(),
4795 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004796 LD->isNonTemporal(), LD->isInvariant(),
4797 LD->getAlignment());
Bob Wilson626613d2010-11-23 19:38:38 +00004798 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
4799 // have been legalized as a BITCAST from v4i32.
4800 if (N->getOpcode() == ISD::BITCAST) {
4801 SDNode *BVN = N->getOperand(0).getNode();
4802 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
4803 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
4804 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
4805 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), MVT::v2i32,
4806 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
4807 }
4808 // Construct a new BUILD_VECTOR with elements truncated to half the size.
4809 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
4810 EVT VT = N->getValueType(0);
4811 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
4812 unsigned NumElts = VT.getVectorNumElements();
4813 MVT TruncVT = MVT::getIntegerVT(EltSize);
4814 SmallVector<SDValue, 8> Ops;
4815 for (unsigned i = 0; i != NumElts; ++i) {
4816 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
4817 const APInt &CInt = C->getAPIntValue();
Bob Wilsonff73d8f2012-04-30 16:53:34 +00004818 // Element types smaller than 32 bits are not legal, so use i32 elements.
4819 // The values are implicitly truncated so sext vs. zext doesn't matter.
4820 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilson626613d2010-11-23 19:38:38 +00004821 }
4822 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
4823 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004824}
4825
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004826static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
4827 unsigned Opcode = N->getOpcode();
4828 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4829 SDNode *N0 = N->getOperand(0).getNode();
4830 SDNode *N1 = N->getOperand(1).getNode();
4831 return N0->hasOneUse() && N1->hasOneUse() &&
4832 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
4833 }
4834 return false;
4835}
4836
4837static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
4838 unsigned Opcode = N->getOpcode();
4839 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
4840 SDNode *N0 = N->getOperand(0).getNode();
4841 SDNode *N1 = N->getOperand(1).getNode();
4842 return N0->hasOneUse() && N1->hasOneUse() &&
4843 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
4844 }
4845 return false;
4846}
4847
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004848static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
4849 // Multiplications are only custom-lowered for 128-bit vectors so that
4850 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
4851 EVT VT = Op.getValueType();
4852 assert(VT.is128BitVector() && "unexpected type for custom-lowering ISD::MUL");
4853 SDNode *N0 = Op.getOperand(0).getNode();
4854 SDNode *N1 = Op.getOperand(1).getNode();
4855 unsigned NewOpc = 0;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004856 bool isMLA = false;
4857 bool isN0SExt = isSignExtended(N0, DAG);
4858 bool isN1SExt = isSignExtended(N1, DAG);
4859 if (isN0SExt && isN1SExt)
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004860 NewOpc = ARMISD::VMULLs;
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004861 else {
4862 bool isN0ZExt = isZeroExtended(N0, DAG);
4863 bool isN1ZExt = isZeroExtended(N1, DAG);
4864 if (isN0ZExt && isN1ZExt)
4865 NewOpc = ARMISD::VMULLu;
4866 else if (isN1SExt || isN1ZExt) {
4867 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
4868 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
4869 if (isN1SExt && isAddSubSExt(N0, DAG)) {
4870 NewOpc = ARMISD::VMULLs;
4871 isMLA = true;
4872 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
4873 NewOpc = ARMISD::VMULLu;
4874 isMLA = true;
4875 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
4876 std::swap(N0, N1);
4877 NewOpc = ARMISD::VMULLu;
4878 isMLA = true;
4879 }
4880 }
4881
4882 if (!NewOpc) {
4883 if (VT == MVT::v2i64)
4884 // Fall through to expand this. It is not legal.
4885 return SDValue();
4886 else
4887 // Other vector multiplications are legal.
4888 return Op;
4889 }
4890 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004891
4892 // Legalize to a VMULL instruction.
4893 DebugLoc DL = Op.getDebugLoc();
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004894 SDValue Op0;
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004895 SDValue Op1 = SkipExtension(N1, DAG);
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004896 if (!isMLA) {
4897 Op0 = SkipExtension(N0, DAG);
4898 assert(Op0.getValueType().is64BitVector() &&
4899 Op1.getValueType().is64BitVector() &&
4900 "unexpected types for extended operands to VMULL");
4901 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
4902 }
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004903
Evan Cheng78fe9ab2011-03-29 01:56:09 +00004904 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
4905 // isel lowering to take advantage of no-stall back to back vmul + vmla.
4906 // vmull q0, d4, d6
4907 // vmlal q0, d5, d6
4908 // is faster than
4909 // vaddl q0, d4, d5
4910 // vmovl q1, d6
4911 // vmul q0, q0, q1
4912 SDValue N00 = SkipExtension(N0->getOperand(0).getNode(), DAG);
4913 SDValue N01 = SkipExtension(N0->getOperand(1).getNode(), DAG);
4914 EVT Op1VT = Op1.getValueType();
4915 return DAG.getNode(N0->getOpcode(), DL, VT,
4916 DAG.getNode(NewOpc, DL, VT,
4917 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
4918 DAG.getNode(NewOpc, DL, VT,
4919 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilsond0b69cf2010-09-01 23:50:19 +00004920}
4921
Owen Anderson76706012011-04-05 21:48:57 +00004922static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004923LowerSDIV_v4i8(SDValue X, SDValue Y, DebugLoc dl, SelectionDAG &DAG) {
4924 // Convert to float
4925 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
4926 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
4927 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
4928 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
4929 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
4930 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
4931 // Get reciprocal estimate.
4932 // float4 recip = vrecpeq_f32(yf);
Owen Anderson76706012011-04-05 21:48:57 +00004933 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004934 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
4935 // Because char has a smaller range than uchar, we can actually get away
4936 // without any newton steps. This requires that we use a weird bias
4937 // of 0xb000, however (again, this has been exhaustively tested).
4938 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
4939 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
4940 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
4941 Y = DAG.getConstant(0xb000, MVT::i32);
4942 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
4943 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
4944 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
4945 // Convert back to short.
4946 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
4947 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
4948 return X;
4949}
4950
Owen Anderson76706012011-04-05 21:48:57 +00004951static SDValue
Nate Begeman7973f352011-02-11 20:53:29 +00004952LowerSDIV_v4i16(SDValue N0, SDValue N1, DebugLoc dl, SelectionDAG &DAG) {
4953 SDValue N2;
4954 // Convert to float.
4955 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
4956 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
4957 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
4958 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
4959 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
4960 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson76706012011-04-05 21:48:57 +00004961
Nate Begeman7973f352011-02-11 20:53:29 +00004962 // Use reciprocal estimate and one refinement step.
4963 // float4 recip = vrecpeq_f32(yf);
4964 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00004965 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004966 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson76706012011-04-05 21:48:57 +00004967 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00004968 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
4969 N1, N2);
4970 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
4971 // Because short has a smaller range than ushort, we can actually get away
4972 // with only a single newton step. This requires that we use a weird bias
4973 // of 89, however (again, this has been exhaustively tested).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004974 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begeman7973f352011-02-11 20:53:29 +00004975 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
4976 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00004977 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begeman7973f352011-02-11 20:53:29 +00004978 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
4979 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
4980 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
4981 // Convert back to integer and return.
4982 // return vmovn_s32(vcvt_s32_f32(result));
4983 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
4984 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
4985 return N0;
4986}
4987
4988static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
4989 EVT VT = Op.getValueType();
4990 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
4991 "unexpected type for custom-lowering ISD::SDIV");
4992
4993 DebugLoc dl = Op.getDebugLoc();
4994 SDValue N0 = Op.getOperand(0);
4995 SDValue N1 = Op.getOperand(1);
4996 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00004997
Nate Begeman7973f352011-02-11 20:53:29 +00004998 if (VT == MVT::v8i8) {
4999 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5000 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005001
Nate Begeman7973f352011-02-11 20:53:29 +00005002 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5003 DAG.getIntPtrConstant(4));
5004 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005005 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005006 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5007 DAG.getIntPtrConstant(0));
5008 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5009 DAG.getIntPtrConstant(0));
5010
5011 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5012 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5013
5014 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5015 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005016
Nate Begeman7973f352011-02-11 20:53:29 +00005017 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5018 return N0;
5019 }
5020 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5021}
5022
5023static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5024 EVT VT = Op.getValueType();
5025 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5026 "unexpected type for custom-lowering ISD::UDIV");
5027
5028 DebugLoc dl = Op.getDebugLoc();
5029 SDValue N0 = Op.getOperand(0);
5030 SDValue N1 = Op.getOperand(1);
5031 SDValue N2, N3;
Owen Anderson76706012011-04-05 21:48:57 +00005032
Nate Begeman7973f352011-02-11 20:53:29 +00005033 if (VT == MVT::v8i8) {
5034 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5035 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson76706012011-04-05 21:48:57 +00005036
Nate Begeman7973f352011-02-11 20:53:29 +00005037 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5038 DAG.getIntPtrConstant(4));
5039 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson76706012011-04-05 21:48:57 +00005040 DAG.getIntPtrConstant(4));
Nate Begeman7973f352011-02-11 20:53:29 +00005041 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5042 DAG.getIntPtrConstant(0));
5043 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5044 DAG.getIntPtrConstant(0));
Owen Anderson76706012011-04-05 21:48:57 +00005045
Nate Begeman7973f352011-02-11 20:53:29 +00005046 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5047 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson76706012011-04-05 21:48:57 +00005048
Nate Begeman7973f352011-02-11 20:53:29 +00005049 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5050 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson76706012011-04-05 21:48:57 +00005051
5052 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begeman7973f352011-02-11 20:53:29 +00005053 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5054 N0);
5055 return N0;
5056 }
Owen Anderson76706012011-04-05 21:48:57 +00005057
Nate Begeman7973f352011-02-11 20:53:29 +00005058 // v4i16 sdiv ... Convert to float.
5059 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5060 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5061 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5062 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5063 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005064 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begeman7973f352011-02-11 20:53:29 +00005065
5066 // Use reciprocal estimate and two refinement steps.
5067 // float4 recip = vrecpeq_f32(yf);
5068 // recip *= vrecpsq_f32(yf, recip);
5069 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson76706012011-04-05 21:48:57 +00005070 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005071 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson76706012011-04-05 21:48:57 +00005072 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005073 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005074 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005075 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson76706012011-04-05 21:48:57 +00005076 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begeman7973f352011-02-11 20:53:29 +00005077 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005078 BN1, N2);
Nate Begeman7973f352011-02-11 20:53:29 +00005079 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5080 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5081 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5082 // and that it will never cause us to return an answer too large).
Mon P Wang28e2b1d2011-05-19 04:15:07 +00005083 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begeman7973f352011-02-11 20:53:29 +00005084 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5085 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5086 N1 = DAG.getConstant(2, MVT::i32);
5087 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5088 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5089 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5090 // Convert back to integer and return.
5091 // return vmovn_u32(vcvt_s32_f32(result));
5092 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5093 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5094 return N0;
5095}
5096
Evan Cheng342e3162011-08-30 01:34:54 +00005097static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5098 EVT VT = Op.getNode()->getValueType(0);
5099 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5100
5101 unsigned Opc;
5102 bool ExtraOp = false;
5103 switch (Op.getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00005104 default: llvm_unreachable("Invalid code");
Evan Cheng342e3162011-08-30 01:34:54 +00005105 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5106 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5107 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5108 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5109 }
5110
5111 if (!ExtraOp)
5112 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5113 Op.getOperand(1));
5114 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
5115 Op.getOperand(1), Op.getOperand(2));
5116}
5117
Eli Friedman74bf18c2011-09-15 22:26:18 +00005118static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedman7cc15662011-09-15 22:18:49 +00005119 // Monotonic load/store is legal for all targets
5120 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5121 return Op;
5122
5123 // Aquire/Release load/store is not legal for targets without a
5124 // dmb or equivalent available.
5125 return SDValue();
5126}
5127
5128
Eli Friedman2bdffe42011-08-31 00:31:29 +00005129static void
Eli Friedman4d3f3292011-08-31 17:52:22 +00005130ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5131 SelectionDAG &DAG, unsigned NewOp) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005132 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +00005133 assert (Node->getValueType(0) == MVT::i64 &&
5134 "Only know how to expand i64 atomics");
Eli Friedman2bdffe42011-08-31 00:31:29 +00005135
Eli Friedman4d3f3292011-08-31 17:52:22 +00005136 SmallVector<SDValue, 6> Ops;
5137 Ops.push_back(Node->getOperand(0)); // Chain
5138 Ops.push_back(Node->getOperand(1)); // Ptr
5139 // Low part of Val1
5140 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5141 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5142 // High part of Val1
5143 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5144 Node->getOperand(2), DAG.getIntPtrConstant(1)));
Andrew Trick3af7a672011-09-20 03:06:13 +00005145 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
Eli Friedman4d3f3292011-08-31 17:52:22 +00005146 // High part of Val1
5147 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5148 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5149 // High part of Val2
5150 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5151 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5152 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005153 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5154 SDValue Result =
Eli Friedman4d3f3292011-08-31 17:52:22 +00005155 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
Eli Friedman2bdffe42011-08-31 00:31:29 +00005156 cast<MemSDNode>(Node)->getMemOperand());
Eli Friedman4d3f3292011-08-31 17:52:22 +00005157 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
Eli Friedman2bdffe42011-08-31 00:31:29 +00005158 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5159 Results.push_back(Result.getValue(2));
5160}
5161
Dan Gohmand858e902010-04-17 15:26:15 +00005162SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga8e29892007-01-19 07:51:42 +00005163 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005164 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Chenga8e29892007-01-19 07:51:42 +00005165 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilsonddb16df2009-10-30 05:45:42 +00005166 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005167 case ISD::GlobalAddress:
5168 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5169 LowerGlobalAddressELF(Op, DAG);
Bill Wendling69a05a72011-03-14 23:02:38 +00005170 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingde2b1512010-08-11 08:43:16 +00005171 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng06b53c02009-11-12 07:13:11 +00005172 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5173 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005174 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman1e93df62010-04-17 14:41:14 +00005175 case ISD::VASTART: return LowerVASTART(Op, DAG);
Jim Grosbach7c03dbd2009-12-14 21:24:16 +00005176 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG, Subtarget);
Eli Friedman14648462011-07-27 22:21:52 +00005177 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Chengdfed19f2010-11-03 06:34:55 +00005178 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilson76a312b2010-03-19 22:51:32 +00005179 case ISD::SINT_TO_FP:
5180 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5181 case ISD::FP_TO_SINT:
5182 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005183 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng2457f2c2010-05-22 01:47:14 +00005184 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbach0e0da732009-05-12 23:59:14 +00005185 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +00005186 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00005187 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbach5eb19512010-05-22 01:06:18 +00005188 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha87ded22010-02-08 23:22:00 +00005189 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5190 Subtarget);
Evan Cheng21a61792011-03-14 18:02:30 +00005191 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005192 case ISD::SHL:
Chris Lattner27a6c732007-11-24 07:07:01 +00005193 case ISD::SRL:
Bob Wilson5bafff32009-06-22 23:27:02 +00005194 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng06b53c02009-11-12 07:13:11 +00005195 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbachbcf2f2c2009-10-31 21:42:19 +00005196 case ISD::SRL_PARTS:
Evan Cheng06b53c02009-11-12 07:13:11 +00005197 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach3482c802010-01-18 19:58:49 +00005198 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Duncan Sands28b77e92011-09-06 19:07:46 +00005199 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hames45b5f882012-03-15 18:49:02 +00005200 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesenf630c712010-07-29 20:10:08 +00005201 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00005202 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedman5c89cb82011-10-24 23:08:52 +00005203 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00005204 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsona6d65862009-08-03 20:36:38 +00005205 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilsonb31a11b2010-08-20 04:54:02 +00005206 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilsond0b69cf2010-09-01 23:50:19 +00005207 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begeman7973f352011-02-11 20:53:29 +00005208 case ISD::SDIV: return LowerSDIV(Op, DAG);
5209 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Cheng342e3162011-08-30 01:34:54 +00005210 case ISD::ADDC:
5211 case ISD::ADDE:
5212 case ISD::SUBC:
5213 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Eli Friedman7cc15662011-09-15 22:18:49 +00005214 case ISD::ATOMIC_LOAD:
Eli Friedman74bf18c2011-09-15 22:26:18 +00005215 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Evan Chenga8e29892007-01-19 07:51:42 +00005216 }
Evan Chenga8e29892007-01-19 07:51:42 +00005217}
5218
Duncan Sands1607f052008-12-01 11:39:25 +00005219/// ReplaceNodeResults - Replace the results of node with an illegal result
5220/// type with new values built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00005221void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5222 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005223 SelectionDAG &DAG) const {
Bob Wilson164cd8b2010-04-14 20:45:23 +00005224 SDValue Res;
Chris Lattner27a6c732007-11-24 07:07:01 +00005225 switch (N->getOpcode()) {
Duncan Sands1607f052008-12-01 11:39:25 +00005226 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00005227 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005228 case ISD::BITCAST:
5229 Res = ExpandBITCAST(N, DAG);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005230 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005231 case ISD::SRL:
Bob Wilson164cd8b2010-04-14 20:45:23 +00005232 case ISD::SRA:
Bob Wilsond5448bb2010-11-18 21:16:28 +00005233 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilson164cd8b2010-04-14 20:45:23 +00005234 break;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005235 case ISD::ATOMIC_LOAD_ADD:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005236 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005237 return;
5238 case ISD::ATOMIC_LOAD_AND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005239 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005240 return;
5241 case ISD::ATOMIC_LOAD_NAND:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005242 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005243 return;
5244 case ISD::ATOMIC_LOAD_OR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005245 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005246 return;
5247 case ISD::ATOMIC_LOAD_SUB:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005248 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005249 return;
5250 case ISD::ATOMIC_LOAD_XOR:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005251 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005252 return;
5253 case ISD::ATOMIC_SWAP:
Eli Friedman4d3f3292011-08-31 17:52:22 +00005254 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005255 return;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005256 case ISD::ATOMIC_CMP_SWAP:
5257 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5258 return;
Duncan Sands1607f052008-12-01 11:39:25 +00005259 }
Bob Wilson164cd8b2010-04-14 20:45:23 +00005260 if (Res.getNode())
5261 Results.push_back(Res);
Chris Lattner27a6c732007-11-24 07:07:01 +00005262}
Chris Lattner27a6c732007-11-24 07:07:01 +00005263
Evan Chenga8e29892007-01-19 07:51:42 +00005264//===----------------------------------------------------------------------===//
5265// ARM Scheduler Hooks
5266//===----------------------------------------------------------------------===//
5267
5268MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005269ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
5270 MachineBasicBlock *BB,
5271 unsigned Size) const {
Jim Grosbach5278eb82009-12-11 01:42:04 +00005272 unsigned dest = MI->getOperand(0).getReg();
5273 unsigned ptr = MI->getOperand(1).getReg();
5274 unsigned oldval = MI->getOperand(2).getReg();
5275 unsigned newval = MI->getOperand(3).getReg();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005276 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5277 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005278 bool isThumb2 = Subtarget->isThumb2();
Jim Grosbach5278eb82009-12-11 01:42:04 +00005279
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005280 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Craig Topper420761a2012-04-20 07:30:17 +00005281 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
5282 (const TargetRegisterClass*)&ARM::rGPRRegClass :
5283 (const TargetRegisterClass*)&ARM::GPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005284
5285 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005286 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5287 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
5288 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
Cameron Zwarich7d336c02011-05-18 02:20:07 +00005289 }
5290
Jim Grosbach5278eb82009-12-11 01:42:04 +00005291 unsigned ldrOpc, strOpc;
5292 switch (Size) {
5293 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005294 case 1:
5295 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Evan Chengaa261022011-02-07 18:50:47 +00005296 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005297 break;
5298 case 2:
5299 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5300 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5301 break;
5302 case 4:
5303 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5304 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5305 break;
Jim Grosbach5278eb82009-12-11 01:42:04 +00005306 }
5307
5308 MachineFunction *MF = BB->getParent();
5309 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5310 MachineFunction::iterator It = BB;
5311 ++It; // insert the new blocks after the current block
5312
5313 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5314 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
5315 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5316 MF->insert(It, loop1MBB);
5317 MF->insert(It, loop2MBB);
5318 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005319
5320 // Transfer the remainder of BB and its successor edges to exitMBB.
5321 exitMBB->splice(exitMBB->begin(), BB,
5322 llvm::next(MachineBasicBlock::iterator(MI)),
5323 BB->end());
5324 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005325
5326 // thisMBB:
5327 // ...
5328 // fallthrough --> loop1MBB
5329 BB->addSuccessor(loop1MBB);
5330
5331 // loop1MBB:
5332 // ldrex dest, [ptr]
5333 // cmp dest, oldval
5334 // bne exitMBB
5335 BB = loop1MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005336 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5337 if (ldrOpc == ARM::t2LDREX)
5338 MIB.addImm(0);
5339 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005340 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005341 .addReg(dest).addReg(oldval));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005342 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5343 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005344 BB->addSuccessor(loop2MBB);
5345 BB->addSuccessor(exitMBB);
5346
5347 // loop2MBB:
5348 // strex scratch, newval, [ptr]
5349 // cmp scratch, #0
5350 // bne loop1MBB
5351 BB = loop2MBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005352 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
5353 if (strOpc == ARM::t2STREX)
5354 MIB.addImm(0);
5355 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005356 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbach5278eb82009-12-11 01:42:04 +00005357 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005358 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5359 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbach5278eb82009-12-11 01:42:04 +00005360 BB->addSuccessor(loop1MBB);
5361 BB->addSuccessor(exitMBB);
5362
5363 // exitMBB:
5364 // ...
5365 BB = exitMBB;
Jim Grosbach5efaed32010-01-15 00:18:34 +00005366
Dan Gohman14152b42010-07-06 20:24:04 +00005367 MI->eraseFromParent(); // The instruction is gone now.
Jim Grosbach5efaed32010-01-15 00:18:34 +00005368
Jim Grosbach5278eb82009-12-11 01:42:04 +00005369 return BB;
5370}
5371
5372MachineBasicBlock *
Jim Grosbache801dc42009-12-12 01:40:06 +00005373ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
5374 unsigned Size, unsigned BinOpcode) const {
Jim Grosbachc3c23542009-12-14 04:22:04 +00005375 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
5376 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5377
5378 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005379 MachineFunction *MF = BB->getParent();
Jim Grosbachc3c23542009-12-14 04:22:04 +00005380 MachineFunction::iterator It = BB;
5381 ++It;
5382
5383 unsigned dest = MI->getOperand(0).getReg();
5384 unsigned ptr = MI->getOperand(1).getReg();
5385 unsigned incr = MI->getOperand(2).getReg();
5386 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005387 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005388
5389 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5390 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005391 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5392 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005393 }
5394
Jim Grosbachc3c23542009-12-14 04:22:04 +00005395 unsigned ldrOpc, strOpc;
5396 switch (Size) {
5397 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005398 case 1:
5399 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
Jakob Stoklund Olesen15913c92010-01-13 19:54:39 +00005400 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005401 break;
5402 case 2:
5403 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5404 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
5405 break;
5406 case 4:
5407 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5408 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5409 break;
Jim Grosbachc3c23542009-12-14 04:22:04 +00005410 }
5411
Jim Grosbach867bbbf2010-01-15 00:22:18 +00005412 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5413 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5414 MF->insert(It, loopMBB);
5415 MF->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005416
5417 // Transfer the remainder of BB and its successor edges to exitMBB.
5418 exitMBB->splice(exitMBB->begin(), BB,
5419 llvm::next(MachineBasicBlock::iterator(MI)),
5420 BB->end());
5421 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005422
Craig Topper420761a2012-04-20 07:30:17 +00005423 const TargetRegisterClass *TRC = isThumb2 ?
5424 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5425 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005426 unsigned scratch = MRI.createVirtualRegister(TRC);
5427 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005428
5429 // thisMBB:
5430 // ...
5431 // fallthrough --> loopMBB
5432 BB->addSuccessor(loopMBB);
5433
5434 // loopMBB:
5435 // ldrex dest, ptr
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005436 // <binop> scratch2, dest, incr
5437 // strex scratch, scratch2, ptr
Jim Grosbachc3c23542009-12-14 04:22:04 +00005438 // cmp scratch, #0
5439 // bne- loopMBB
5440 // fallthrough --> exitMBB
5441 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005442 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5443 if (ldrOpc == ARM::t2LDREX)
5444 MIB.addImm(0);
5445 AddDefaultPred(MIB);
Jim Grosbachc67b5562009-12-15 00:12:35 +00005446 if (BinOpcode) {
5447 // operand order needs to go the other way for NAND
5448 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
5449 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5450 addReg(incr).addReg(dest)).addReg(0);
5451 else
5452 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
5453 addReg(dest).addReg(incr)).addReg(0);
5454 }
Jim Grosbachc3c23542009-12-14 04:22:04 +00005455
Jim Grosbachb6aed502011-09-09 18:37:27 +00005456 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5457 if (strOpc == ARM::t2STREX)
5458 MIB.addImm(0);
5459 AddDefaultPred(MIB);
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005460 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
Jim Grosbachc3c23542009-12-14 04:22:04 +00005461 .addReg(scratch).addImm(0));
Jim Grosbacha36c8f22009-12-14 20:14:59 +00005462 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5463 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Jim Grosbachc3c23542009-12-14 04:22:04 +00005464
5465 BB->addSuccessor(loopMBB);
5466 BB->addSuccessor(exitMBB);
5467
5468 // exitMBB:
5469 // ...
5470 BB = exitMBB;
Evan Cheng102ebf12009-12-21 19:53:39 +00005471
Dan Gohman14152b42010-07-06 20:24:04 +00005472 MI->eraseFromParent(); // The instruction is gone now.
Evan Cheng102ebf12009-12-21 19:53:39 +00005473
Jim Grosbachc3c23542009-12-14 04:22:04 +00005474 return BB;
Jim Grosbache801dc42009-12-12 01:40:06 +00005475}
5476
Jim Grosbachf7da8822011-04-26 19:44:18 +00005477MachineBasicBlock *
5478ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
5479 MachineBasicBlock *BB,
5480 unsigned Size,
5481 bool signExtend,
5482 ARMCC::CondCodes Cond) const {
5483 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5484
5485 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5486 MachineFunction *MF = BB->getParent();
5487 MachineFunction::iterator It = BB;
5488 ++It;
5489
5490 unsigned dest = MI->getOperand(0).getReg();
5491 unsigned ptr = MI->getOperand(1).getReg();
5492 unsigned incr = MI->getOperand(2).getReg();
5493 unsigned oldval = dest;
5494 DebugLoc dl = MI->getDebugLoc();
Jim Grosbachf7da8822011-04-26 19:44:18 +00005495 bool isThumb2 = Subtarget->isThumb2();
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005496
5497 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5498 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005499 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
5500 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005501 }
5502
Jim Grosbachf7da8822011-04-26 19:44:18 +00005503 unsigned ldrOpc, strOpc, extendOpc;
5504 switch (Size) {
5505 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
5506 case 1:
5507 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
5508 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005509 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005510 break;
5511 case 2:
5512 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
5513 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005514 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
Jim Grosbachf7da8822011-04-26 19:44:18 +00005515 break;
5516 case 4:
5517 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
5518 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
5519 extendOpc = 0;
5520 break;
5521 }
5522
5523 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5524 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5525 MF->insert(It, loopMBB);
5526 MF->insert(It, exitMBB);
5527
5528 // Transfer the remainder of BB and its successor edges to exitMBB.
5529 exitMBB->splice(exitMBB->begin(), BB,
5530 llvm::next(MachineBasicBlock::iterator(MI)),
5531 BB->end());
5532 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5533
Craig Topper420761a2012-04-20 07:30:17 +00005534 const TargetRegisterClass *TRC = isThumb2 ?
5535 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5536 (const TargetRegisterClass*)&ARM::GPRRegClass;
Cameron Zwarichde64aaf2011-05-27 23:54:00 +00005537 unsigned scratch = MRI.createVirtualRegister(TRC);
5538 unsigned scratch2 = MRI.createVirtualRegister(TRC);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005539
5540 // thisMBB:
5541 // ...
5542 // fallthrough --> loopMBB
5543 BB->addSuccessor(loopMBB);
5544
5545 // loopMBB:
5546 // ldrex dest, ptr
5547 // (sign extend dest, if required)
5548 // cmp dest, incr
5549 // cmov.cond scratch2, dest, incr
5550 // strex scratch, scratch2, ptr
5551 // cmp scratch, #0
5552 // bne- loopMBB
5553 // fallthrough --> exitMBB
5554 BB = loopMBB;
Jim Grosbachb6aed502011-09-09 18:37:27 +00005555 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
5556 if (ldrOpc == ARM::t2LDREX)
5557 MIB.addImm(0);
5558 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005559
5560 // Sign extend the value, if necessary.
5561 if (signExtend && extendOpc) {
Craig Topper420761a2012-04-20 07:30:17 +00005562 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
Jim Grosbachc5a8c862011-07-27 16:47:19 +00005563 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
5564 .addReg(dest)
5565 .addImm(0));
Jim Grosbachf7da8822011-04-26 19:44:18 +00005566 }
5567
5568 // Build compare and cmov instructions.
5569 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
5570 .addReg(oldval).addReg(incr));
5571 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
5572 .addReg(oldval).addReg(incr).addImm(Cond).addReg(ARM::CPSR);
5573
Jim Grosbachb6aed502011-09-09 18:37:27 +00005574 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
5575 if (strOpc == ARM::t2STREX)
5576 MIB.addImm(0);
5577 AddDefaultPred(MIB);
Jim Grosbachf7da8822011-04-26 19:44:18 +00005578 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5579 .addReg(scratch).addImm(0));
5580 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5581 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5582
5583 BB->addSuccessor(loopMBB);
5584 BB->addSuccessor(exitMBB);
5585
5586 // exitMBB:
5587 // ...
5588 BB = exitMBB;
5589
5590 MI->eraseFromParent(); // The instruction is gone now.
5591
5592 return BB;
5593}
5594
Eli Friedman2bdffe42011-08-31 00:31:29 +00005595MachineBasicBlock *
5596ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
5597 unsigned Op1, unsigned Op2,
Eli Friedman4d3f3292011-08-31 17:52:22 +00005598 bool NeedsCarry, bool IsCmpxchg) const {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005599 // This also handles ATOMIC_SWAP, indicated by Op1==0.
5600 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5601
5602 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5603 MachineFunction *MF = BB->getParent();
5604 MachineFunction::iterator It = BB;
5605 ++It;
5606
5607 unsigned destlo = MI->getOperand(0).getReg();
5608 unsigned desthi = MI->getOperand(1).getReg();
5609 unsigned ptr = MI->getOperand(2).getReg();
5610 unsigned vallo = MI->getOperand(3).getReg();
5611 unsigned valhi = MI->getOperand(4).getReg();
5612 DebugLoc dl = MI->getDebugLoc();
5613 bool isThumb2 = Subtarget->isThumb2();
5614
5615 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
5616 if (isThumb2) {
Craig Topper420761a2012-04-20 07:30:17 +00005617 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
5618 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
5619 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
Eli Friedman2bdffe42011-08-31 00:31:29 +00005620 }
5621
5622 unsigned ldrOpc = isThumb2 ? ARM::t2LDREXD : ARM::LDREXD;
5623 unsigned strOpc = isThumb2 ? ARM::t2STREXD : ARM::STREXD;
5624
5625 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Eli Friedman7df496d2011-09-01 22:27:41 +00005626 MachineBasicBlock *contBB = 0, *cont2BB = 0;
Eli Friedman4d3f3292011-08-31 17:52:22 +00005627 if (IsCmpxchg) {
5628 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
5629 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
5630 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005631 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
5632 MF->insert(It, loopMBB);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005633 if (IsCmpxchg) {
5634 MF->insert(It, contBB);
5635 MF->insert(It, cont2BB);
5636 }
Eli Friedman2bdffe42011-08-31 00:31:29 +00005637 MF->insert(It, exitMBB);
5638
5639 // Transfer the remainder of BB and its successor edges to exitMBB.
5640 exitMBB->splice(exitMBB->begin(), BB,
5641 llvm::next(MachineBasicBlock::iterator(MI)),
5642 BB->end());
5643 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
5644
Craig Topper420761a2012-04-20 07:30:17 +00005645 const TargetRegisterClass *TRC = isThumb2 ?
5646 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5647 (const TargetRegisterClass*)&ARM::GPRRegClass;
Eli Friedman2bdffe42011-08-31 00:31:29 +00005648 unsigned storesuccess = MRI.createVirtualRegister(TRC);
5649
5650 // thisMBB:
5651 // ...
5652 // fallthrough --> loopMBB
5653 BB->addSuccessor(loopMBB);
5654
5655 // loopMBB:
5656 // ldrexd r2, r3, ptr
5657 // <binopa> r0, r2, incr
5658 // <binopb> r1, r3, incr
5659 // strexd storesuccess, r0, r1, ptr
5660 // cmp storesuccess, #0
5661 // bne- loopMBB
5662 // fallthrough --> exitMBB
5663 //
5664 // Note that the registers are explicitly specified because there is not any
5665 // way to force the register allocator to allocate a register pair.
5666 //
Andrew Trick3af7a672011-09-20 03:06:13 +00005667 // FIXME: The hardcoded registers are not necessary for Thumb2, but we
Eli Friedman2bdffe42011-08-31 00:31:29 +00005668 // need to properly enforce the restriction that the two output registers
5669 // for ldrexd must be different.
5670 BB = loopMBB;
5671 // Load
5672 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc))
5673 .addReg(ARM::R2, RegState::Define)
5674 .addReg(ARM::R3, RegState::Define).addReg(ptr));
5675 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
5676 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo).addReg(ARM::R2);
5677 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi).addReg(ARM::R3);
Eli Friedman4d3f3292011-08-31 17:52:22 +00005678
5679 if (IsCmpxchg) {
5680 // Add early exit
5681 for (unsigned i = 0; i < 2; i++) {
5682 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
5683 ARM::CMPrr))
5684 .addReg(i == 0 ? destlo : desthi)
5685 .addReg(i == 0 ? vallo : valhi));
5686 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5687 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5688 BB->addSuccessor(exitMBB);
5689 BB->addSuccessor(i == 0 ? contBB : cont2BB);
5690 BB = (i == 0 ? contBB : cont2BB);
5691 }
5692
5693 // Copy to physregs for strexd
5694 unsigned setlo = MI->getOperand(5).getReg();
5695 unsigned sethi = MI->getOperand(6).getReg();
5696 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(setlo);
5697 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(sethi);
5698 } else if (Op1) {
Eli Friedman2bdffe42011-08-31 00:31:29 +00005699 // Perform binary operation
5700 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), ARM::R0)
5701 .addReg(destlo).addReg(vallo))
5702 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
5703 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), ARM::R1)
5704 .addReg(desthi).addReg(valhi)).addReg(0);
5705 } else {
5706 // Copy to physregs for strexd
5707 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R0).addReg(vallo);
5708 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), ARM::R1).addReg(valhi);
5709 }
5710
5711 // Store
5712 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), storesuccess)
5713 .addReg(ARM::R0).addReg(ARM::R1).addReg(ptr));
5714 // Cmp+jump
5715 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
5716 .addReg(storesuccess).addImm(0));
5717 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
5718 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
5719
5720 BB->addSuccessor(loopMBB);
5721 BB->addSuccessor(exitMBB);
5722
5723 // exitMBB:
5724 // ...
5725 BB = exitMBB;
5726
5727 MI->eraseFromParent(); // The instruction is gone now.
5728
5729 return BB;
5730}
5731
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005732/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
5733/// registers the function context.
5734void ARMTargetLowering::
5735SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
5736 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005737 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5738 DebugLoc dl = MI->getDebugLoc();
5739 MachineFunction *MF = MBB->getParent();
5740 MachineRegisterInfo *MRI = &MF->getRegInfo();
5741 MachineConstantPool *MCP = MF->getConstantPool();
5742 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5743 const Function *F = MF->getFunction();
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005744
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005745 bool isThumb = Subtarget->isThumb();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005746 bool isThumb2 = Subtarget->isThumb2();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005747
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005748 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendlingff4216a2011-10-03 22:44:15 +00005749 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005750 ARMConstantPoolValue *CPV =
5751 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
5752 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
5753
Craig Topper420761a2012-04-20 07:30:17 +00005754 const TargetRegisterClass *TRC = isThumb ?
5755 (const TargetRegisterClass*)&ARM::tGPRRegClass :
5756 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00005757
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005758 // Grab constant pool and fixed stack memory operands.
5759 MachineMemOperand *CPMMO =
5760 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
5761 MachineMemOperand::MOLoad, 4, 4);
5762
5763 MachineMemOperand *FIMMOSt =
5764 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
5765 MachineMemOperand::MOStore, 4, 4);
5766
5767 // Load the address of the dispatch MBB into the jump buffer.
5768 if (isThumb2) {
5769 // Incoming value: jbuf
5770 // ldr.n r5, LCPI1_1
5771 // orr r5, r5, #1
5772 // add r5, pc
5773 // str r5, [$jbuf, #+4] ; &jbuf[1]
5774 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5775 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
5776 .addConstantPoolIndex(CPI)
5777 .addMemOperand(CPMMO));
5778 // Set the low bit because of thumb mode.
5779 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5780 AddDefaultCC(
5781 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
5782 .addReg(NewVReg1, RegState::Kill)
5783 .addImm(0x01)));
5784 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5785 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
5786 .addReg(NewVReg2, RegState::Kill)
5787 .addImm(PCLabelId);
5788 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
5789 .addReg(NewVReg3, RegState::Kill)
5790 .addFrameIndex(FI)
5791 .addImm(36) // &jbuf[1] :: pc
5792 .addMemOperand(FIMMOSt));
5793 } else if (isThumb) {
5794 // Incoming value: jbuf
5795 // ldr.n r1, LCPI1_4
5796 // add r1, pc
5797 // mov r2, #1
5798 // orrs r1, r2
5799 // add r2, $jbuf, #+4 ; &jbuf[1]
5800 // str r1, [r2]
5801 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5802 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
5803 .addConstantPoolIndex(CPI)
5804 .addMemOperand(CPMMO));
5805 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5806 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
5807 .addReg(NewVReg1, RegState::Kill)
5808 .addImm(PCLabelId);
5809 // Set the low bit because of thumb mode.
5810 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5811 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
5812 .addReg(ARM::CPSR, RegState::Define)
5813 .addImm(1));
5814 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
5815 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
5816 .addReg(ARM::CPSR, RegState::Define)
5817 .addReg(NewVReg2, RegState::Kill)
5818 .addReg(NewVReg3, RegState::Kill));
5819 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
5820 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
5821 .addFrameIndex(FI)
5822 .addImm(36)); // &jbuf[1] :: pc
5823 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
5824 .addReg(NewVReg4, RegState::Kill)
5825 .addReg(NewVReg5, RegState::Kill)
5826 .addImm(0)
5827 .addMemOperand(FIMMOSt));
5828 } else {
5829 // Incoming value: jbuf
5830 // ldr r1, LCPI1_1
5831 // add r1, pc, r1
5832 // str r1, [$jbuf, #+4] ; &jbuf[1]
5833 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5834 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
5835 .addConstantPoolIndex(CPI)
5836 .addImm(0)
5837 .addMemOperand(CPMMO));
5838 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
5839 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
5840 .addReg(NewVReg1, RegState::Kill)
5841 .addImm(PCLabelId));
5842 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
5843 .addReg(NewVReg2, RegState::Kill)
5844 .addFrameIndex(FI)
5845 .addImm(36) // &jbuf[1] :: pc
5846 .addMemOperand(FIMMOSt));
5847 }
5848}
5849
5850MachineBasicBlock *ARMTargetLowering::
5851EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
5852 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5853 DebugLoc dl = MI->getDebugLoc();
5854 MachineFunction *MF = MBB->getParent();
5855 MachineRegisterInfo *MRI = &MF->getRegInfo();
5856 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
5857 MachineFrameInfo *MFI = MF->getFrameInfo();
5858 int FI = MFI->getFunctionContextIndex();
5859
Craig Topper420761a2012-04-20 07:30:17 +00005860 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
5861 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen027c32a2012-05-20 06:38:47 +00005862 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005863
Bill Wendling04f15b42011-10-06 21:29:56 +00005864 // Get a mapping of the call site numbers to all of the landing pads they're
5865 // associated with.
Bill Wendling2a850152011-10-05 00:02:33 +00005866 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
5867 unsigned MaxCSNum = 0;
5868 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbachd4f020a2012-04-06 23:43:50 +00005869 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
5870 ++BB) {
Bill Wendling2a850152011-10-05 00:02:33 +00005871 if (!BB->isLandingPad()) continue;
5872
5873 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
5874 // pad.
5875 for (MachineBasicBlock::iterator
5876 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
5877 if (!II->isEHLabel()) continue;
5878
5879 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendling5cbef192011-10-05 23:28:57 +00005880 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling2a850152011-10-05 00:02:33 +00005881
Bill Wendling5cbef192011-10-05 23:28:57 +00005882 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
5883 for (SmallVectorImpl<unsigned>::iterator
5884 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
5885 CSI != CSE; ++CSI) {
5886 CallSiteNumToLPad[*CSI].push_back(BB);
5887 MaxCSNum = std::max(MaxCSNum, *CSI);
5888 }
Bill Wendling2a850152011-10-05 00:02:33 +00005889 break;
5890 }
5891 }
5892
5893 // Get an ordered list of the machine basic blocks for the jump table.
5894 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling2acf6382011-10-07 23:18:02 +00005895 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling2a850152011-10-05 00:02:33 +00005896 LPadList.reserve(CallSiteNumToLPad.size());
5897 for (unsigned I = 1; I <= MaxCSNum; ++I) {
5898 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
5899 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00005900 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling2a850152011-10-05 00:02:33 +00005901 LPadList.push_back(*II);
Bill Wendling2acf6382011-10-07 23:18:02 +00005902 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
5903 }
Bill Wendling2a850152011-10-05 00:02:33 +00005904 }
5905
Bill Wendling5cbef192011-10-05 23:28:57 +00005906 assert(!LPadList.empty() &&
5907 "No landing pad destinations for the dispatch jump table!");
5908
Bill Wendling04f15b42011-10-06 21:29:56 +00005909 // Create the jump table and associated information.
Bill Wendling2a850152011-10-05 00:02:33 +00005910 MachineJumpTableInfo *JTI =
5911 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
5912 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
5913 unsigned UId = AFI->createJumpTableUId();
5914
Bill Wendling04f15b42011-10-06 21:29:56 +00005915 // Create the MBBs for the dispatch code.
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005916
5917 // Shove the dispatch's address into the return slot in the function context.
5918 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
5919 DispatchBB->setIsLandingPad();
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005920
Bill Wendlingbb734682011-10-05 00:39:32 +00005921 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Bill Wendling083a8eb2011-10-06 23:37:36 +00005922 BuildMI(TrapBB, dl, TII->get(Subtarget->isThumb() ? ARM::tTRAP : ARM::TRAP));
Bill Wendlingbb734682011-10-05 00:39:32 +00005923 DispatchBB->addSuccessor(TrapBB);
5924
5925 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
5926 DispatchBB->addSuccessor(DispContBB);
Bill Wendling2a850152011-10-05 00:02:33 +00005927
Bill Wendlinga48ed4f2011-10-17 21:32:56 +00005928 // Insert and MBBs.
Bill Wendling930193c2011-10-06 00:53:33 +00005929 MF->insert(MF->end(), DispatchBB);
5930 MF->insert(MF->end(), DispContBB);
5931 MF->insert(MF->end(), TrapBB);
Bill Wendling930193c2011-10-06 00:53:33 +00005932
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005933 // Insert code into the entry block that creates and registers the function
5934 // context.
5935 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
5936
Bill Wendlinge29fa1d2011-10-06 22:18:16 +00005937 MachineMemOperand *FIMMOLd =
Bill Wendling04f15b42011-10-06 21:29:56 +00005938 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendling083a8eb2011-10-06 23:37:36 +00005939 MachineMemOperand::MOLoad |
5940 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling930193c2011-10-06 00:53:33 +00005941
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005942 if (AFI->isThumb1OnlyFunction())
5943 BuildMI(DispatchBB, dl, TII->get(ARM::tInt_eh_sjlj_dispatchsetup));
5944 else if (!Subtarget->hasVFP2())
5945 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup_nofp));
Lang Hamesc0a9f822012-03-29 21:56:11 +00005946 else
Bob Wilsonf4aea8f2011-12-22 23:39:48 +00005947 BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
Bob Wilsoneaab6ef2011-11-16 07:11:57 +00005948
Bill Wendling952cb502011-10-18 22:49:07 +00005949 unsigned NumLPads = LPadList.size();
Bill Wendling95ce2e92011-10-06 22:53:00 +00005950 if (Subtarget->isThumb2()) {
5951 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
5952 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
5953 .addFrameIndex(FI)
5954 .addImm(4)
5955 .addMemOperand(FIMMOLd));
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005956
Bill Wendling952cb502011-10-18 22:49:07 +00005957 if (NumLPads < 256) {
5958 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
5959 .addReg(NewVReg1)
5960 .addImm(LPadList.size()));
5961 } else {
5962 unsigned VReg1 = MRI->createVirtualRegister(TRC);
5963 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00005964 .addImm(NumLPads & 0xFFFF));
5965
5966 unsigned VReg2 = VReg1;
5967 if ((NumLPads & 0xFFFF0000) != 0) {
5968 VReg2 = MRI->createVirtualRegister(TRC);
5969 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
5970 .addReg(VReg1)
5971 .addImm(NumLPads >> 16));
5972 }
5973
Bill Wendling952cb502011-10-18 22:49:07 +00005974 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
5975 .addReg(NewVReg1)
5976 .addReg(VReg2));
5977 }
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005978
Bill Wendling95ce2e92011-10-06 22:53:00 +00005979 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
5980 .addMBB(TrapBB)
5981 .addImm(ARMCC::HI)
5982 .addReg(ARM::CPSR);
Bill Wendlingbb734682011-10-05 00:39:32 +00005983
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005984 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
5985 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005986 .addJumpTableIndex(MJTI)
5987 .addImm(UId));
Bill Wendling2a850152011-10-05 00:02:33 +00005988
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005989 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00005990 AddDefaultCC(
5991 AddDefaultPred(
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005992 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
5993 .addReg(NewVReg3, RegState::Kill)
Bill Wendling95ce2e92011-10-06 22:53:00 +00005994 .addReg(NewVReg1)
5995 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
5996
5997 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb9fecf42011-10-18 21:55:58 +00005998 .addReg(NewVReg4, RegState::Kill)
Bill Wendling2a850152011-10-05 00:02:33 +00005999 .addReg(NewVReg1)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006000 .addJumpTableIndex(MJTI)
6001 .addImm(UId);
6002 } else if (Subtarget->isThumb()) {
Bill Wendling083a8eb2011-10-06 23:37:36 +00006003 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6004 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6005 .addFrameIndex(FI)
6006 .addImm(1)
6007 .addMemOperand(FIMMOLd));
Bill Wendlingf1083d42011-10-07 22:08:37 +00006008
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006009 if (NumLPads < 256) {
6010 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6011 .addReg(NewVReg1)
6012 .addImm(NumLPads));
6013 } else {
6014 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling922ad782011-10-19 09:24:02 +00006015 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6016 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6017
6018 // MachineConstantPool wants an explicit alignment.
6019 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6020 if (Align == 0)
6021 Align = getTargetData()->getTypeAllocSize(C->getType());
6022 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendlinga5871dc2011-10-18 23:11:05 +00006023
6024 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6025 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6026 .addReg(VReg1, RegState::Define)
6027 .addConstantPoolIndex(Idx));
6028 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6029 .addReg(NewVReg1)
6030 .addReg(VReg1));
6031 }
6032
Bill Wendling083a8eb2011-10-06 23:37:36 +00006033 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6034 .addMBB(TrapBB)
6035 .addImm(ARMCC::HI)
6036 .addReg(ARM::CPSR);
6037
6038 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6039 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6040 .addReg(ARM::CPSR, RegState::Define)
6041 .addReg(NewVReg1)
6042 .addImm(2));
6043
6044 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling217f0e92011-10-06 23:41:14 +00006045 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendling083a8eb2011-10-06 23:37:36 +00006046 .addJumpTableIndex(MJTI)
6047 .addImm(UId));
6048
6049 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6050 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6051 .addReg(ARM::CPSR, RegState::Define)
6052 .addReg(NewVReg2, RegState::Kill)
6053 .addReg(NewVReg3));
6054
6055 MachineMemOperand *JTMMOLd =
6056 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6057 MachineMemOperand::MOLoad, 4, 4);
6058
6059 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6060 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6061 .addReg(NewVReg4, RegState::Kill)
6062 .addImm(0)
6063 .addMemOperand(JTMMOLd));
6064
6065 unsigned NewVReg6 = MRI->createVirtualRegister(TRC);
6066 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6067 .addReg(ARM::CPSR, RegState::Define)
6068 .addReg(NewVReg5, RegState::Kill)
6069 .addReg(NewVReg3));
6070
6071 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6072 .addReg(NewVReg6, RegState::Kill)
6073 .addJumpTableIndex(MJTI)
6074 .addImm(UId);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006075 } else {
6076 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6077 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6078 .addFrameIndex(FI)
6079 .addImm(4)
6080 .addMemOperand(FIMMOLd));
Bill Wendling564392b2011-10-18 22:11:18 +00006081
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006082 if (NumLPads < 256) {
6083 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6084 .addReg(NewVReg1)
6085 .addImm(NumLPads));
Bill Wendling922ad782011-10-19 09:24:02 +00006086 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006087 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6088 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling15a1a222011-10-18 23:19:55 +00006089 .addImm(NumLPads & 0xFFFF));
6090
6091 unsigned VReg2 = VReg1;
6092 if ((NumLPads & 0xFFFF0000) != 0) {
6093 VReg2 = MRI->createVirtualRegister(TRC);
6094 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6095 .addReg(VReg1)
6096 .addImm(NumLPads >> 16));
6097 }
6098
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006099 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6100 .addReg(NewVReg1)
6101 .addReg(VReg2));
Bill Wendling922ad782011-10-19 09:24:02 +00006102 } else {
6103 MachineConstantPool *ConstantPool = MF->getConstantPool();
6104 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6105 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6106
6107 // MachineConstantPool wants an explicit alignment.
6108 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6109 if (Align == 0)
6110 Align = getTargetData()->getTypeAllocSize(C->getType());
6111 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6112
6113 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6114 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6115 .addReg(VReg1, RegState::Define)
Bill Wendling767f8be2011-10-20 20:37:11 +00006116 .addConstantPoolIndex(Idx)
6117 .addImm(0));
Bill Wendling922ad782011-10-19 09:24:02 +00006118 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6119 .addReg(NewVReg1)
6120 .addReg(VReg1, RegState::Kill));
Bill Wendling85f3a0a2011-10-18 22:52:20 +00006121 }
6122
Bill Wendling95ce2e92011-10-06 22:53:00 +00006123 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6124 .addMBB(TrapBB)
6125 .addImm(ARMCC::HI)
6126 .addReg(ARM::CPSR);
Bill Wendling2a850152011-10-05 00:02:33 +00006127
Bill Wendling564392b2011-10-18 22:11:18 +00006128 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006129 AddDefaultCC(
Bill Wendling564392b2011-10-18 22:11:18 +00006130 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006131 .addReg(NewVReg1)
6132 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling564392b2011-10-18 22:11:18 +00006133 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6134 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006135 .addJumpTableIndex(MJTI)
6136 .addImm(UId));
6137
6138 MachineMemOperand *JTMMOLd =
6139 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6140 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling564392b2011-10-18 22:11:18 +00006141 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling95ce2e92011-10-06 22:53:00 +00006142 AddDefaultPred(
Bill Wendling564392b2011-10-18 22:11:18 +00006143 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6144 .addReg(NewVReg3, RegState::Kill)
6145 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006146 .addImm(0)
6147 .addMemOperand(JTMMOLd));
6148
6149 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
Bill Wendling564392b2011-10-18 22:11:18 +00006150 .addReg(NewVReg5, RegState::Kill)
6151 .addReg(NewVReg4)
Bill Wendling95ce2e92011-10-06 22:53:00 +00006152 .addJumpTableIndex(MJTI)
6153 .addImm(UId);
6154 }
Bill Wendling2a850152011-10-05 00:02:33 +00006155
Bill Wendlingbb734682011-10-05 00:39:32 +00006156 // Add the jump table entries as successors to the MBB.
Bill Wendling2acf6382011-10-07 23:18:02 +00006157 MachineBasicBlock *PrevMBB = 0;
Bill Wendlingbb734682011-10-05 00:39:32 +00006158 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling2acf6382011-10-07 23:18:02 +00006159 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6160 MachineBasicBlock *CurMBB = *I;
6161 if (PrevMBB != CurMBB)
6162 DispContBB->addSuccessor(CurMBB);
6163 PrevMBB = CurMBB;
6164 }
6165
Bill Wendling24bb9252011-10-17 05:25:09 +00006166 // N.B. the order the invoke BBs are processed in doesn't matter here.
Bill Wendling969c9ef2011-10-14 23:34:37 +00006167 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6168 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
Craig Topper015f2282012-03-04 03:33:22 +00006169 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006170 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling2acf6382011-10-07 23:18:02 +00006171 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6172 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6173 MachineBasicBlock *BB = *I;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006174
6175 // Remove the landing pad successor from the invoke block and replace it
6176 // with the new dispatch block.
Bill Wendlingde39d862011-10-26 07:16:18 +00006177 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6178 BB->succ_end());
6179 while (!Successors.empty()) {
6180 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling2acf6382011-10-07 23:18:02 +00006181 if (SMBB->isLandingPad()) {
6182 BB->removeSuccessor(SMBB);
Bill Wendlingf7b02072011-10-18 18:30:49 +00006183 MBBLPads.push_back(SMBB);
Bill Wendling2acf6382011-10-07 23:18:02 +00006184 }
6185 }
6186
6187 BB->addSuccessor(DispatchBB);
Bill Wendling969c9ef2011-10-14 23:34:37 +00006188
6189 // Find the invoke call and mark all of the callee-saved registers as
6190 // 'implicit defined' so that they're spilled. This prevents code from
6191 // moving instructions to before the EH block, where they will never be
6192 // executed.
6193 for (MachineBasicBlock::reverse_iterator
6194 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006195 if (!II->isCall()) continue;
Bill Wendling969c9ef2011-10-14 23:34:37 +00006196
6197 DenseMap<unsigned, bool> DefRegs;
6198 for (MachineInstr::mop_iterator
6199 OI = II->operands_begin(), OE = II->operands_end();
6200 OI != OE; ++OI) {
6201 if (!OI->isReg()) continue;
6202 DefRegs[OI->getReg()] = true;
6203 }
6204
6205 MachineInstrBuilder MIB(&*II);
6206
Bill Wendling5d798592011-10-14 23:55:44 +00006207 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006208 unsigned Reg = SavedRegs[i];
6209 if (Subtarget->isThumb2() &&
Craig Topper420761a2012-04-20 07:30:17 +00006210 !ARM::tGPRRegClass.contains(Reg) &&
6211 !ARM::hGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006212 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006213 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006214 continue;
Craig Topper420761a2012-04-20 07:30:17 +00006215 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendlingb8dcb312011-10-22 00:29:28 +00006216 continue;
6217 if (!DefRegs[Reg])
6218 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling5d798592011-10-14 23:55:44 +00006219 }
Bill Wendling969c9ef2011-10-14 23:34:37 +00006220
6221 break;
6222 }
Bill Wendling2acf6382011-10-07 23:18:02 +00006223 }
Bill Wendlingbb734682011-10-05 00:39:32 +00006224
Bill Wendlingf7b02072011-10-18 18:30:49 +00006225 // Mark all former landing pads as non-landing pads. The dispatch is the only
6226 // landing pad now.
6227 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6228 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6229 (*I)->setIsLandingPad(false);
6230
Bill Wendlingbb734682011-10-05 00:39:32 +00006231 // The instruction is gone now.
6232 MI->eraseFromParent();
6233
Bill Wendlingf7e4aef2011-10-03 21:25:38 +00006234 return MBB;
6235}
6236
Evan Cheng218977b2010-07-13 19:27:42 +00006237static
6238MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6239 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6240 E = MBB->succ_end(); I != E; ++I)
6241 if (*I != Succ)
6242 return *I;
6243 llvm_unreachable("Expecting a BB with two successors!");
6244}
6245
Manman Ren68f25572012-06-01 19:33:18 +00006246MachineBasicBlock *ARMTargetLowering::
6247EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
6248 // This pseudo instruction has 3 operands: dst, src, size
6249 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6250 // Otherwise, we will generate unrolled scalar copies.
6251 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6252 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6253 MachineFunction::iterator It = BB;
6254 ++It;
6255
6256 unsigned dest = MI->getOperand(0).getReg();
6257 unsigned src = MI->getOperand(1).getReg();
6258 unsigned SizeVal = MI->getOperand(2).getImm();
6259 unsigned Align = MI->getOperand(3).getImm();
6260 DebugLoc dl = MI->getDebugLoc();
6261
6262 bool isThumb2 = Subtarget->isThumb2();
6263 MachineFunction *MF = BB->getParent();
6264 MachineRegisterInfo &MRI = MF->getRegInfo();
Manman Reneda9fdf2012-06-18 22:23:48 +00006265 unsigned ldrOpc, strOpc, UnitSize = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006266
6267 const TargetRegisterClass *TRC = isThumb2 ?
6268 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6269 (const TargetRegisterClass*)&ARM::GPRRegClass;
Manman Reneda9fdf2012-06-18 22:23:48 +00006270 const TargetRegisterClass *TRC_Vec = 0;
Manman Ren68f25572012-06-01 19:33:18 +00006271
6272 if (Align & 1) {
6273 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6274 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6275 UnitSize = 1;
6276 } else if (Align & 2) {
6277 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
6278 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
6279 UnitSize = 2;
6280 } else {
Manman Reneda9fdf2012-06-18 22:23:48 +00006281 // Check whether we can use NEON instructions.
6282 if (!MF->getFunction()->hasFnAttr(Attribute::NoImplicitFloat) &&
6283 Subtarget->hasNEON()) {
6284 if ((Align % 16 == 0) && SizeVal >= 16) {
6285 ldrOpc = ARM::VLD1q32wb_fixed;
6286 strOpc = ARM::VST1q32wb_fixed;
6287 UnitSize = 16;
6288 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
6289 }
6290 else if ((Align % 8 == 0) && SizeVal >= 8) {
6291 ldrOpc = ARM::VLD1d32wb_fixed;
6292 strOpc = ARM::VST1d32wb_fixed;
6293 UnitSize = 8;
6294 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
6295 }
6296 }
6297 // Can't use NEON instructions.
6298 if (UnitSize == 0) {
6299 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6300 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
6301 UnitSize = 4;
6302 }
Manman Ren68f25572012-06-01 19:33:18 +00006303 }
Manman Reneda9fdf2012-06-18 22:23:48 +00006304
Manman Ren68f25572012-06-01 19:33:18 +00006305 unsigned BytesLeft = SizeVal % UnitSize;
6306 unsigned LoopSize = SizeVal - BytesLeft;
6307
6308 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6309 // Use LDR and STR to copy.
6310 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6311 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6312 unsigned srcIn = src;
6313 unsigned destIn = dest;
6314 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
Manman Reneda9fdf2012-06-18 22:23:48 +00006315 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
Manman Ren68f25572012-06-01 19:33:18 +00006316 unsigned srcOut = MRI.createVirtualRegister(TRC);
6317 unsigned destOut = MRI.createVirtualRegister(TRC);
Manman Reneda9fdf2012-06-18 22:23:48 +00006318 if (UnitSize >= 8) {
6319 AddDefaultPred(BuildMI(*BB, MI, dl,
6320 TII->get(ldrOpc), scratch)
6321 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
6322
6323 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6324 .addReg(destIn).addImm(0).addReg(scratch));
6325 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006326 AddDefaultPred(BuildMI(*BB, MI, dl,
6327 TII->get(ldrOpc), scratch)
6328 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
6329
6330 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6331 .addReg(scratch).addReg(destIn)
6332 .addImm(UnitSize));
6333 } else {
6334 AddDefaultPred(BuildMI(*BB, MI, dl,
6335 TII->get(ldrOpc), scratch)
6336 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
6337 .addImm(UnitSize));
6338
6339 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6340 .addReg(scratch).addReg(destIn)
6341 .addReg(0).addImm(UnitSize));
6342 }
6343 srcIn = srcOut;
6344 destIn = destOut;
6345 }
6346
6347 // Handle the leftover bytes with LDRB and STRB.
6348 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6349 // [destOut] = STRB_POST(scratch, destIn, 1)
6350 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6351 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6352 for (unsigned i = 0; i < BytesLeft; i++) {
6353 unsigned scratch = MRI.createVirtualRegister(TRC);
6354 unsigned srcOut = MRI.createVirtualRegister(TRC);
6355 unsigned destOut = MRI.createVirtualRegister(TRC);
6356 if (isThumb2) {
6357 AddDefaultPred(BuildMI(*BB, MI, dl,
6358 TII->get(ldrOpc),scratch)
6359 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6360
6361 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6362 .addReg(scratch).addReg(destIn)
6363 .addReg(0).addImm(1));
6364 } else {
6365 AddDefaultPred(BuildMI(*BB, MI, dl,
6366 TII->get(ldrOpc),scratch)
6367 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6368
6369 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
6370 .addReg(scratch).addReg(destIn)
6371 .addReg(0).addImm(1));
6372 }
6373 srcIn = srcOut;
6374 destIn = destOut;
6375 }
6376 MI->eraseFromParent(); // The instruction is gone now.
6377 return BB;
6378 }
6379
6380 // Expand the pseudo op to a loop.
6381 // thisMBB:
6382 // ...
6383 // movw varEnd, # --> with thumb2
6384 // movt varEnd, #
6385 // ldrcp varEnd, idx --> without thumb2
6386 // fallthrough --> loopMBB
6387 // loopMBB:
6388 // PHI varPhi, varEnd, varLoop
6389 // PHI srcPhi, src, srcLoop
6390 // PHI destPhi, dst, destLoop
6391 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6392 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6393 // subs varLoop, varPhi, #UnitSize
6394 // bne loopMBB
6395 // fallthrough --> exitMBB
6396 // exitMBB:
6397 // epilogue to handle left-over bytes
6398 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6399 // [destOut] = STRB_POST(scratch, destLoop, 1)
6400 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6401 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6402 MF->insert(It, loopMBB);
6403 MF->insert(It, exitMBB);
6404
6405 // Transfer the remainder of BB and its successor edges to exitMBB.
6406 exitMBB->splice(exitMBB->begin(), BB,
6407 llvm::next(MachineBasicBlock::iterator(MI)),
6408 BB->end());
6409 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6410
6411 // Load an immediate to varEnd.
6412 unsigned varEnd = MRI.createVirtualRegister(TRC);
6413 if (isThumb2) {
6414 unsigned VReg1 = varEnd;
6415 if ((LoopSize & 0xFFFF0000) != 0)
6416 VReg1 = MRI.createVirtualRegister(TRC);
6417 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
6418 .addImm(LoopSize & 0xFFFF));
6419
6420 if ((LoopSize & 0xFFFF0000) != 0)
6421 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
6422 .addReg(VReg1)
6423 .addImm(LoopSize >> 16));
6424 } else {
6425 MachineConstantPool *ConstantPool = MF->getConstantPool();
6426 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6427 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
6428
6429 // MachineConstantPool wants an explicit alignment.
6430 unsigned Align = getTargetData()->getPrefTypeAlignment(Int32Ty);
6431 if (Align == 0)
6432 Align = getTargetData()->getTypeAllocSize(C->getType());
6433 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6434
6435 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
6436 .addReg(varEnd, RegState::Define)
6437 .addConstantPoolIndex(Idx)
6438 .addImm(0));
6439 }
6440 BB->addSuccessor(loopMBB);
6441
6442 // Generate the loop body:
6443 // varPhi = PHI(varLoop, varEnd)
6444 // srcPhi = PHI(srcLoop, src)
6445 // destPhi = PHI(destLoop, dst)
6446 MachineBasicBlock *entryBB = BB;
6447 BB = loopMBB;
6448 unsigned varLoop = MRI.createVirtualRegister(TRC);
6449 unsigned varPhi = MRI.createVirtualRegister(TRC);
6450 unsigned srcLoop = MRI.createVirtualRegister(TRC);
6451 unsigned srcPhi = MRI.createVirtualRegister(TRC);
6452 unsigned destLoop = MRI.createVirtualRegister(TRC);
6453 unsigned destPhi = MRI.createVirtualRegister(TRC);
6454
6455 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
6456 .addReg(varLoop).addMBB(loopMBB)
6457 .addReg(varEnd).addMBB(entryBB);
6458 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
6459 .addReg(srcLoop).addMBB(loopMBB)
6460 .addReg(src).addMBB(entryBB);
6461 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
6462 .addReg(destLoop).addMBB(loopMBB)
6463 .addReg(dest).addMBB(entryBB);
6464
6465 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6466 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
Manman Reneda9fdf2012-06-18 22:23:48 +00006467 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
6468 if (UnitSize >= 8) {
6469 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6470 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
6471
6472 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6473 .addReg(destPhi).addImm(0).addReg(scratch));
6474 } else if (isThumb2) {
Manman Ren68f25572012-06-01 19:33:18 +00006475 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6476 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
6477
6478 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6479 .addReg(scratch).addReg(destPhi)
6480 .addImm(UnitSize));
6481 } else {
6482 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
6483 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
6484 .addImm(UnitSize));
6485
6486 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
6487 .addReg(scratch).addReg(destPhi)
6488 .addReg(0).addImm(UnitSize));
6489 }
6490
6491 // Decrement loop variable by UnitSize.
6492 MachineInstrBuilder MIB = BuildMI(BB, dl,
6493 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
6494 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
6495 MIB->getOperand(5).setReg(ARM::CPSR);
6496 MIB->getOperand(5).setIsDef(true);
6497
6498 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6499 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6500
6501 // loopMBB can loop back to loopMBB or fall through to exitMBB.
6502 BB->addSuccessor(loopMBB);
6503 BB->addSuccessor(exitMBB);
6504
6505 // Add epilogue to handle BytesLeft.
6506 BB = exitMBB;
6507 MachineInstr *StartOfExit = exitMBB->begin();
6508 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
6509 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
6510
6511 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6512 // [destOut] = STRB_POST(scratch, destLoop, 1)
6513 unsigned srcIn = srcLoop;
6514 unsigned destIn = destLoop;
6515 for (unsigned i = 0; i < BytesLeft; i++) {
6516 unsigned scratch = MRI.createVirtualRegister(TRC);
6517 unsigned srcOut = MRI.createVirtualRegister(TRC);
6518 unsigned destOut = MRI.createVirtualRegister(TRC);
6519 if (isThumb2) {
6520 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6521 TII->get(ldrOpc),scratch)
6522 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
6523
6524 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6525 .addReg(scratch).addReg(destIn)
6526 .addImm(1));
6527 } else {
6528 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
6529 TII->get(ldrOpc),scratch)
6530 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
6531
6532 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
6533 .addReg(scratch).addReg(destIn)
6534 .addReg(0).addImm(1));
6535 }
6536 srcIn = srcOut;
6537 destIn = destOut;
6538 }
6539
6540 MI->eraseFromParent(); // The instruction is gone now.
6541 return BB;
6542}
6543
Jim Grosbache801dc42009-12-12 01:40:06 +00006544MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006545ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006546 MachineBasicBlock *BB) const {
Evan Chenga8e29892007-01-19 07:51:42 +00006547 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesenb6728402009-02-13 02:25:56 +00006548 DebugLoc dl = MI->getDebugLoc();
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006549 bool isThumb2 = Subtarget->isThumb2();
Evan Chenga8e29892007-01-19 07:51:42 +00006550 switch (MI->getOpcode()) {
Andrew Trick1c3af772011-04-23 03:55:32 +00006551 default: {
Jim Grosbach5278eb82009-12-11 01:42:04 +00006552 MI->dump();
Evan Cheng86198642009-08-07 00:34:42 +00006553 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick1c3af772011-04-23 03:55:32 +00006554 }
Jim Grosbachee2c2a42011-09-16 21:55:56 +00006555 // The Thumb2 pre-indexed stores have the same MI operands, they just
6556 // define them differently in the .td files from the isel patterns, so
6557 // they need pseudos.
6558 case ARM::t2STR_preidx:
6559 MI->setDesc(TII->get(ARM::t2STR_PRE));
6560 return BB;
6561 case ARM::t2STRB_preidx:
6562 MI->setDesc(TII->get(ARM::t2STRB_PRE));
6563 return BB;
6564 case ARM::t2STRH_preidx:
6565 MI->setDesc(TII->get(ARM::t2STRH_PRE));
6566 return BB;
6567
Jim Grosbach19dec202011-08-05 20:35:44 +00006568 case ARM::STRi_preidx:
6569 case ARM::STRBi_preidx: {
Jim Grosbach6cd57162011-08-09 21:22:41 +00006570 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbach19dec202011-08-05 20:35:44 +00006571 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
6572 // Decode the offset.
6573 unsigned Offset = MI->getOperand(4).getImm();
6574 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
6575 Offset = ARM_AM::getAM2Offset(Offset);
6576 if (isSub)
6577 Offset = -Offset;
6578
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006579 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer2753ae32011-08-27 17:36:14 +00006580 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbach19dec202011-08-05 20:35:44 +00006581 .addOperand(MI->getOperand(0)) // Rn_wb
6582 .addOperand(MI->getOperand(1)) // Rt
6583 .addOperand(MI->getOperand(2)) // Rn
6584 .addImm(Offset) // offset (skip GPR==zero_reg)
6585 .addOperand(MI->getOperand(5)) // pred
Jim Grosbach4dfe2202011-08-12 21:02:34 +00006586 .addOperand(MI->getOperand(6))
6587 .addMemOperand(MMO);
Jim Grosbach19dec202011-08-05 20:35:44 +00006588 MI->eraseFromParent();
6589 return BB;
6590 }
6591 case ARM::STRr_preidx:
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00006592 case ARM::STRBr_preidx:
6593 case ARM::STRH_preidx: {
6594 unsigned NewOpc;
6595 switch (MI->getOpcode()) {
6596 default: llvm_unreachable("unexpected opcode!");
6597 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
6598 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
6599 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
6600 }
Jim Grosbach19dec202011-08-05 20:35:44 +00006601 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
6602 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
6603 MIB.addOperand(MI->getOperand(i));
6604 MI->eraseFromParent();
6605 return BB;
6606 }
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006607 case ARM::ATOMIC_LOAD_ADD_I8:
6608 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6609 case ARM::ATOMIC_LOAD_ADD_I16:
6610 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
6611 case ARM::ATOMIC_LOAD_ADD_I32:
6612 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006613
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006614 case ARM::ATOMIC_LOAD_AND_I8:
6615 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6616 case ARM::ATOMIC_LOAD_AND_I16:
6617 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
6618 case ARM::ATOMIC_LOAD_AND_I32:
6619 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006620
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006621 case ARM::ATOMIC_LOAD_OR_I8:
6622 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6623 case ARM::ATOMIC_LOAD_OR_I16:
6624 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
6625 case ARM::ATOMIC_LOAD_OR_I32:
6626 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006627
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006628 case ARM::ATOMIC_LOAD_XOR_I8:
6629 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6630 case ARM::ATOMIC_LOAD_XOR_I16:
6631 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
6632 case ARM::ATOMIC_LOAD_XOR_I32:
6633 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006634
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006635 case ARM::ATOMIC_LOAD_NAND_I8:
6636 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6637 case ARM::ATOMIC_LOAD_NAND_I16:
6638 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
6639 case ARM::ATOMIC_LOAD_NAND_I32:
6640 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006641
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006642 case ARM::ATOMIC_LOAD_SUB_I8:
6643 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6644 case ARM::ATOMIC_LOAD_SUB_I16:
6645 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
6646 case ARM::ATOMIC_LOAD_SUB_I32:
6647 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
Jim Grosbache801dc42009-12-12 01:40:06 +00006648
Jim Grosbachf7da8822011-04-26 19:44:18 +00006649 case ARM::ATOMIC_LOAD_MIN_I8:
6650 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
6651 case ARM::ATOMIC_LOAD_MIN_I16:
6652 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
6653 case ARM::ATOMIC_LOAD_MIN_I32:
6654 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
6655
6656 case ARM::ATOMIC_LOAD_MAX_I8:
6657 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
6658 case ARM::ATOMIC_LOAD_MAX_I16:
6659 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
6660 case ARM::ATOMIC_LOAD_MAX_I32:
6661 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
6662
6663 case ARM::ATOMIC_LOAD_UMIN_I8:
6664 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
6665 case ARM::ATOMIC_LOAD_UMIN_I16:
6666 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
6667 case ARM::ATOMIC_LOAD_UMIN_I32:
6668 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
6669
6670 case ARM::ATOMIC_LOAD_UMAX_I8:
6671 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
6672 case ARM::ATOMIC_LOAD_UMAX_I16:
6673 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
6674 case ARM::ATOMIC_LOAD_UMAX_I32:
6675 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
6676
Jim Grosbacha36c8f22009-12-14 20:14:59 +00006677 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
6678 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
6679 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
Jim Grosbache801dc42009-12-12 01:40:06 +00006680
6681 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
6682 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
6683 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
Jim Grosbach5278eb82009-12-11 01:42:04 +00006684
Eli Friedman2bdffe42011-08-31 00:31:29 +00006685
6686 case ARM::ATOMADD6432:
6687 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006688 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
6689 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006690 case ARM::ATOMSUB6432:
6691 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006692 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6693 /*NeedsCarry*/ true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006694 case ARM::ATOMOR6432:
6695 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006696 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006697 case ARM::ATOMXOR6432:
6698 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006699 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006700 case ARM::ATOMAND6432:
6701 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
Eli Friedman4d3f3292011-08-31 17:52:22 +00006702 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006703 case ARM::ATOMSWAP6432:
6704 return EmitAtomicBinary64(MI, BB, 0, 0, false);
Eli Friedman4d3f3292011-08-31 17:52:22 +00006705 case ARM::ATOMCMPXCHG6432:
6706 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
6707 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
6708 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
Eli Friedman2bdffe42011-08-31 00:31:29 +00006709
Evan Cheng007ea272009-08-12 05:17:19 +00006710 case ARM::tMOVCCr_pseudo: {
Evan Chenga8e29892007-01-19 07:51:42 +00006711 // To "insert" a SELECT_CC instruction, we actually have to insert the
6712 // diamond control-flow pattern. The incoming instruction knows the
6713 // destination vreg to set, the condition code register to branch on, the
6714 // true/false values to select between, and a branch opcode to use.
6715 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006716 MachineFunction::iterator It = BB;
Evan Chenga8e29892007-01-19 07:51:42 +00006717 ++It;
6718
6719 // thisMBB:
6720 // ...
6721 // TrueVal = ...
6722 // cmpTY ccX, r1, r2
6723 // bCC copy1MBB
6724 // fallthrough --> copy0MBB
6725 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006726 MachineFunction *F = BB->getParent();
6727 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6728 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman258c58c2010-07-06 15:49:48 +00006729 F->insert(It, copy0MBB);
6730 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006731
6732 // Transfer the remainder of BB and its successor edges to sinkMBB.
6733 sinkMBB->splice(sinkMBB->begin(), BB,
6734 llvm::next(MachineBasicBlock::iterator(MI)),
6735 BB->end());
6736 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6737
Dan Gohman258c58c2010-07-06 15:49:48 +00006738 BB->addSuccessor(copy0MBB);
6739 BB->addSuccessor(sinkMBB);
Dan Gohmanb81c7712010-07-06 15:18:19 +00006740
Dan Gohman14152b42010-07-06 20:24:04 +00006741 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
6742 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
6743
Evan Chenga8e29892007-01-19 07:51:42 +00006744 // copy0MBB:
6745 // %FalseValue = ...
6746 // # fallthrough to sinkMBB
6747 BB = copy0MBB;
6748
6749 // Update machine-CFG edges
6750 BB->addSuccessor(sinkMBB);
6751
6752 // sinkMBB:
6753 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6754 // ...
6755 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006756 BuildMI(*BB, BB->begin(), dl,
6757 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Chenga8e29892007-01-19 07:51:42 +00006758 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6759 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6760
Dan Gohman14152b42010-07-06 20:24:04 +00006761 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Chenga8e29892007-01-19 07:51:42 +00006762 return BB;
6763 }
Evan Cheng86198642009-08-07 00:34:42 +00006764
Evan Cheng218977b2010-07-13 19:27:42 +00006765 case ARM::BCCi64:
6766 case ARM::BCCZi64: {
Bob Wilson3c904692010-12-23 22:45:49 +00006767 // If there is an unconditional branch to the other successor, remove it.
6768 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick7fa75ce2011-01-19 02:26:13 +00006769
Evan Cheng218977b2010-07-13 19:27:42 +00006770 // Compare both parts that make up the double comparison separately for
6771 // equality.
6772 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
6773
6774 unsigned LHS1 = MI->getOperand(1).getReg();
6775 unsigned LHS2 = MI->getOperand(2).getReg();
6776 if (RHSisZero) {
6777 AddDefaultPred(BuildMI(BB, dl,
6778 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6779 .addReg(LHS1).addImm(0));
6780 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6781 .addReg(LHS2).addImm(0)
6782 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6783 } else {
6784 unsigned RHS1 = MI->getOperand(3).getReg();
6785 unsigned RHS2 = MI->getOperand(4).getReg();
6786 AddDefaultPred(BuildMI(BB, dl,
6787 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6788 .addReg(LHS1).addReg(RHS1));
6789 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6790 .addReg(LHS2).addReg(RHS2)
6791 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
6792 }
6793
6794 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
6795 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
6796 if (MI->getOperand(0).getImm() == ARMCC::NE)
6797 std::swap(destMBB, exitMBB);
6798
6799 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6800 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson51f6a7a2011-09-09 21:48:23 +00006801 if (isThumb2)
6802 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
6803 else
6804 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng218977b2010-07-13 19:27:42 +00006805
6806 MI->eraseFromParent(); // The pseudo instruction is gone now.
6807 return BB;
6808 }
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006809
Bill Wendling5bc85282011-10-17 20:37:20 +00006810 case ARM::Int_eh_sjlj_setjmp:
6811 case ARM::Int_eh_sjlj_setjmp_nofp:
6812 case ARM::tInt_eh_sjlj_setjmp:
6813 case ARM::t2Int_eh_sjlj_setjmp:
6814 case ARM::t2Int_eh_sjlj_setjmp_nofp:
6815 EmitSjLjDispatchBlock(MI, BB);
6816 return BB;
6817
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006818 case ARM::ABS:
6819 case ARM::t2ABS: {
6820 // To insert an ABS instruction, we have to insert the
6821 // diamond control-flow pattern. The incoming instruction knows the
6822 // source vreg to test against 0, the destination vreg to set,
6823 // the condition code register to branch on, the
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006824 // true/false values to select between, and a branch opcode to use.
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006825 // It transforms
6826 // V1 = ABS V0
6827 // into
6828 // V2 = MOVS V0
6829 // BCC (branch to SinkBB if V0 >= 0)
6830 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006831 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006832 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6833 MachineFunction::iterator BBI = BB;
6834 ++BBI;
6835 MachineFunction *Fn = BB->getParent();
6836 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6837 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
6838 Fn->insert(BBI, RSBBB);
6839 Fn->insert(BBI, SinkBB);
6840
6841 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
6842 unsigned int ABSDstReg = MI->getOperand(0).getReg();
6843 bool isThumb2 = Subtarget->isThumb2();
6844 MachineRegisterInfo &MRI = Fn->getRegInfo();
6845 // In Thumb mode S must not be specified if source register is the SP or
6846 // PC and if destination register is the SP, so restrict register class
Craig Topper420761a2012-04-20 07:30:17 +00006847 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
6848 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6849 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006850
6851 // Transfer the remainder of BB and its successor edges to sinkMBB.
6852 SinkBB->splice(SinkBB->begin(), BB,
6853 llvm::next(MachineBasicBlock::iterator(MI)),
6854 BB->end());
6855 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
6856
6857 BB->addSuccessor(RSBBB);
6858 BB->addSuccessor(SinkBB);
6859
6860 // fall through to SinkMBB
6861 RSBBB->addSuccessor(SinkBB);
6862
Manman Ren307473d2012-06-15 21:32:12 +00006863 // insert a cmp at the end of BB
Andrew Trick49b446f2012-07-18 18:34:24 +00006864 AddDefaultPred(BuildMI(BB, dl,
Manman Ren307473d2012-06-15 21:32:12 +00006865 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6866 .addReg(ABSSrcReg).addImm(0));
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006867
6868 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006869 BuildMI(BB, dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006870 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
6871 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
6872
6873 // insert rsbri in RSBBB
6874 // Note: BCC and rsbri will be converted into predicated rsbmi
6875 // by if-conversion pass
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006876 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006877 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Ren307473d2012-06-15 21:32:12 +00006878 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006879 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
6880
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006881 // insert PHI in SinkBB,
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006882 // reuse ABSDstReg to not change uses of ABS instruction
6883 BuildMI(*SinkBB, SinkBB->begin(), dl,
6884 TII->get(ARM::PHI), ABSDstReg)
6885 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Ren307473d2012-06-15 21:32:12 +00006886 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006887
6888 // remove ABS instruction
Andrew Trick7f5f0da2011-10-18 18:40:53 +00006889 MI->eraseFromParent();
Bill Wendlingef2c86f2011-10-10 22:59:55 +00006890
6891 // return last added BB
6892 return SinkBB;
6893 }
Manman Ren68f25572012-06-01 19:33:18 +00006894 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren763a75d2012-06-01 02:44:42 +00006895 ++NumLoopByVals;
Manman Ren68f25572012-06-01 19:33:18 +00006896 return EmitStructByval(MI, BB);
Evan Chenga8e29892007-01-19 07:51:42 +00006897 }
6898}
6899
Evan Cheng37fefc22011-08-30 19:09:48 +00006900void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
6901 SDNode *Node) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006902 if (!MI->hasPostISelHook()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006903 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
6904 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
6905 return;
6906 }
6907
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006908 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick4815d562011-09-20 03:17:40 +00006909 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
6910 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
6911 // operand is still set to noreg. If needed, set the optional operand's
6912 // register to CPSR, and remove the redundant implicit def.
Andrew Trick3be654f2011-09-21 02:20:46 +00006913 //
Andrew Trick90b7b122011-10-18 19:18:52 +00006914 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick4815d562011-09-20 03:17:40 +00006915
Andrew Trick3be654f2011-09-21 02:20:46 +00006916 // Rename pseudo opcodes.
6917 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
6918 if (NewOpc) {
6919 const ARMBaseInstrInfo *TII =
6920 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick90b7b122011-10-18 19:18:52 +00006921 MCID = &TII->get(NewOpc);
6922
6923 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
6924 "converted opcode should be the same except for cc_out");
6925
6926 MI->setDesc(*MCID);
6927
6928 // Add the optional cc_out operand
6929 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick3be654f2011-09-21 02:20:46 +00006930 }
Andrew Trick90b7b122011-10-18 19:18:52 +00006931 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick4815d562011-09-20 03:17:40 +00006932
6933 // Any ARM instruction that sets the 's' bit should specify an optional
6934 // "cc_out" operand in the last operand position.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00006935 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006936 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006937 return;
6938 }
Andrew Trick3be654f2011-09-21 02:20:46 +00006939 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
6940 // since we already have an optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006941 bool definesCPSR = false;
6942 bool deadCPSR = false;
Andrew Trick90b7b122011-10-18 19:18:52 +00006943 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick4815d562011-09-20 03:17:40 +00006944 i != e; ++i) {
6945 const MachineOperand &MO = MI->getOperand(i);
6946 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
6947 definesCPSR = true;
6948 if (MO.isDead())
6949 deadCPSR = true;
6950 MI->RemoveOperand(i);
6951 break;
Evan Cheng37fefc22011-08-30 19:09:48 +00006952 }
6953 }
Andrew Trick4815d562011-09-20 03:17:40 +00006954 if (!definesCPSR) {
Andrew Trick3be654f2011-09-21 02:20:46 +00006955 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick4815d562011-09-20 03:17:40 +00006956 return;
6957 }
6958 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick3be654f2011-09-21 02:20:46 +00006959 if (deadCPSR) {
6960 assert(!MI->getOperand(ccOutIdx).getReg() &&
6961 "expect uninitialized optional cc_out operand");
Andrew Trick4815d562011-09-20 03:17:40 +00006962 return;
Andrew Trick3be654f2011-09-21 02:20:46 +00006963 }
Andrew Trick4815d562011-09-20 03:17:40 +00006964
Andrew Trick3be654f2011-09-21 02:20:46 +00006965 // If this instruction was defined with an optional CPSR def and its dag node
6966 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick4815d562011-09-20 03:17:40 +00006967 MachineOperand &MO = MI->getOperand(ccOutIdx);
6968 MO.setReg(ARM::CPSR);
6969 MO.setIsDef(true);
Evan Cheng37fefc22011-08-30 19:09:48 +00006970}
6971
Evan Chenga8e29892007-01-19 07:51:42 +00006972//===----------------------------------------------------------------------===//
6973// ARM Optimization Hooks
6974//===----------------------------------------------------------------------===//
6975
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00006976// Helper function that checks if N is a null or all ones constant.
6977static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
6978 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
6979 if (!C)
6980 return false;
6981 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
6982}
6983
6984// Combine a constant select operand into its use:
6985//
6986// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
6987// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
6988//
6989// The transform is rejected if the select doesn't have a constant operand that
6990// is null.
6991//
6992// @param N The node to transform.
6993// @param Slct The N operand that is a select.
6994// @param OtherOp The other N operand (x above).
6995// @param DCI Context.
6996// @returns The new node, or SDValue() on failure.
Chris Lattnerd1980a52009-03-12 06:52:53 +00006997static
6998SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
6999 TargetLowering::DAGCombinerInfo &DCI) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007000 SelectionDAG &DAG = DCI.DAG;
7001 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00007002 EVT VT = N->getValueType(0);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007003 unsigned Opc = N->getOpcode();
7004 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
7005 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
7006 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
7007 ISD::CondCode CC = ISD::SETCC_INVALID;
7008
7009 if (isSlctCC) {
7010 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
7011 } else {
7012 SDValue CCOp = Slct.getOperand(0);
7013 if (CCOp.getOpcode() == ISD::SETCC)
7014 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
7015 }
7016
7017 bool DoXform = false;
7018 bool InvCC = false;
7019 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
7020 "Bad input!");
7021
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007022 if (isZeroOrAllOnes(LHS, false)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007023 DoXform = true;
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007024 } else if (CC != ISD::SETCC_INVALID && isZeroOrAllOnes(RHS, false)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007025 std::swap(LHS, RHS);
7026 SDValue Op0 = Slct.getOperand(0);
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007027 EVT OpVT = isSlctCC ? Op0.getValueType() : Op0.getOperand(0).getValueType();
Chris Lattnerd1980a52009-03-12 06:52:53 +00007028 bool isInt = OpVT.isInteger();
7029 CC = ISD::getSetCCInverse(CC, isInt);
7030
7031 if (!TLI.isCondCodeLegal(CC, OpVT))
7032 return SDValue(); // Inverse operator isn't legal.
7033
7034 DoXform = true;
7035 InvCC = true;
7036 }
7037
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007038 if (!DoXform)
7039 return SDValue();
7040
7041 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
7042 if (isSlctCC)
7043 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
7044 Slct.getOperand(0), Slct.getOperand(1), CC);
7045 SDValue CCOp = Slct.getOperand(0);
7046 if (InvCC)
7047 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
7048 CCOp.getOperand(0), CCOp.getOperand(1), CC);
7049 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
7050 CCOp, OtherOp, Result);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007051}
7052
Eric Christopherfa6f5912011-06-29 21:10:36 +00007053// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattner189531f2011-06-14 23:48:48 +00007054// (only after legalization).
7055static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7056 TargetLowering::DAGCombinerInfo &DCI,
7057 const ARMSubtarget *Subtarget) {
7058
7059 // Only perform optimization if after legalize, and if NEON is available. We
7060 // also expected both operands to be BUILD_VECTORs.
7061 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7062 || N0.getOpcode() != ISD::BUILD_VECTOR
7063 || N1.getOpcode() != ISD::BUILD_VECTOR)
7064 return SDValue();
7065
7066 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7067 EVT VT = N->getValueType(0);
7068 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7069 return SDValue();
7070
7071 // Check that the vector operands are of the right form.
7072 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7073 // operands, where N is the size of the formed vector.
7074 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7075 // index such that we have a pair wise add pattern.
Tanya Lattner189531f2011-06-14 23:48:48 +00007076
7077 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson7a10ab72011-06-15 06:04:34 +00007078 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattner189531f2011-06-14 23:48:48 +00007079 return SDValue();
Bob Wilson7a10ab72011-06-15 06:04:34 +00007080 SDValue Vec = N0->getOperand(0)->getOperand(0);
7081 SDNode *V = Vec.getNode();
7082 unsigned nextIndex = 0;
Tanya Lattner189531f2011-06-14 23:48:48 +00007083
Eric Christopherfa6f5912011-06-29 21:10:36 +00007084 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattner189531f2011-06-14 23:48:48 +00007085 // check to see if each of their operands are an EXTRACT_VECTOR with
7086 // the same vector and appropriate index.
7087 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7088 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7089 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopherfa6f5912011-06-29 21:10:36 +00007090
Tanya Lattner189531f2011-06-14 23:48:48 +00007091 SDValue ExtVec0 = N0->getOperand(i);
7092 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007093
Tanya Lattner189531f2011-06-14 23:48:48 +00007094 // First operand is the vector, verify its the same.
7095 if (V != ExtVec0->getOperand(0).getNode() ||
7096 V != ExtVec1->getOperand(0).getNode())
7097 return SDValue();
Eric Christopherfa6f5912011-06-29 21:10:36 +00007098
Tanya Lattner189531f2011-06-14 23:48:48 +00007099 // Second is the constant, verify its correct.
7100 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7101 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopherfa6f5912011-06-29 21:10:36 +00007102
Tanya Lattner189531f2011-06-14 23:48:48 +00007103 // For the constant, we want to see all the even or all the odd.
7104 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7105 || C1->getZExtValue() != nextIndex+1)
7106 return SDValue();
7107
7108 // Increment index.
7109 nextIndex+=2;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007110 } else
Tanya Lattner189531f2011-06-14 23:48:48 +00007111 return SDValue();
7112 }
7113
7114 // Create VPADDL node.
7115 SelectionDAG &DAG = DCI.DAG;
7116 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattner189531f2011-06-14 23:48:48 +00007117
7118 // Build operand list.
7119 SmallVector<SDValue, 8> Ops;
7120 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7121 TLI.getPointerTy()));
7122
7123 // Input is the vector.
7124 Ops.push_back(Vec);
Eric Christopherfa6f5912011-06-29 21:10:36 +00007125
Tanya Lattner189531f2011-06-14 23:48:48 +00007126 // Get widened type and narrowed type.
7127 MVT widenType;
7128 unsigned numElem = VT.getVectorNumElements();
7129 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
7130 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7131 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7132 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7133 default:
Craig Topperbc219812012-02-07 02:50:20 +00007134 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattner189531f2011-06-14 23:48:48 +00007135 }
7136
7137 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
7138 widenType, &Ops[0], Ops.size());
7139 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, tmp);
7140}
7141
Bob Wilson3d5792a2010-07-29 20:34:14 +00007142/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7143/// operands N0 and N1. This is a helper for PerformADDCombine that is
7144/// called with the default operands, and if that fails, with commuted
7145/// operands.
7146static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattner189531f2011-06-14 23:48:48 +00007147 TargetLowering::DAGCombinerInfo &DCI,
7148 const ARMSubtarget *Subtarget){
7149
7150 // Attempt to create vpaddl for this add.
7151 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7152 if (Result.getNode())
7153 return Result;
Eric Christopherfa6f5912011-06-29 21:10:36 +00007154
Chris Lattnerd1980a52009-03-12 06:52:53 +00007155 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7156 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
7157 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7158 if (Result.getNode()) return Result;
7159 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007160 return SDValue();
7161}
7162
Bob Wilson3d5792a2010-07-29 20:34:14 +00007163/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7164///
7165static SDValue PerformADDCombine(SDNode *N,
Tanya Lattner189531f2011-06-14 23:48:48 +00007166 TargetLowering::DAGCombinerInfo &DCI,
7167 const ARMSubtarget *Subtarget) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007168 SDValue N0 = N->getOperand(0);
7169 SDValue N1 = N->getOperand(1);
7170
7171 // First try with the default operand order.
Tanya Lattner189531f2011-06-14 23:48:48 +00007172 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007173 if (Result.getNode())
7174 return Result;
7175
7176 // If that didn't work, try again with the operands commuted.
Tanya Lattner189531f2011-06-14 23:48:48 +00007177 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson3d5792a2010-07-29 20:34:14 +00007178}
7179
Chris Lattnerd1980a52009-03-12 06:52:53 +00007180/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson3d5792a2010-07-29 20:34:14 +00007181///
Chris Lattnerd1980a52009-03-12 06:52:53 +00007182static SDValue PerformSUBCombine(SDNode *N,
7183 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson3d5792a2010-07-29 20:34:14 +00007184 SDValue N0 = N->getOperand(0);
7185 SDValue N1 = N->getOperand(1);
Bob Wilson2dc4f542009-03-20 22:42:55 +00007186
Chris Lattnerd1980a52009-03-12 06:52:53 +00007187 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7188 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
7189 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7190 if (Result.getNode()) return Result;
7191 }
Bob Wilson2dc4f542009-03-20 22:42:55 +00007192
Chris Lattnerd1980a52009-03-12 06:52:53 +00007193 return SDValue();
7194}
7195
Evan Cheng463d3582011-03-31 19:38:48 +00007196/// PerformVMULCombine
7197/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7198/// special multiplier accumulator forwarding.
7199/// vmul d3, d0, d2
7200/// vmla d3, d1, d2
7201/// is faster than
7202/// vadd d3, d0, d1
7203/// vmul d3, d3, d2
7204static SDValue PerformVMULCombine(SDNode *N,
7205 TargetLowering::DAGCombinerInfo &DCI,
7206 const ARMSubtarget *Subtarget) {
7207 if (!Subtarget->hasVMLxForwarding())
7208 return SDValue();
7209
7210 SelectionDAG &DAG = DCI.DAG;
7211 SDValue N0 = N->getOperand(0);
7212 SDValue N1 = N->getOperand(1);
7213 unsigned Opcode = N0.getOpcode();
7214 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7215 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier689edc82011-06-16 01:21:54 +00007216 Opcode = N1.getOpcode();
Evan Cheng463d3582011-03-31 19:38:48 +00007217 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7218 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7219 return SDValue();
7220 std::swap(N0, N1);
7221 }
7222
7223 EVT VT = N->getValueType(0);
7224 DebugLoc DL = N->getDebugLoc();
7225 SDValue N00 = N0->getOperand(0);
7226 SDValue N01 = N0->getOperand(1);
7227 return DAG.getNode(Opcode, DL, VT,
7228 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7229 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7230}
7231
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007232static SDValue PerformMULCombine(SDNode *N,
7233 TargetLowering::DAGCombinerInfo &DCI,
7234 const ARMSubtarget *Subtarget) {
7235 SelectionDAG &DAG = DCI.DAG;
7236
7237 if (Subtarget->isThumb1Only())
7238 return SDValue();
7239
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007240 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7241 return SDValue();
7242
7243 EVT VT = N->getValueType(0);
Evan Cheng463d3582011-03-31 19:38:48 +00007244 if (VT.is64BitVector() || VT.is128BitVector())
7245 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007246 if (VT != MVT::i32)
7247 return SDValue();
7248
7249 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7250 if (!C)
7251 return SDValue();
7252
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007253 int64_t MulAmt = C->getSExtValue();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007254 unsigned ShiftAmt = CountTrailingZeros_64(MulAmt);
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007255
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007256 ShiftAmt = ShiftAmt & (32 - 1);
7257 SDValue V = N->getOperand(0);
7258 DebugLoc DL = N->getDebugLoc();
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007259
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007260 SDValue Res;
7261 MulAmt >>= ShiftAmt;
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007262
7263 if (MulAmt >= 0) {
7264 if (isPowerOf2_32(MulAmt - 1)) {
7265 // (mul x, 2^N + 1) => (add (shl x, N), x)
7266 Res = DAG.getNode(ISD::ADD, DL, VT,
7267 V,
7268 DAG.getNode(ISD::SHL, DL, VT,
7269 V,
7270 DAG.getConstant(Log2_32(MulAmt - 1),
7271 MVT::i32)));
7272 } else if (isPowerOf2_32(MulAmt + 1)) {
7273 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7274 Res = DAG.getNode(ISD::SUB, DL, VT,
7275 DAG.getNode(ISD::SHL, DL, VT,
7276 V,
7277 DAG.getConstant(Log2_32(MulAmt + 1),
7278 MVT::i32)),
7279 V);
7280 } else
7281 return SDValue();
7282 } else {
7283 uint64_t MulAmtAbs = -MulAmt;
7284 if (isPowerOf2_32(MulAmtAbs + 1)) {
7285 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7286 Res = DAG.getNode(ISD::SUB, DL, VT,
7287 V,
7288 DAG.getNode(ISD::SHL, DL, VT,
7289 V,
7290 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7291 MVT::i32)));
7292 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7293 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7294 Res = DAG.getNode(ISD::ADD, DL, VT,
7295 V,
7296 DAG.getNode(ISD::SHL, DL, VT,
7297 V,
7298 DAG.getConstant(Log2_32(MulAmtAbs-1),
7299 MVT::i32)));
7300 Res = DAG.getNode(ISD::SUB, DL, VT,
7301 DAG.getConstant(0, MVT::i32),Res);
7302
7303 } else
7304 return SDValue();
7305 }
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007306
7307 if (ShiftAmt != 0)
Anton Korobeynikov2d7ea042012-03-19 19:19:50 +00007308 Res = DAG.getNode(ISD::SHL, DL, VT,
7309 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007310
7311 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4878b842010-05-16 08:54:20 +00007312 DCI.CombineTo(N, Res, false);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00007313 return SDValue();
7314}
7315
Evan Chengc892aeb2012-02-23 01:19:06 +00007316static bool isCMOVWithZeroOrAllOnesLHS(SDValue N, bool AllOnes) {
Jakob Stoklund Olesen1f1ab3e2012-08-17 16:59:09 +00007317 return N.getOpcode() == ARMISD::CMOV && N.getNode()->hasOneUse() &&
7318 isZeroOrAllOnes(N.getOperand(0), AllOnes);
Evan Chengc892aeb2012-02-23 01:19:06 +00007319}
7320
7321/// formConditionalOp - Combine an operation with a conditional move operand
7322/// to form a conditional op. e.g. (or x, (cmov 0, y, cond)) => (or.cond x, y)
7323/// (and x, (cmov -1, y, cond)) => (and.cond, x, y)
7324static SDValue formConditionalOp(SDNode *N, SelectionDAG &DAG,
7325 bool Commutable) {
7326 SDValue N0 = N->getOperand(0);
7327 SDValue N1 = N->getOperand(1);
7328
7329 bool isAND = N->getOpcode() == ISD::AND;
7330 bool isCand = isCMOVWithZeroOrAllOnesLHS(N1, isAND);
7331 if (!isCand && Commutable) {
7332 isCand = isCMOVWithZeroOrAllOnesLHS(N0, isAND);
7333 if (isCand)
7334 std::swap(N0, N1);
7335 }
7336 if (!isCand)
7337 return SDValue();
7338
7339 unsigned Opc = 0;
7340 switch (N->getOpcode()) {
7341 default: llvm_unreachable("Unexpected node");
7342 case ISD::AND: Opc = ARMISD::CAND; break;
7343 case ISD::OR: Opc = ARMISD::COR; break;
7344 case ISD::XOR: Opc = ARMISD::CXOR; break;
7345 }
7346 return DAG.getNode(Opc, N->getDebugLoc(), N->getValueType(0), N0,
7347 N1.getOperand(1), N1.getOperand(2), N1.getOperand(3),
7348 N1.getOperand(4));
7349}
7350
Owen Anderson080c0922010-11-05 19:27:46 +00007351static SDValue PerformANDCombine(SDNode *N,
Evan Chengc892aeb2012-02-23 01:19:06 +00007352 TargetLowering::DAGCombinerInfo &DCI,
7353 const ARMSubtarget *Subtarget) {
Owen Anderson76706012011-04-05 21:48:57 +00007354
Owen Anderson080c0922010-11-05 19:27:46 +00007355 // Attempt to use immediate-form VBIC
7356 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7357 DebugLoc dl = N->getDebugLoc();
7358 EVT VT = N->getValueType(0);
7359 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007360
Tanya Lattner0433b212011-04-07 15:24:20 +00007361 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7362 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007363
Owen Anderson080c0922010-11-05 19:27:46 +00007364 APInt SplatBits, SplatUndef;
7365 unsigned SplatBitSize;
7366 bool HasAnyUndefs;
7367 if (BVN &&
7368 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7369 if (SplatBitSize <= 64) {
7370 EVT VbicVT;
7371 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
7372 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007373 DAG, VbicVT, VT.is128BitVector(),
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007374 OtherModImm);
Owen Anderson080c0922010-11-05 19:27:46 +00007375 if (Val.getNode()) {
7376 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007377 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson080c0922010-11-05 19:27:46 +00007378 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007379 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson080c0922010-11-05 19:27:46 +00007380 }
7381 }
7382 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007383
Evan Chengc892aeb2012-02-23 01:19:06 +00007384 if (!Subtarget->isThumb1Only()) {
7385 // (and x, (cmov -1, y, cond)) => (and.cond x, y)
7386 SDValue CAND = formConditionalOp(N, DAG, true);
7387 if (CAND.getNode())
7388 return CAND;
7389 }
7390
Owen Anderson080c0922010-11-05 19:27:46 +00007391 return SDValue();
7392}
7393
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007394/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
7395static SDValue PerformORCombine(SDNode *N,
7396 TargetLowering::DAGCombinerInfo &DCI,
7397 const ARMSubtarget *Subtarget) {
Owen Anderson60f48702010-11-03 23:15:26 +00007398 // Attempt to use immediate-form VORR
7399 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
7400 DebugLoc dl = N->getDebugLoc();
7401 EVT VT = N->getValueType(0);
7402 SelectionDAG &DAG = DCI.DAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007403
Tanya Lattner0433b212011-04-07 15:24:20 +00007404 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7405 return SDValue();
Andrew Trick1c3af772011-04-23 03:55:32 +00007406
Owen Anderson60f48702010-11-03 23:15:26 +00007407 APInt SplatBits, SplatUndef;
7408 unsigned SplatBitSize;
7409 bool HasAnyUndefs;
7410 if (BVN && Subtarget->hasNEON() &&
7411 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
7412 if (SplatBitSize <= 64) {
7413 EVT VorrVT;
7414 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
7415 SplatUndef.getZExtValue(), SplatBitSize,
Owen Anderson36fa3ea2010-11-05 21:57:54 +00007416 DAG, VorrVT, VT.is128BitVector(),
7417 OtherModImm);
Owen Anderson60f48702010-11-03 23:15:26 +00007418 if (Val.getNode()) {
7419 SDValue Input =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007420 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Anderson60f48702010-11-03 23:15:26 +00007421 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007422 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Anderson60f48702010-11-03 23:15:26 +00007423 }
7424 }
7425 }
7426
Evan Chengc892aeb2012-02-23 01:19:06 +00007427 if (!Subtarget->isThumb1Only()) {
7428 // (or x, (cmov 0, y, cond)) => (or.cond x, y)
7429 SDValue COR = formConditionalOp(N, DAG, true);
7430 if (COR.getNode())
7431 return COR;
7432 }
7433
Nadav Rotemdf832032012-08-13 18:52:44 +00007434
7435 // The code below optimizes (or (and X, Y), Z).
7436 // The AND operand needs to have a single user to make these optimizations
7437 // profitable.
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007438 SDValue N0 = N->getOperand(0);
Nadav Rotemdf832032012-08-13 18:52:44 +00007439 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007440 return SDValue();
7441 SDValue N1 = N->getOperand(1);
7442
7443 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
7444 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
7445 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
7446 APInt SplatUndef;
7447 unsigned SplatBitSize;
7448 bool HasAnyUndefs;
7449
7450 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
7451 APInt SplatBits0;
7452 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
7453 HasAnyUndefs) && !HasAnyUndefs) {
7454 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
7455 APInt SplatBits1;
7456 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
7457 HasAnyUndefs) && !HasAnyUndefs &&
7458 SplatBits0 == ~SplatBits1) {
7459 // Canonicalize the vector type to make instruction selection simpler.
7460 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
7461 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
7462 N0->getOperand(1), N0->getOperand(0),
Cameron Zwarich5af60ce2011-04-13 21:01:19 +00007463 N1->getOperand(0));
Cameron Zwarichc0e6d782011-03-30 23:01:21 +00007464 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
7465 }
7466 }
7467 }
7468
Jim Grosbach54238562010-07-17 03:30:54 +00007469 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
7470 // reasonable.
7471
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007472 // BFI is only available on V6T2+
7473 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
7474 return SDValue();
7475
Jim Grosbach54238562010-07-17 03:30:54 +00007476 DebugLoc DL = N->getDebugLoc();
7477 // 1) or (and A, mask), val => ARMbfi A, val, mask
7478 // iff (val & mask) == val
7479 //
7480 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
7481 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007482 // && mask == ~mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007483 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopher29aeed12011-03-26 01:21:03 +00007484 // && ~mask == mask2
Jim Grosbach54238562010-07-17 03:30:54 +00007485 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007486
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007487 if (VT != MVT::i32)
7488 return SDValue();
7489
Evan Cheng30fb13f2010-12-13 20:32:54 +00007490 SDValue N00 = N0.getOperand(0);
Jim Grosbach54238562010-07-17 03:30:54 +00007491
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007492 // The value and the mask need to be constants so we can verify this is
7493 // actually a bitfield set. If the mask is 0xffff, we can do better
7494 // via a movt instruction, so don't use BFI in that case.
Evan Cheng30fb13f2010-12-13 20:32:54 +00007495 SDValue MaskOp = N0.getOperand(1);
7496 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
7497 if (!MaskC)
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007498 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007499 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007500 if (Mask == 0xffff)
7501 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007502 SDValue Res;
7503 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007504 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
7505 if (N1C) {
7506 unsigned Val = N1C->getZExtValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007507 if ((Val & ~Mask) != Val)
Jim Grosbach54238562010-07-17 03:30:54 +00007508 return SDValue();
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007509
Evan Chenga9688c42010-12-11 04:11:38 +00007510 if (ARM::isBitFieldInvertedMask(Mask)) {
7511 Val >>= CountTrailingZeros_32(~Mask);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007512
Evan Cheng30fb13f2010-12-13 20:32:54 +00007513 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Chenga9688c42010-12-11 04:11:38 +00007514 DAG.getConstant(Val, MVT::i32),
7515 DAG.getConstant(Mask, MVT::i32));
7516
7517 // Do not add new nodes to DAG combiner worklist.
7518 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007519 return SDValue();
Evan Chenga9688c42010-12-11 04:11:38 +00007520 }
Jim Grosbach54238562010-07-17 03:30:54 +00007521 } else if (N1.getOpcode() == ISD::AND) {
7522 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng30fb13f2010-12-13 20:32:54 +00007523 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7524 if (!N11C)
Jim Grosbach54238562010-07-17 03:30:54 +00007525 return SDValue();
Evan Cheng30fb13f2010-12-13 20:32:54 +00007526 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007527
Eric Christopher29aeed12011-03-26 01:21:03 +00007528 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
7529 // as is to match.
Jim Grosbach54238562010-07-17 03:30:54 +00007530 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007531 (Mask == ~Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007532 // The pack halfword instruction works better for masks that fit it,
7533 // so use that when it's available.
7534 if (Subtarget->hasT2ExtractPack() &&
7535 (Mask == 0xffff || Mask == 0xffff0000))
7536 return SDValue();
7537 // 2a
Eric Christopher29aeed12011-03-26 01:21:03 +00007538 unsigned amt = CountTrailingZeros_32(Mask2);
Jim Grosbach54238562010-07-17 03:30:54 +00007539 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopher29aeed12011-03-26 01:21:03 +00007540 DAG.getConstant(amt, MVT::i32));
Evan Cheng30fb13f2010-12-13 20:32:54 +00007541 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbach54238562010-07-17 03:30:54 +00007542 DAG.getConstant(Mask, MVT::i32));
7543 // Do not add new nodes to DAG combiner worklist.
7544 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007545 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007546 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopher29aeed12011-03-26 01:21:03 +00007547 (~Mask == Mask2)) {
Jim Grosbach54238562010-07-17 03:30:54 +00007548 // The pack halfword instruction works better for masks that fit it,
7549 // so use that when it's available.
7550 if (Subtarget->hasT2ExtractPack() &&
7551 (Mask2 == 0xffff || Mask2 == 0xffff0000))
7552 return SDValue();
7553 // 2b
7554 unsigned lsb = CountTrailingZeros_32(Mask);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007555 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbach54238562010-07-17 03:30:54 +00007556 DAG.getConstant(lsb, MVT::i32));
7557 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopher29aeed12011-03-26 01:21:03 +00007558 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbach54238562010-07-17 03:30:54 +00007559 // Do not add new nodes to DAG combiner worklist.
7560 DCI.CombineTo(N, Res, false);
Evan Cheng30fb13f2010-12-13 20:32:54 +00007561 return SDValue();
Jim Grosbach54238562010-07-17 03:30:54 +00007562 }
7563 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007564
Evan Cheng30fb13f2010-12-13 20:32:54 +00007565 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
7566 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
7567 ARM::isBitFieldInvertedMask(~Mask)) {
7568 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
7569 // where lsb(mask) == #shamt and masked bits of B are known zero.
7570 SDValue ShAmt = N00.getOperand(1);
7571 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
7572 unsigned LSB = CountTrailingZeros_32(Mask);
7573 if (ShAmtC != LSB)
7574 return SDValue();
7575
7576 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
7577 DAG.getConstant(~Mask, MVT::i32));
7578
7579 // Do not add new nodes to DAG combiner worklist.
7580 DCI.CombineTo(N, Res, false);
7581 }
7582
Jim Grosbach469bbdb2010-07-16 23:05:05 +00007583 return SDValue();
7584}
7585
Evan Chengc892aeb2012-02-23 01:19:06 +00007586static SDValue PerformXORCombine(SDNode *N,
7587 TargetLowering::DAGCombinerInfo &DCI,
7588 const ARMSubtarget *Subtarget) {
7589 EVT VT = N->getValueType(0);
7590 SelectionDAG &DAG = DCI.DAG;
7591
7592 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
7593 return SDValue();
7594
7595 if (!Subtarget->isThumb1Only()) {
7596 // (xor x, (cmov 0, y, cond)) => (xor.cond x, y)
7597 SDValue CXOR = formConditionalOp(N, DAG, true);
7598 if (CXOR.getNode())
7599 return CXOR;
7600 }
7601
7602 return SDValue();
7603}
7604
Evan Chengbf188ae2011-06-15 01:12:31 +00007605/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
7606/// the bits being cleared by the AND are not demanded by the BFI.
Evan Cheng0c1aec12010-12-14 03:22:07 +00007607static SDValue PerformBFICombine(SDNode *N,
7608 TargetLowering::DAGCombinerInfo &DCI) {
7609 SDValue N1 = N->getOperand(1);
7610 if (N1.getOpcode() == ISD::AND) {
7611 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
7612 if (!N11C)
7613 return SDValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007614 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
7615 unsigned LSB = CountTrailingZeros_32(~InvMask);
7616 unsigned Width = (32 - CountLeadingZeros_32(~InvMask)) - LSB;
7617 unsigned Mask = (1 << Width)-1;
Evan Cheng0c1aec12010-12-14 03:22:07 +00007618 unsigned Mask2 = N11C->getZExtValue();
Evan Chengbf188ae2011-06-15 01:12:31 +00007619 if ((Mask & (~Mask2)) == 0)
Evan Cheng0c1aec12010-12-14 03:22:07 +00007620 return DCI.DAG.getNode(ARMISD::BFI, N->getDebugLoc(), N->getValueType(0),
7621 N->getOperand(0), N1.getOperand(0),
7622 N->getOperand(2));
7623 }
7624 return SDValue();
7625}
7626
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007627/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
7628/// ARMISD::VMOVRRD.
7629static SDValue PerformVMOVRRDCombine(SDNode *N,
7630 TargetLowering::DAGCombinerInfo &DCI) {
7631 // vmovrrd(vmovdrr x, y) -> x,y
7632 SDValue InDouble = N->getOperand(0);
7633 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
7634 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich4071a712011-04-02 02:40:43 +00007635
7636 // vmovrrd(load f64) -> (load i32), (load i32)
7637 SDNode *InNode = InDouble.getNode();
7638 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
7639 InNode->getValueType(0) == MVT::f64 &&
7640 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
7641 !cast<LoadSDNode>(InNode)->isVolatile()) {
7642 // TODO: Should this be done for non-FrameIndex operands?
7643 LoadSDNode *LD = cast<LoadSDNode>(InNode);
7644
7645 SelectionDAG &DAG = DCI.DAG;
7646 DebugLoc DL = LD->getDebugLoc();
7647 SDValue BasePtr = LD->getBasePtr();
7648 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
7649 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007650 LD->isNonTemporal(), LD->isInvariant(),
7651 LD->getAlignment());
Cameron Zwarich4071a712011-04-02 02:40:43 +00007652
7653 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7654 DAG.getConstant(4, MVT::i32));
7655 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
7656 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007657 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich4071a712011-04-02 02:40:43 +00007658 std::min(4U, LD->getAlignment() / 2));
7659
7660 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
7661 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
7662 DCI.RemoveFromWorklist(LD);
7663 DAG.DeleteNode(LD);
7664 return Result;
7665 }
7666
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007667 return SDValue();
7668}
7669
7670/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
7671/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
7672static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
7673 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
7674 SDValue Op0 = N->getOperand(0);
7675 SDValue Op1 = N->getOperand(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007676 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007677 Op0 = Op0.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007678 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007679 Op1 = Op1.getOperand(0);
7680 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
7681 Op0.getNode() == Op1.getNode() &&
7682 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007683 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bob Wilson0b8ccb82010-09-22 22:09:21 +00007684 N->getValueType(0), Op0.getOperand(0));
7685 return SDValue();
7686}
7687
Bob Wilson31600902010-12-21 06:43:19 +00007688/// PerformSTORECombine - Target-specific dag combine xforms for
7689/// ISD::STORE.
7690static SDValue PerformSTORECombine(SDNode *N,
7691 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson31600902010-12-21 06:43:19 +00007692 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosier7f354552012-04-09 20:32:02 +00007693 if (St->isVolatile())
7694 return SDValue();
7695
Andrew Trick49b446f2012-07-18 18:34:24 +00007696 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosier7f354552012-04-09 20:32:02 +00007697 // pack all of the elements in one place. Next, store to memory in fewer
7698 // chunks.
Bob Wilson31600902010-12-21 06:43:19 +00007699 SDValue StVal = St->getValue();
Chad Rosier7f354552012-04-09 20:32:02 +00007700 EVT VT = StVal.getValueType();
7701 if (St->isTruncatingStore() && VT.isVector()) {
7702 SelectionDAG &DAG = DCI.DAG;
7703 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7704 EVT StVT = St->getMemoryVT();
7705 unsigned NumElems = VT.getVectorNumElements();
7706 assert(StVT != VT && "Cannot truncate to the same type");
7707 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
7708 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
7709
7710 // From, To sizes and ElemCount must be pow of two
7711 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
7712
7713 // We are going to use the original vector elt for storing.
7714 // Accumulated smaller vector elements must be a multiple of the store size.
7715 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
7716
7717 unsigned SizeRatio = FromEltSz / ToEltSz;
7718 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
7719
7720 // Create a type on which we perform the shuffle.
7721 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
7722 NumElems*SizeRatio);
7723 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
7724
7725 DebugLoc DL = St->getDebugLoc();
7726 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
7727 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
7728 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
7729
7730 // Can't shuffle using an illegal type.
7731 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
7732
7733 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
7734 DAG.getUNDEF(WideVec.getValueType()),
7735 ShuffleVec.data());
7736 // At this point all of the data is stored at the bottom of the
7737 // register. We now need to save it to mem.
7738
7739 // Find the largest store unit
7740 MVT StoreType = MVT::i8;
7741 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
7742 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
7743 MVT Tp = (MVT::SimpleValueType)tp;
7744 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
7745 StoreType = Tp;
7746 }
7747 // Didn't find a legal store type.
7748 if (!TLI.isTypeLegal(StoreType))
7749 return SDValue();
7750
7751 // Bitcast the original vector into a vector of store-size units
7752 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
7753 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
7754 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
7755 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
7756 SmallVector<SDValue, 8> Chains;
7757 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
7758 TLI.getPointerTy());
7759 SDValue BasePtr = St->getBasePtr();
7760
7761 // Perform one or more big stores into memory.
7762 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
7763 for (unsigned I = 0; I < E; I++) {
7764 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
7765 StoreType, ShuffWide,
7766 DAG.getIntPtrConstant(I));
7767 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
7768 St->getPointerInfo(), St->isVolatile(),
7769 St->isNonTemporal(), St->getAlignment());
7770 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
7771 Increment);
7772 Chains.push_back(Ch);
7773 }
7774 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
7775 Chains.size());
7776 }
7777
7778 if (!ISD::isNormalStore(St))
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007779 return SDValue();
7780
Chad Rosier96b66d62012-04-09 19:38:15 +00007781 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
7782 // ARM stores of arguments in the same cache line.
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007783 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier96b66d62012-04-09 19:38:15 +00007784 StVal.getNode()->hasOneUse()) {
Cameron Zwarichd0aacbc2011-04-12 02:24:17 +00007785 SelectionDAG &DAG = DCI.DAG;
7786 DebugLoc DL = St->getDebugLoc();
7787 SDValue BasePtr = St->getBasePtr();
7788 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
7789 StVal.getNode()->getOperand(0), BasePtr,
7790 St->getPointerInfo(), St->isVolatile(),
7791 St->isNonTemporal(), St->getAlignment());
7792
7793 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
7794 DAG.getConstant(4, MVT::i32));
7795 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
7796 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
7797 St->isNonTemporal(),
7798 std::min(4U, St->getAlignment() / 2));
7799 }
7800
7801 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson31600902010-12-21 06:43:19 +00007802 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7803 return SDValue();
7804
Chad Rosier96b66d62012-04-09 19:38:15 +00007805 // Bitcast an i64 store extracted from a vector to f64.
7806 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson31600902010-12-21 06:43:19 +00007807 SelectionDAG &DAG = DCI.DAG;
7808 DebugLoc dl = StVal.getDebugLoc();
7809 SDValue IntVec = StVal.getOperand(0);
7810 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7811 IntVec.getValueType().getVectorNumElements());
7812 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
7813 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
7814 Vec, StVal.getOperand(1));
7815 dl = N->getDebugLoc();
7816 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
7817 // Make the DAGCombiner fold the bitcasts.
7818 DCI.AddToWorklist(Vec.getNode());
7819 DCI.AddToWorklist(ExtElt.getNode());
7820 DCI.AddToWorklist(V.getNode());
7821 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
7822 St->getPointerInfo(), St->isVolatile(),
7823 St->isNonTemporal(), St->getAlignment(),
7824 St->getTBAAInfo());
7825}
7826
7827/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
7828/// are normal, non-volatile loads. If so, it is profitable to bitcast an
7829/// i64 vector to have f64 elements, since the value can then be loaded
7830/// directly into a VFP register.
7831static bool hasNormalLoadOperand(SDNode *N) {
7832 unsigned NumElts = N->getValueType(0).getVectorNumElements();
7833 for (unsigned i = 0; i < NumElts; ++i) {
7834 SDNode *Elt = N->getOperand(i).getNode();
7835 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
7836 return true;
7837 }
7838 return false;
7839}
7840
Bob Wilson75f02882010-09-17 22:59:05 +00007841/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
7842/// ISD::BUILD_VECTOR.
Bob Wilson31600902010-12-21 06:43:19 +00007843static SDValue PerformBUILD_VECTORCombine(SDNode *N,
7844 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilson75f02882010-09-17 22:59:05 +00007845 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
7846 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
7847 // into a pair of GPRs, which is fine when the value is used as a scalar,
7848 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson31600902010-12-21 06:43:19 +00007849 SelectionDAG &DAG = DCI.DAG;
7850 if (N->getNumOperands() == 2) {
7851 SDValue RV = PerformVMOVDRRCombine(N, DAG);
7852 if (RV.getNode())
7853 return RV;
7854 }
Bob Wilson75f02882010-09-17 22:59:05 +00007855
Bob Wilson31600902010-12-21 06:43:19 +00007856 // Load i64 elements as f64 values so that type legalization does not split
7857 // them up into i32 values.
7858 EVT VT = N->getValueType(0);
7859 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
7860 return SDValue();
7861 DebugLoc dl = N->getDebugLoc();
7862 SmallVector<SDValue, 8> Ops;
7863 unsigned NumElts = VT.getVectorNumElements();
7864 for (unsigned i = 0; i < NumElts; ++i) {
7865 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
7866 Ops.push_back(V);
7867 // Make the DAGCombiner fold the bitcast.
7868 DCI.AddToWorklist(V.getNode());
7869 }
7870 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
7871 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
7872 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
7873}
7874
7875/// PerformInsertEltCombine - Target-specific dag combine xforms for
7876/// ISD::INSERT_VECTOR_ELT.
7877static SDValue PerformInsertEltCombine(SDNode *N,
7878 TargetLowering::DAGCombinerInfo &DCI) {
7879 // Bitcast an i64 load inserted into a vector to f64.
7880 // Otherwise, the i64 value will be legalized to a pair of i32 values.
7881 EVT VT = N->getValueType(0);
7882 SDNode *Elt = N->getOperand(1).getNode();
7883 if (VT.getVectorElementType() != MVT::i64 ||
7884 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
7885 return SDValue();
7886
7887 SelectionDAG &DAG = DCI.DAG;
7888 DebugLoc dl = N->getDebugLoc();
7889 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
7890 VT.getVectorNumElements());
7891 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
7892 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
7893 // Make the DAGCombiner fold the bitcasts.
7894 DCI.AddToWorklist(Vec.getNode());
7895 DCI.AddToWorklist(V.getNode());
7896 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
7897 Vec, V, N->getOperand(2));
7898 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilson75f02882010-09-17 22:59:05 +00007899}
7900
Bob Wilsonf20700c2010-10-27 20:38:28 +00007901/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
7902/// ISD::VECTOR_SHUFFLE.
7903static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
7904 // The LLVM shufflevector instruction does not require the shuffle mask
7905 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
7906 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
7907 // operands do not match the mask length, they are extended by concatenating
7908 // them with undef vectors. That is probably the right thing for other
7909 // targets, but for NEON it is better to concatenate two double-register
7910 // size vector operands into a single quad-register size vector. Do that
7911 // transformation here:
7912 // shuffle(concat(v1, undef), concat(v2, undef)) ->
7913 // shuffle(concat(v1, v2), undef)
7914 SDValue Op0 = N->getOperand(0);
7915 SDValue Op1 = N->getOperand(1);
7916 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
7917 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
7918 Op0.getNumOperands() != 2 ||
7919 Op1.getNumOperands() != 2)
7920 return SDValue();
7921 SDValue Concat0Op1 = Op0.getOperand(1);
7922 SDValue Concat1Op1 = Op1.getOperand(1);
7923 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
7924 Concat1Op1.getOpcode() != ISD::UNDEF)
7925 return SDValue();
7926 // Skip the transformation if any of the types are illegal.
7927 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7928 EVT VT = N->getValueType(0);
7929 if (!TLI.isTypeLegal(VT) ||
7930 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
7931 !TLI.isTypeLegal(Concat1Op1.getValueType()))
7932 return SDValue();
7933
7934 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
7935 Op0.getOperand(0), Op1.getOperand(0));
7936 // Translate the shuffle mask.
7937 SmallVector<int, 16> NewMask;
7938 unsigned NumElts = VT.getVectorNumElements();
7939 unsigned HalfElts = NumElts/2;
7940 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7941 for (unsigned n = 0; n < NumElts; ++n) {
7942 int MaskElt = SVN->getMaskElt(n);
7943 int NewElt = -1;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007944 if (MaskElt < (int)HalfElts)
Bob Wilsonf20700c2010-10-27 20:38:28 +00007945 NewElt = MaskElt;
Bob Wilson1fa9d302010-10-27 23:49:00 +00007946 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonf20700c2010-10-27 20:38:28 +00007947 NewElt = HalfElts + MaskElt - NumElts;
7948 NewMask.push_back(NewElt);
7949 }
7950 return DAG.getVectorShuffle(VT, N->getDebugLoc(), NewConcat,
7951 DAG.getUNDEF(VT), NewMask.data());
7952}
7953
Bob Wilson1c3ef902011-02-07 17:43:21 +00007954/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
7955/// NEON load/store intrinsics to merge base address updates.
7956static SDValue CombineBaseUpdate(SDNode *N,
7957 TargetLowering::DAGCombinerInfo &DCI) {
7958 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7959 return SDValue();
7960
7961 SelectionDAG &DAG = DCI.DAG;
7962 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
7963 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
7964 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
7965 SDValue Addr = N->getOperand(AddrOpIdx);
7966
7967 // Search for a use of the address operand that is an increment.
7968 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
7969 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
7970 SDNode *User = *UI;
7971 if (User->getOpcode() != ISD::ADD ||
7972 UI.getUse().getResNo() != Addr.getResNo())
7973 continue;
7974
7975 // Check that the add is independent of the load/store. Otherwise, folding
7976 // it would create a cycle.
7977 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
7978 continue;
7979
7980 // Find the new opcode for the updating load/store.
7981 bool isLoad = true;
7982 bool isLaneOp = false;
7983 unsigned NewOpc = 0;
7984 unsigned NumVecs = 0;
7985 if (isIntrinsic) {
7986 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
7987 switch (IntNo) {
Craig Topperbc219812012-02-07 02:50:20 +00007988 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00007989 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
7990 NumVecs = 1; break;
7991 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
7992 NumVecs = 2; break;
7993 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
7994 NumVecs = 3; break;
7995 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
7996 NumVecs = 4; break;
7997 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
7998 NumVecs = 2; isLaneOp = true; break;
7999 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8000 NumVecs = 3; isLaneOp = true; break;
8001 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8002 NumVecs = 4; isLaneOp = true; break;
8003 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8004 NumVecs = 1; isLoad = false; break;
8005 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8006 NumVecs = 2; isLoad = false; break;
8007 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8008 NumVecs = 3; isLoad = false; break;
8009 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8010 NumVecs = 4; isLoad = false; break;
8011 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8012 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8013 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8014 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8015 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8016 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8017 }
8018 } else {
8019 isLaneOp = true;
8020 switch (N->getOpcode()) {
Craig Topperbc219812012-02-07 02:50:20 +00008021 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson1c3ef902011-02-07 17:43:21 +00008022 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8023 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8024 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8025 }
8026 }
8027
8028 // Find the size of memory referenced by the load/store.
8029 EVT VecTy;
8030 if (isLoad)
8031 VecTy = N->getValueType(0);
Owen Anderson76706012011-04-05 21:48:57 +00008032 else
Bob Wilson1c3ef902011-02-07 17:43:21 +00008033 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8034 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8035 if (isLaneOp)
8036 NumBytes /= VecTy.getVectorNumElements();
8037
8038 // If the increment is a constant, it must match the memory ref size.
8039 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8040 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8041 uint64_t IncVal = CInc->getZExtValue();
8042 if (IncVal != NumBytes)
8043 continue;
8044 } else if (NumBytes >= 3 * 16) {
8045 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8046 // separate instructions that make it harder to use a non-constant update.
8047 continue;
8048 }
8049
8050 // Create the new updating load/store node.
8051 EVT Tys[6];
8052 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8053 unsigned n;
8054 for (n = 0; n < NumResultVecs; ++n)
8055 Tys[n] = VecTy;
8056 Tys[n++] = MVT::i32;
8057 Tys[n] = MVT::Other;
8058 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
8059 SmallVector<SDValue, 8> Ops;
8060 Ops.push_back(N->getOperand(0)); // incoming chain
8061 Ops.push_back(N->getOperand(AddrOpIdx));
8062 Ops.push_back(Inc);
8063 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8064 Ops.push_back(N->getOperand(i));
8065 }
8066 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
8067 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, N->getDebugLoc(), SDTys,
8068 Ops.data(), Ops.size(),
8069 MemInt->getMemoryVT(),
8070 MemInt->getMemOperand());
8071
8072 // Update the uses.
8073 std::vector<SDValue> NewResults;
8074 for (unsigned i = 0; i < NumResultVecs; ++i) {
8075 NewResults.push_back(SDValue(UpdN.getNode(), i));
8076 }
8077 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8078 DCI.CombineTo(N, NewResults);
8079 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8080
8081 break;
Owen Anderson76706012011-04-05 21:48:57 +00008082 }
Bob Wilson1c3ef902011-02-07 17:43:21 +00008083 return SDValue();
8084}
8085
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008086/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8087/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8088/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8089/// return true.
8090static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8091 SelectionDAG &DAG = DCI.DAG;
8092 EVT VT = N->getValueType(0);
8093 // vldN-dup instructions only support 64-bit vectors for N > 1.
8094 if (!VT.is64BitVector())
8095 return false;
8096
8097 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8098 SDNode *VLD = N->getOperand(0).getNode();
8099 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8100 return false;
8101 unsigned NumVecs = 0;
8102 unsigned NewOpc = 0;
8103 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8104 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8105 NumVecs = 2;
8106 NewOpc = ARMISD::VLD2DUP;
8107 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8108 NumVecs = 3;
8109 NewOpc = ARMISD::VLD3DUP;
8110 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8111 NumVecs = 4;
8112 NewOpc = ARMISD::VLD4DUP;
8113 } else {
8114 return false;
8115 }
8116
8117 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8118 // numbers match the load.
8119 unsigned VLDLaneNo =
8120 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8121 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8122 UI != UE; ++UI) {
8123 // Ignore uses of the chain result.
8124 if (UI.getUse().getResNo() == NumVecs)
8125 continue;
8126 SDNode *User = *UI;
8127 if (User->getOpcode() != ARMISD::VDUPLANE ||
8128 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8129 return false;
8130 }
8131
8132 // Create the vldN-dup node.
8133 EVT Tys[5];
8134 unsigned n;
8135 for (n = 0; n < NumVecs; ++n)
8136 Tys[n] = VT;
8137 Tys[n] = MVT::Other;
8138 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
8139 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8140 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
8141 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, VLD->getDebugLoc(), SDTys,
8142 Ops, 2, VLDMemInt->getMemoryVT(),
8143 VLDMemInt->getMemOperand());
8144
8145 // Update the uses.
8146 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8147 UI != UE; ++UI) {
8148 unsigned ResNo = UI.getUse().getResNo();
8149 // Ignore uses of the chain result.
8150 if (ResNo == NumVecs)
8151 continue;
8152 SDNode *User = *UI;
8153 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8154 }
8155
8156 // Now the vldN-lane intrinsic is dead except for its chain result.
8157 // Update uses of the chain.
8158 std::vector<SDValue> VLDDupResults;
8159 for (unsigned n = 0; n < NumVecs; ++n)
8160 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8161 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8162 DCI.CombineTo(VLD, VLDDupResults);
8163
8164 return true;
8165}
8166
Bob Wilson9e82bf12010-07-14 01:22:12 +00008167/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8168/// ARMISD::VDUPLANE.
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008169static SDValue PerformVDUPLANECombine(SDNode *N,
8170 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson9e82bf12010-07-14 01:22:12 +00008171 SDValue Op = N->getOperand(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008172
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008173 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8174 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8175 if (CombineVLDDUP(N, DCI))
8176 return SDValue(N, 0);
8177
8178 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8179 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008180 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008181 Op = Op.getOperand(0);
Bob Wilson7e3f0d22010-07-14 06:31:50 +00008182 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson9e82bf12010-07-14 01:22:12 +00008183 return SDValue();
8184
8185 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8186 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8187 // The canonical VMOV for a zero vector uses a 32-bit element size.
8188 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8189 unsigned EltBits;
8190 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8191 EltSize = 8;
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008192 EVT VT = N->getValueType(0);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008193 if (EltSize > VT.getVectorElementType().getSizeInBits())
8194 return SDValue();
8195
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008196 return DCI.DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Bob Wilson9e82bf12010-07-14 01:22:12 +00008197}
8198
Eric Christopherfa6f5912011-06-29 21:10:36 +00008199// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosieref01edf2011-06-24 19:23:04 +00008200// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8201static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8202{
Chad Rosier118c9a02011-06-28 17:26:57 +00008203 integerPart cN;
8204 integerPart c0 = 0;
Chad Rosieref01edf2011-06-24 19:23:04 +00008205 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8206 I != E; I++) {
8207 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8208 if (!C)
8209 return false;
8210
Eric Christopherfa6f5912011-06-29 21:10:36 +00008211 bool isExact;
Chad Rosieref01edf2011-06-24 19:23:04 +00008212 APFloat APF = C->getValueAPF();
8213 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8214 != APFloat::opOK || !isExact)
8215 return false;
8216
8217 c0 = (I == 0) ? cN : c0;
8218 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8219 return false;
8220 }
8221 C = c0;
8222 return true;
8223}
8224
8225/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8226/// can replace combinations of VMUL and VCVT (floating-point to integer)
8227/// when the VMUL has a constant operand that is a power of 2.
8228///
8229/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8230/// vmul.f32 d16, d17, d16
8231/// vcvt.s32.f32 d16, d16
8232/// becomes:
8233/// vcvt.s32.f32 d16, d16, #3
8234static SDValue PerformVCVTCombine(SDNode *N,
8235 TargetLowering::DAGCombinerInfo &DCI,
8236 const ARMSubtarget *Subtarget) {
8237 SelectionDAG &DAG = DCI.DAG;
8238 SDValue Op = N->getOperand(0);
8239
8240 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8241 Op.getOpcode() != ISD::FMUL)
8242 return SDValue();
8243
8244 uint64_t C;
8245 SDValue N0 = Op->getOperand(0);
8246 SDValue ConstVec = Op->getOperand(1);
8247 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8248
Eric Christopherfa6f5912011-06-29 21:10:36 +00008249 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosieref01edf2011-06-24 19:23:04 +00008250 !isConstVecPow2(ConstVec, isSigned, C))
8251 return SDValue();
8252
8253 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
8254 Intrinsic::arm_neon_vcvtfp2fxu;
8255 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8256 N->getValueType(0),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008257 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
Chad Rosieref01edf2011-06-24 19:23:04 +00008258 DAG.getConstant(Log2_64(C), MVT::i32));
8259}
8260
8261/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
8262/// can replace combinations of VCVT (integer to floating-point) and VDIV
8263/// when the VDIV has a constant operand that is a power of 2.
8264///
8265/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8266/// vcvt.f32.s32 d16, d16
8267/// vdiv.f32 d16, d17, d16
8268/// becomes:
8269/// vcvt.f32.s32 d16, d16, #3
8270static SDValue PerformVDIVCombine(SDNode *N,
8271 TargetLowering::DAGCombinerInfo &DCI,
8272 const ARMSubtarget *Subtarget) {
8273 SelectionDAG &DAG = DCI.DAG;
8274 SDValue Op = N->getOperand(0);
8275 unsigned OpOpcode = Op.getNode()->getOpcode();
8276
8277 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
8278 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
8279 return SDValue();
8280
8281 uint64_t C;
8282 SDValue ConstVec = N->getOperand(1);
8283 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
8284
8285 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
8286 !isConstVecPow2(ConstVec, isSigned, C))
8287 return SDValue();
8288
Eric Christopherfa6f5912011-06-29 21:10:36 +00008289 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosieref01edf2011-06-24 19:23:04 +00008290 Intrinsic::arm_neon_vcvtfxu2fp;
8291 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, N->getDebugLoc(),
8292 Op.getValueType(),
Eric Christopherfa6f5912011-06-29 21:10:36 +00008293 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Chad Rosieref01edf2011-06-24 19:23:04 +00008294 Op.getOperand(0), DAG.getConstant(Log2_64(C), MVT::i32));
8295}
8296
8297/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson5bafff32009-06-22 23:27:02 +00008298/// operand of a vector shift operation, where all the elements of the
8299/// build_vector must have the same constant integer value.
8300static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
8301 // Ignore bit_converts.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008302 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson5bafff32009-06-22 23:27:02 +00008303 Op = Op.getOperand(0);
8304 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
8305 APInt SplatBits, SplatUndef;
8306 unsigned SplatBitSize;
8307 bool HasAnyUndefs;
8308 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
8309 HasAnyUndefs, ElementBits) ||
8310 SplatBitSize > ElementBits)
8311 return false;
8312 Cnt = SplatBits.getSExtValue();
8313 return true;
8314}
8315
8316/// isVShiftLImm - Check if this is a valid build_vector for the immediate
8317/// operand of a vector shift left operation. That value must be in the range:
8318/// 0 <= Value < ElementBits for a left shift; or
8319/// 0 <= Value <= ElementBits for a long left shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008320static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008321 assert(VT.isVector() && "vector shift count is not a vector type");
8322 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8323 if (! getVShiftImm(Op, ElementBits, Cnt))
8324 return false;
8325 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
8326}
8327
8328/// isVShiftRImm - Check if this is a valid build_vector for the immediate
8329/// operand of a vector shift right operation. For a shift opcode, the value
8330/// is positive, but for an intrinsic the value count must be negative. The
8331/// absolute value must be in the range:
8332/// 1 <= |Value| <= ElementBits for a right shift; or
8333/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Andersone50ed302009-08-10 22:56:29 +00008334static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson5bafff32009-06-22 23:27:02 +00008335 int64_t &Cnt) {
8336 assert(VT.isVector() && "vector shift count is not a vector type");
8337 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
8338 if (! getVShiftImm(Op, ElementBits, Cnt))
8339 return false;
8340 if (isIntrinsic)
8341 Cnt = -Cnt;
8342 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
8343}
8344
8345/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
8346static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
8347 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8348 switch (IntNo) {
8349 default:
8350 // Don't do anything for most intrinsics.
8351 break;
8352
8353 // Vector shifts: check for immediate versions and lower them.
8354 // Note: This is done during DAG combining instead of DAG legalizing because
8355 // the build_vectors for 64-bit vector element shift counts are generally
8356 // not legal, and it is hard to see their values after they get legalized to
8357 // loads from a constant pool.
8358 case Intrinsic::arm_neon_vshifts:
8359 case Intrinsic::arm_neon_vshiftu:
8360 case Intrinsic::arm_neon_vshiftls:
8361 case Intrinsic::arm_neon_vshiftlu:
8362 case Intrinsic::arm_neon_vshiftn:
8363 case Intrinsic::arm_neon_vrshifts:
8364 case Intrinsic::arm_neon_vrshiftu:
8365 case Intrinsic::arm_neon_vrshiftn:
8366 case Intrinsic::arm_neon_vqshifts:
8367 case Intrinsic::arm_neon_vqshiftu:
8368 case Intrinsic::arm_neon_vqshiftsu:
8369 case Intrinsic::arm_neon_vqshiftns:
8370 case Intrinsic::arm_neon_vqshiftnu:
8371 case Intrinsic::arm_neon_vqshiftnsu:
8372 case Intrinsic::arm_neon_vqrshiftns:
8373 case Intrinsic::arm_neon_vqrshiftnu:
8374 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Andersone50ed302009-08-10 22:56:29 +00008375 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008376 int64_t Cnt;
8377 unsigned VShiftOpc = 0;
8378
8379 switch (IntNo) {
8380 case Intrinsic::arm_neon_vshifts:
8381 case Intrinsic::arm_neon_vshiftu:
8382 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
8383 VShiftOpc = ARMISD::VSHL;
8384 break;
8385 }
8386 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
8387 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
8388 ARMISD::VSHRs : ARMISD::VSHRu);
8389 break;
8390 }
8391 return SDValue();
8392
8393 case Intrinsic::arm_neon_vshiftls:
8394 case Intrinsic::arm_neon_vshiftlu:
8395 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
8396 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008397 llvm_unreachable("invalid shift count for vshll intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008398
8399 case Intrinsic::arm_neon_vrshifts:
8400 case Intrinsic::arm_neon_vrshiftu:
8401 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
8402 break;
8403 return SDValue();
8404
8405 case Intrinsic::arm_neon_vqshifts:
8406 case Intrinsic::arm_neon_vqshiftu:
8407 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8408 break;
8409 return SDValue();
8410
8411 case Intrinsic::arm_neon_vqshiftsu:
8412 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
8413 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00008414 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008415
8416 case Intrinsic::arm_neon_vshiftn:
8417 case Intrinsic::arm_neon_vrshiftn:
8418 case Intrinsic::arm_neon_vqshiftns:
8419 case Intrinsic::arm_neon_vqshiftnu:
8420 case Intrinsic::arm_neon_vqshiftnsu:
8421 case Intrinsic::arm_neon_vqrshiftns:
8422 case Intrinsic::arm_neon_vqrshiftnu:
8423 case Intrinsic::arm_neon_vqrshiftnsu:
8424 // Narrowing shifts require an immediate right shift.
8425 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
8426 break;
Jim Grosbach18f30e62010-06-02 21:53:11 +00008427 llvm_unreachable("invalid shift count for narrowing vector shift "
8428 "intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008429
8430 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008431 llvm_unreachable("unhandled vector shift");
Bob Wilson5bafff32009-06-22 23:27:02 +00008432 }
8433
8434 switch (IntNo) {
8435 case Intrinsic::arm_neon_vshifts:
8436 case Intrinsic::arm_neon_vshiftu:
8437 // Opcode already set above.
8438 break;
8439 case Intrinsic::arm_neon_vshiftls:
8440 case Intrinsic::arm_neon_vshiftlu:
8441 if (Cnt == VT.getVectorElementType().getSizeInBits())
8442 VShiftOpc = ARMISD::VSHLLi;
8443 else
8444 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
8445 ARMISD::VSHLLs : ARMISD::VSHLLu);
8446 break;
8447 case Intrinsic::arm_neon_vshiftn:
8448 VShiftOpc = ARMISD::VSHRN; break;
8449 case Intrinsic::arm_neon_vrshifts:
8450 VShiftOpc = ARMISD::VRSHRs; break;
8451 case Intrinsic::arm_neon_vrshiftu:
8452 VShiftOpc = ARMISD::VRSHRu; break;
8453 case Intrinsic::arm_neon_vrshiftn:
8454 VShiftOpc = ARMISD::VRSHRN; break;
8455 case Intrinsic::arm_neon_vqshifts:
8456 VShiftOpc = ARMISD::VQSHLs; break;
8457 case Intrinsic::arm_neon_vqshiftu:
8458 VShiftOpc = ARMISD::VQSHLu; break;
8459 case Intrinsic::arm_neon_vqshiftsu:
8460 VShiftOpc = ARMISD::VQSHLsu; break;
8461 case Intrinsic::arm_neon_vqshiftns:
8462 VShiftOpc = ARMISD::VQSHRNs; break;
8463 case Intrinsic::arm_neon_vqshiftnu:
8464 VShiftOpc = ARMISD::VQSHRNu; break;
8465 case Intrinsic::arm_neon_vqshiftnsu:
8466 VShiftOpc = ARMISD::VQSHRNsu; break;
8467 case Intrinsic::arm_neon_vqrshiftns:
8468 VShiftOpc = ARMISD::VQRSHRNs; break;
8469 case Intrinsic::arm_neon_vqrshiftnu:
8470 VShiftOpc = ARMISD::VQRSHRNu; break;
8471 case Intrinsic::arm_neon_vqrshiftnsu:
8472 VShiftOpc = ARMISD::VQRSHRNsu; break;
8473 }
8474
8475 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008476 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008477 }
8478
8479 case Intrinsic::arm_neon_vshiftins: {
Owen Andersone50ed302009-08-10 22:56:29 +00008480 EVT VT = N->getOperand(1).getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008481 int64_t Cnt;
8482 unsigned VShiftOpc = 0;
8483
8484 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
8485 VShiftOpc = ARMISD::VSLI;
8486 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
8487 VShiftOpc = ARMISD::VSRI;
8488 else {
Torok Edwinc23197a2009-07-14 16:55:14 +00008489 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson5bafff32009-06-22 23:27:02 +00008490 }
8491
8492 return DAG.getNode(VShiftOpc, N->getDebugLoc(), N->getValueType(0),
8493 N->getOperand(1), N->getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008494 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008495 }
8496
8497 case Intrinsic::arm_neon_vqrshifts:
8498 case Intrinsic::arm_neon_vqrshiftu:
8499 // No immediate versions of these to check for.
8500 break;
8501 }
8502
8503 return SDValue();
8504}
8505
8506/// PerformShiftCombine - Checks for immediate versions of vector shifts and
8507/// lowers them. As with the vector shift intrinsics, this is done during DAG
8508/// combining instead of DAG legalizing because the build_vectors for 64-bit
8509/// vector element shift counts are generally not legal, and it is hard to see
8510/// their values after they get legalized to loads from a constant pool.
8511static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
8512 const ARMSubtarget *ST) {
Owen Andersone50ed302009-08-10 22:56:29 +00008513 EVT VT = N->getValueType(0);
Evan Cheng5fb468a2012-02-23 02:58:19 +00008514 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
8515 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
8516 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
8517 SDValue N1 = N->getOperand(1);
8518 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
8519 SDValue N0 = N->getOperand(0);
8520 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
8521 DAG.MaskedValueIsZero(N0.getOperand(0),
8522 APInt::getHighBitsSet(32, 16)))
8523 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, N0, N1);
8524 }
8525 }
Bob Wilson5bafff32009-06-22 23:27:02 +00008526
8527 // Nothing to be done for scalar shifts.
Tanya Lattner9684a7c2010-11-18 22:06:46 +00008528 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8529 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson5bafff32009-06-22 23:27:02 +00008530 return SDValue();
8531
8532 assert(ST->hasNEON() && "unexpected vector shift");
8533 int64_t Cnt;
8534
8535 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008536 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008537
8538 case ISD::SHL:
8539 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
8540 return DAG.getNode(ARMISD::VSHL, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008541 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008542 break;
8543
8544 case ISD::SRA:
8545 case ISD::SRL:
8546 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
8547 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
8548 ARMISD::VSHRs : ARMISD::VSHRu);
8549 return DAG.getNode(VShiftOpc, N->getDebugLoc(), VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00008550 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson5bafff32009-06-22 23:27:02 +00008551 }
8552 }
8553 return SDValue();
8554}
8555
8556/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
8557/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
8558static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
8559 const ARMSubtarget *ST) {
8560 SDValue N0 = N->getOperand(0);
8561
8562 // Check for sign- and zero-extensions of vector extract operations of 8-
8563 // and 16-bit vector elements. NEON supports these directly. They are
8564 // handled during DAG combining because type legalization will promote them
8565 // to 32-bit types and it is messy to recognize the operations after that.
8566 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
8567 SDValue Vec = N0.getOperand(0);
8568 SDValue Lane = N0.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008569 EVT VT = N->getValueType(0);
8570 EVT EltVT = N0.getValueType();
Bob Wilson5bafff32009-06-22 23:27:02 +00008571 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8572
Owen Anderson825b72b2009-08-11 20:47:22 +00008573 if (VT == MVT::i32 &&
8574 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilson3468c2e2010-11-03 16:24:50 +00008575 TLI.isTypeLegal(Vec.getValueType()) &&
8576 isa<ConstantSDNode>(Lane)) {
Bob Wilson5bafff32009-06-22 23:27:02 +00008577
8578 unsigned Opc = 0;
8579 switch (N->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008580 default: llvm_unreachable("unexpected opcode");
Bob Wilson5bafff32009-06-22 23:27:02 +00008581 case ISD::SIGN_EXTEND:
8582 Opc = ARMISD::VGETLANEs;
8583 break;
8584 case ISD::ZERO_EXTEND:
8585 case ISD::ANY_EXTEND:
8586 Opc = ARMISD::VGETLANEu;
8587 break;
8588 }
8589 return DAG.getNode(Opc, N->getDebugLoc(), VT, Vec, Lane);
8590 }
8591 }
8592
8593 return SDValue();
8594}
8595
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008596/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
8597/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
8598static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
8599 const ARMSubtarget *ST) {
8600 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng60108e92010-07-15 22:07:12 +00008601 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008602 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
8603 // a NaN; only do the transformation when it matches that behavior.
8604
8605 // For now only do this when using NEON for FP operations; if using VFP, it
8606 // is not obvious that the benefit outweighs the cost of switching to the
8607 // NEON pipeline.
8608 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
8609 N->getValueType(0) != MVT::f32)
8610 return SDValue();
8611
8612 SDValue CondLHS = N->getOperand(0);
8613 SDValue CondRHS = N->getOperand(1);
8614 SDValue LHS = N->getOperand(2);
8615 SDValue RHS = N->getOperand(3);
8616 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
8617
8618 unsigned Opcode = 0;
8619 bool IsReversed;
Bob Wilsone742bb52010-02-24 22:15:53 +00008620 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008621 IsReversed = false; // x CC y ? x : y
Bob Wilsone742bb52010-02-24 22:15:53 +00008622 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008623 IsReversed = true ; // x CC y ? y : x
8624 } else {
8625 return SDValue();
8626 }
8627
Bob Wilsone742bb52010-02-24 22:15:53 +00008628 bool IsUnordered;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008629 switch (CC) {
8630 default: break;
8631 case ISD::SETOLT:
8632 case ISD::SETOLE:
8633 case ISD::SETLT:
8634 case ISD::SETLE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008635 case ISD::SETULT:
8636 case ISD::SETULE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008637 // If LHS is NaN, an ordered comparison will be false and the result will
8638 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
8639 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8640 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
8641 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8642 break;
8643 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
8644 // will return -0, so vmin can only be used for unsafe math or if one of
8645 // the operands is known to be nonzero.
8646 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008647 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008648 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8649 break;
8650 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008651 break;
8652
8653 case ISD::SETOGT:
8654 case ISD::SETOGE:
8655 case ISD::SETGT:
8656 case ISD::SETGE:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008657 case ISD::SETUGT:
8658 case ISD::SETUGE:
Bob Wilsone742bb52010-02-24 22:15:53 +00008659 // If LHS is NaN, an ordered comparison will be false and the result will
8660 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
8661 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
8662 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
8663 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
8664 break;
8665 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
8666 // will return +0, so vmax can only be used for unsafe math or if one of
8667 // the operands is known to be nonzero.
8668 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky8a8d4792011-12-02 22:16:29 +00008669 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsone742bb52010-02-24 22:15:53 +00008670 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8671 break;
8672 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008673 break;
8674 }
8675
8676 if (!Opcode)
8677 return SDValue();
8678 return DAG.getNode(Opcode, N->getDebugLoc(), N->getValueType(0), LHS, RHS);
8679}
8680
Evan Chenge721f5c2011-07-13 00:42:17 +00008681/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
8682SDValue
8683ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
8684 SDValue Cmp = N->getOperand(4);
8685 if (Cmp.getOpcode() != ARMISD::CMPZ)
8686 // Only looking at EQ and NE cases.
8687 return SDValue();
8688
8689 EVT VT = N->getValueType(0);
8690 DebugLoc dl = N->getDebugLoc();
8691 SDValue LHS = Cmp.getOperand(0);
8692 SDValue RHS = Cmp.getOperand(1);
8693 SDValue FalseVal = N->getOperand(0);
8694 SDValue TrueVal = N->getOperand(1);
8695 SDValue ARMcc = N->getOperand(2);
Jim Grosbachb04546f2011-09-13 20:30:37 +00008696 ARMCC::CondCodes CC =
8697 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chenge721f5c2011-07-13 00:42:17 +00008698
8699 // Simplify
8700 // mov r1, r0
8701 // cmp r1, x
8702 // mov r0, y
8703 // moveq r0, x
8704 // to
8705 // cmp r0, x
8706 // movne r0, y
8707 //
8708 // mov r1, r0
8709 // cmp r1, x
8710 // mov r0, x
8711 // movne r0, y
8712 // to
8713 // cmp r0, x
8714 // movne r0, y
8715 /// FIXME: Turn this into a target neutral optimization?
8716 SDValue Res;
Evan Cheng9b88d2d2011-09-28 23:16:31 +00008717 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chenge721f5c2011-07-13 00:42:17 +00008718 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
8719 N->getOperand(3), Cmp);
8720 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
8721 SDValue ARMcc;
8722 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
8723 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
8724 N->getOperand(3), NewCmp);
8725 }
8726
8727 if (Res.getNode()) {
8728 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008729 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chenge721f5c2011-07-13 00:42:17 +00008730 // Capture demanded bits information that would be otherwise lost.
8731 if (KnownZero == 0xfffffffe)
8732 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8733 DAG.getValueType(MVT::i1));
8734 else if (KnownZero == 0xffffff00)
8735 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8736 DAG.getValueType(MVT::i8));
8737 else if (KnownZero == 0xffff0000)
8738 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
8739 DAG.getValueType(MVT::i16));
8740 }
8741
8742 return Res;
8743}
8744
Dan Gohman475871a2008-07-27 21:46:04 +00008745SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson2dc4f542009-03-20 22:42:55 +00008746 DAGCombinerInfo &DCI) const {
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008747 switch (N->getOpcode()) {
8748 default: break;
Tanya Lattner189531f2011-06-14 23:48:48 +00008749 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008750 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikova9790d72010-05-15 18:16:59 +00008751 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach469bbdb2010-07-16 23:05:05 +00008752 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chengc892aeb2012-02-23 01:19:06 +00008753 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
8754 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Cheng0c1aec12010-12-14 03:22:07 +00008755 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbache5165492009-11-09 00:11:35 +00008756 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson0b8ccb82010-09-22 22:09:21 +00008757 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson31600902010-12-21 06:43:19 +00008758 case ISD::STORE: return PerformSTORECombine(N, DCI);
8759 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
8760 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonf20700c2010-10-27 20:38:28 +00008761 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilsonb1dfa7a2010-11-28 06:51:26 +00008762 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosieref01edf2011-06-24 19:23:04 +00008763 case ISD::FP_TO_SINT:
8764 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
8765 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008766 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson5bafff32009-06-22 23:27:02 +00008767 case ISD::SHL:
8768 case ISD::SRA:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008769 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson5bafff32009-06-22 23:27:02 +00008770 case ISD::SIGN_EXTEND:
8771 case ISD::ZERO_EXTEND:
Bob Wilson9f6c4c12010-02-18 06:05:53 +00008772 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
8773 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chenge721f5c2011-07-13 00:42:17 +00008774 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson1c3ef902011-02-07 17:43:21 +00008775 case ARMISD::VLD2DUP:
8776 case ARMISD::VLD3DUP:
8777 case ARMISD::VLD4DUP:
8778 return CombineBaseUpdate(N, DCI);
8779 case ISD::INTRINSIC_VOID:
8780 case ISD::INTRINSIC_W_CHAIN:
8781 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
8782 case Intrinsic::arm_neon_vld1:
8783 case Intrinsic::arm_neon_vld2:
8784 case Intrinsic::arm_neon_vld3:
8785 case Intrinsic::arm_neon_vld4:
8786 case Intrinsic::arm_neon_vld2lane:
8787 case Intrinsic::arm_neon_vld3lane:
8788 case Intrinsic::arm_neon_vld4lane:
8789 case Intrinsic::arm_neon_vst1:
8790 case Intrinsic::arm_neon_vst2:
8791 case Intrinsic::arm_neon_vst3:
8792 case Intrinsic::arm_neon_vst4:
8793 case Intrinsic::arm_neon_vst2lane:
8794 case Intrinsic::arm_neon_vst3lane:
8795 case Intrinsic::arm_neon_vst4lane:
8796 return CombineBaseUpdate(N, DCI);
8797 default: break;
8798 }
8799 break;
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008800 }
Dan Gohman475871a2008-07-27 21:46:04 +00008801 return SDValue();
Chris Lattnerf1b1c5e2007-11-27 22:36:16 +00008802}
8803
Evan Cheng31959b12011-02-02 01:06:55 +00008804bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
8805 EVT VT) const {
8806 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
8807}
8808
Bill Wendlingaf566342009-08-15 21:21:19 +00008809bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Bob Wilson02aba732010-09-28 04:09:35 +00008810 if (!Subtarget->allowsUnalignedMem())
Bob Wilson86fe66d2010-06-25 04:12:31 +00008811 return false;
Bill Wendlingaf566342009-08-15 21:21:19 +00008812
8813 switch (VT.getSimpleVT().SimpleTy) {
8814 default:
8815 return false;
8816 case MVT::i8:
8817 case MVT::i16:
8818 case MVT::i32:
8819 return true;
Evan Chenga99c5082012-08-15 17:44:53 +00008820 case MVT::f64:
8821 return Subtarget->hasNEON();
Bill Wendlingaf566342009-08-15 21:21:19 +00008822 // FIXME: VLD1 etc with standard alignment is legal.
8823 }
8824}
8825
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008826static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
8827 unsigned AlignCheck) {
8828 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
8829 (DstAlign == 0 || DstAlign % AlignCheck == 0));
8830}
8831
8832EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
8833 unsigned DstAlign, unsigned SrcAlign,
Lang Hamesa1e78882011-11-02 23:37:04 +00008834 bool IsZeroVal,
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008835 bool MemcpyStrSrc,
8836 MachineFunction &MF) const {
8837 const Function *F = MF.getFunction();
8838
8839 // See if we can use NEON instructions for this...
Lang Hamesa1e78882011-11-02 23:37:04 +00008840 if (IsZeroVal &&
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008841 !F->hasFnAttr(Attribute::NoImplicitFloat) &&
8842 Subtarget->hasNEON()) {
8843 if (memOpAlign(SrcAlign, DstAlign, 16) && Size >= 16) {
8844 return MVT::v4i32;
8845 } else if (memOpAlign(SrcAlign, DstAlign, 8) && Size >= 8) {
8846 return MVT::v2i32;
8847 }
8848 }
8849
Lang Hames5207bf22011-11-08 18:56:23 +00008850 // Lowering to i32/i16 if the size permits.
8851 if (Size >= 4) {
8852 return MVT::i32;
8853 } else if (Size >= 2) {
8854 return MVT::i16;
8855 }
8856
Lang Hames1a1d1fc2011-11-02 22:52:45 +00008857 // Let the target-independent logic figure it out.
8858 return MVT::Other;
8859}
8860
Evan Chenge6c835f2009-08-14 20:09:37 +00008861static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
8862 if (V < 0)
8863 return false;
8864
8865 unsigned Scale = 1;
8866 switch (VT.getSimpleVT().SimpleTy) {
8867 default: return false;
8868 case MVT::i1:
8869 case MVT::i8:
8870 // Scale == 1;
8871 break;
8872 case MVT::i16:
8873 // Scale == 2;
8874 Scale = 2;
8875 break;
8876 case MVT::i32:
8877 // Scale == 4;
8878 Scale = 4;
8879 break;
8880 }
8881
8882 if ((V & (Scale - 1)) != 0)
8883 return false;
8884 V /= Scale;
8885 return V == (V & ((1LL << 5) - 1));
8886}
8887
8888static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
8889 const ARMSubtarget *Subtarget) {
8890 bool isNeg = false;
8891 if (V < 0) {
8892 isNeg = true;
8893 V = - V;
8894 }
8895
8896 switch (VT.getSimpleVT().SimpleTy) {
8897 default: return false;
8898 case MVT::i1:
8899 case MVT::i8:
8900 case MVT::i16:
8901 case MVT::i32:
8902 // + imm12 or - imm8
8903 if (isNeg)
8904 return V == (V & ((1LL << 8) - 1));
8905 return V == (V & ((1LL << 12) - 1));
8906 case MVT::f32:
8907 case MVT::f64:
8908 // Same as ARM mode. FIXME: NEON?
8909 if (!Subtarget->hasVFP2())
8910 return false;
8911 if ((V & 3) != 0)
8912 return false;
8913 V >>= 2;
8914 return V == (V & ((1LL << 8) - 1));
8915 }
8916}
8917
Evan Chengb01fad62007-03-12 23:30:29 +00008918/// isLegalAddressImmediate - Return true if the integer value can be used
8919/// as the offset of the target addressing mode for load / store of the
8920/// given type.
Owen Andersone50ed302009-08-10 22:56:29 +00008921static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattner37caf8c2007-04-09 23:33:39 +00008922 const ARMSubtarget *Subtarget) {
Evan Cheng961f8792007-03-13 20:37:59 +00008923 if (V == 0)
8924 return true;
8925
Evan Cheng65011532009-03-09 19:15:00 +00008926 if (!VT.isSimple())
8927 return false;
8928
Evan Chenge6c835f2009-08-14 20:09:37 +00008929 if (Subtarget->isThumb1Only())
8930 return isLegalT1AddressImmediate(V, VT);
8931 else if (Subtarget->isThumb2())
8932 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Chengb01fad62007-03-12 23:30:29 +00008933
Evan Chenge6c835f2009-08-14 20:09:37 +00008934 // ARM mode.
Evan Chengb01fad62007-03-12 23:30:29 +00008935 if (V < 0)
8936 V = - V;
Owen Anderson825b72b2009-08-11 20:47:22 +00008937 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengb01fad62007-03-12 23:30:29 +00008938 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00008939 case MVT::i1:
8940 case MVT::i8:
8941 case MVT::i32:
Evan Chengb01fad62007-03-12 23:30:29 +00008942 // +- imm12
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008943 return V == (V & ((1LL << 12) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008944 case MVT::i16:
Evan Chengb01fad62007-03-12 23:30:29 +00008945 // +- imm8
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008946 return V == (V & ((1LL << 8) - 1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008947 case MVT::f32:
8948 case MVT::f64:
Evan Chenge6c835f2009-08-14 20:09:37 +00008949 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Chengb01fad62007-03-12 23:30:29 +00008950 return false;
Evan Cheng0b0a9a92007-05-03 02:00:18 +00008951 if ((V & 3) != 0)
Evan Chengb01fad62007-03-12 23:30:29 +00008952 return false;
8953 V >>= 2;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00008954 return V == (V & ((1LL << 8) - 1));
Evan Chengb01fad62007-03-12 23:30:29 +00008955 }
Evan Chenga8e29892007-01-19 07:51:42 +00008956}
8957
Evan Chenge6c835f2009-08-14 20:09:37 +00008958bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
8959 EVT VT) const {
8960 int Scale = AM.Scale;
8961 if (Scale < 0)
8962 return false;
8963
8964 switch (VT.getSimpleVT().SimpleTy) {
8965 default: return false;
8966 case MVT::i1:
8967 case MVT::i8:
8968 case MVT::i16:
8969 case MVT::i32:
8970 if (Scale == 1)
8971 return true;
8972 // r + r << imm
8973 Scale = Scale & ~1;
8974 return Scale == 2 || Scale == 4 || Scale == 8;
8975 case MVT::i64:
8976 // r + r
8977 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
8978 return true;
8979 return false;
8980 case MVT::isVoid:
8981 // Note, we allow "void" uses (basically, uses that aren't loads or
8982 // stores), because arm allows folding a scale into many arithmetic
8983 // operations. This should be made more precise and revisited later.
8984
8985 // Allow r << imm, but the imm has to be a multiple of two.
8986 if (Scale & 1) return false;
8987 return isPowerOf2_32(Scale);
8988 }
8989}
8990
Chris Lattner37caf8c2007-04-09 23:33:39 +00008991/// isLegalAddressingMode - Return true if the addressing mode represented
8992/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson2dc4f542009-03-20 22:42:55 +00008993bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008994 Type *Ty) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008995 EVT VT = getValueType(Ty, true);
Bob Wilson2c7dab12009-04-08 17:55:28 +00008996 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Chengb01fad62007-03-12 23:30:29 +00008997 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00008998
Chris Lattner37caf8c2007-04-09 23:33:39 +00008999 // Can never fold addr of global into load/store.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009000 if (AM.BaseGV)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009001 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009002
Chris Lattner37caf8c2007-04-09 23:33:39 +00009003 switch (AM.Scale) {
9004 case 0: // no scale reg, must be "r+i" or "r", or "i".
9005 break;
9006 case 1:
Evan Chenge6c835f2009-08-14 20:09:37 +00009007 if (Subtarget->isThumb1Only())
Chris Lattner37caf8c2007-04-09 23:33:39 +00009008 return false;
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009009 // FALL THROUGH.
Chris Lattner37caf8c2007-04-09 23:33:39 +00009010 default:
Chris Lattner5a3d40d2007-04-13 06:50:55 +00009011 // ARM doesn't support any R+R*scale+imm addr modes.
9012 if (AM.BaseOffs)
9013 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009014
Bob Wilson2c7dab12009-04-08 17:55:28 +00009015 if (!VT.isSimple())
9016 return false;
9017
Evan Chenge6c835f2009-08-14 20:09:37 +00009018 if (Subtarget->isThumb2())
9019 return isLegalT2ScaledAddressingMode(AM, VT);
9020
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009021 int Scale = AM.Scale;
Owen Anderson825b72b2009-08-11 20:47:22 +00009022 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner37caf8c2007-04-09 23:33:39 +00009023 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00009024 case MVT::i1:
9025 case MVT::i8:
9026 case MVT::i32:
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009027 if (Scale < 0) Scale = -Scale;
9028 if (Scale == 1)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009029 return true;
9030 // r + r << imm
Chris Lattnere1152942007-04-11 16:17:12 +00009031 return isPowerOf2_32(Scale & ~1);
Owen Anderson825b72b2009-08-11 20:47:22 +00009032 case MVT::i16:
Evan Chenge6c835f2009-08-14 20:09:37 +00009033 case MVT::i64:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009034 // r + r
Chris Lattnereb13d1b2007-04-10 03:48:29 +00009035 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattner37caf8c2007-04-09 23:33:39 +00009036 return true;
Chris Lattnere1152942007-04-11 16:17:12 +00009037 return false;
Bob Wilson2dc4f542009-03-20 22:42:55 +00009038
Owen Anderson825b72b2009-08-11 20:47:22 +00009039 case MVT::isVoid:
Chris Lattner37caf8c2007-04-09 23:33:39 +00009040 // Note, we allow "void" uses (basically, uses that aren't loads or
9041 // stores), because arm allows folding a scale into many arithmetic
9042 // operations. This should be made more precise and revisited later.
Bob Wilson2dc4f542009-03-20 22:42:55 +00009043
Chris Lattner37caf8c2007-04-09 23:33:39 +00009044 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chenge6c835f2009-08-14 20:09:37 +00009045 if (Scale & 1) return false;
9046 return isPowerOf2_32(Scale);
Chris Lattner37caf8c2007-04-09 23:33:39 +00009047 }
Evan Chengb01fad62007-03-12 23:30:29 +00009048 }
Chris Lattner37caf8c2007-04-09 23:33:39 +00009049 return true;
Evan Chengb01fad62007-03-12 23:30:29 +00009050}
9051
Evan Cheng77e47512009-11-11 19:05:52 +00009052/// isLegalICmpImmediate - Return true if the specified immediate is legal
9053/// icmp immediate, that is the target has icmp instructions which can compare
9054/// a register against the immediate without having to materialize the
9055/// immediate into a register.
Evan Cheng06b53c02009-11-12 07:13:11 +00009056bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009057 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng77e47512009-11-11 19:05:52 +00009058 if (!Subtarget->isThumb())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009059 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng77e47512009-11-11 19:05:52 +00009060 if (Subtarget->isThumb2())
Chandler Carruthba4d4572012-04-06 20:10:52 +00009061 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen70fbea72012-04-06 17:45:04 +00009062 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng06b53c02009-11-12 07:13:11 +00009063 return Imm >= 0 && Imm <= 255;
Evan Cheng77e47512009-11-11 19:05:52 +00009064}
9065
Andrew Trick8d8d9612012-07-18 18:34:27 +00009066/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9067/// *or sub* immediate, that is the target has add or sub instructions which can
9068/// add a register with the immediate without having to materialize the
Dan Gohmancca82142011-05-03 00:46:49 +00009069/// immediate into a register.
9070bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Trick8d8d9612012-07-18 18:34:27 +00009071 // Same encoding for add/sub, just flip the sign.
9072 int64_t AbsImm = llvm::abs64(Imm);
9073 if (!Subtarget->isThumb())
9074 return ARM_AM::getSOImmVal(AbsImm) != -1;
9075 if (Subtarget->isThumb2())
9076 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9077 // Thumb1 only has 8-bit unsigned immediate.
9078 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohmancca82142011-05-03 00:46:49 +00009079}
9080
Owen Andersone50ed302009-08-10 22:56:29 +00009081static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009082 bool isSEXTLoad, SDValue &Base,
9083 SDValue &Offset, bool &isInc,
9084 SelectionDAG &DAG) {
Evan Chenga8e29892007-01-19 07:51:42 +00009085 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9086 return false;
9087
Owen Anderson825b72b2009-08-11 20:47:22 +00009088 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Chenga8e29892007-01-19 07:51:42 +00009089 // AddressingMode 3
9090 Base = Ptr->getOperand(0);
9091 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009092 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009093 if (RHSC < 0 && RHSC > -256) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009094 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009095 isInc = false;
9096 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9097 return true;
9098 }
9099 }
9100 isInc = (Ptr->getOpcode() == ISD::ADD);
9101 Offset = Ptr->getOperand(1);
9102 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +00009103 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Chenga8e29892007-01-19 07:51:42 +00009104 // AddressingMode 2
9105 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009106 int RHSC = (int)RHS->getZExtValue();
Evan Chenga8e29892007-01-19 07:51:42 +00009107 if (RHSC < 0 && RHSC > -0x1000) {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009108 assert(Ptr->getOpcode() == ISD::ADD);
Evan Chenga8e29892007-01-19 07:51:42 +00009109 isInc = false;
9110 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9111 Base = Ptr->getOperand(0);
9112 return true;
9113 }
9114 }
9115
9116 if (Ptr->getOpcode() == ISD::ADD) {
9117 isInc = true;
Evan Chengee04a6d2011-07-20 23:34:39 +00009118 ARM_AM::ShiftOpc ShOpcVal=
9119 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Chenga8e29892007-01-19 07:51:42 +00009120 if (ShOpcVal != ARM_AM::no_shift) {
9121 Base = Ptr->getOperand(1);
9122 Offset = Ptr->getOperand(0);
9123 } else {
9124 Base = Ptr->getOperand(0);
9125 Offset = Ptr->getOperand(1);
9126 }
9127 return true;
9128 }
9129
9130 isInc = (Ptr->getOpcode() == ISD::ADD);
9131 Base = Ptr->getOperand(0);
9132 Offset = Ptr->getOperand(1);
9133 return true;
9134 }
9135
Jim Grosbache5165492009-11-09 00:11:35 +00009136 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Chenga8e29892007-01-19 07:51:42 +00009137 return false;
9138}
9139
Owen Andersone50ed302009-08-10 22:56:29 +00009140static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Chenge88d5ce2009-07-02 07:28:31 +00009141 bool isSEXTLoad, SDValue &Base,
9142 SDValue &Offset, bool &isInc,
9143 SelectionDAG &DAG) {
9144 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9145 return false;
9146
9147 Base = Ptr->getOperand(0);
9148 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9149 int RHSC = (int)RHS->getZExtValue();
9150 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9151 assert(Ptr->getOpcode() == ISD::ADD);
9152 isInc = false;
9153 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9154 return true;
9155 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9156 isInc = Ptr->getOpcode() == ISD::ADD;
9157 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9158 return true;
9159 }
9160 }
9161
9162 return false;
9163}
9164
Evan Chenga8e29892007-01-19 07:51:42 +00009165/// getPreIndexedAddressParts - returns true by value, base pointer and
9166/// offset pointer and addressing mode by reference if the node's address
9167/// can be legally represented as pre-indexed load / store address.
9168bool
Dan Gohman475871a2008-07-27 21:46:04 +00009169ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9170 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009171 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009172 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009173 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009174 return false;
9175
Owen Andersone50ed302009-08-10 22:56:29 +00009176 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009177 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009178 bool isSEXTLoad = false;
9179 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
9180 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009181 VT = LD->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009182 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9183 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
9184 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009185 VT = ST->getMemoryVT();
Evan Chenga8e29892007-01-19 07:51:42 +00009186 } else
9187 return false;
9188
9189 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009190 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009191 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009192 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
9193 Offset, isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009194 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009195 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng04129572009-07-02 06:44:30 +00009196 Offset, isInc, DAG);
Evan Chenge88d5ce2009-07-02 07:28:31 +00009197 if (!isLegal)
9198 return false;
9199
9200 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
9201 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009202}
9203
9204/// getPostIndexedAddressParts - returns true by value, base pointer and
9205/// offset pointer and addressing mode by reference if this node can be
9206/// combined with a load / store to form a post-indexed load / store.
9207bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman475871a2008-07-27 21:46:04 +00009208 SDValue &Base,
9209 SDValue &Offset,
Evan Chenga8e29892007-01-19 07:51:42 +00009210 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00009211 SelectionDAG &DAG) const {
Evan Chenge88d5ce2009-07-02 07:28:31 +00009212 if (Subtarget->isThumb1Only())
Evan Chenga8e29892007-01-19 07:51:42 +00009213 return false;
9214
Owen Andersone50ed302009-08-10 22:56:29 +00009215 EVT VT;
Dan Gohman475871a2008-07-27 21:46:04 +00009216 SDValue Ptr;
Evan Chenga8e29892007-01-19 07:51:42 +00009217 bool isSEXTLoad = false;
9218 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009219 VT = LD->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009220 Ptr = LD->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009221 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
9222 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009223 VT = ST->getMemoryVT();
Evan Cheng28dad2a2010-05-18 21:31:17 +00009224 Ptr = ST->getBasePtr();
Evan Chenga8e29892007-01-19 07:51:42 +00009225 } else
9226 return false;
9227
9228 bool isInc;
Evan Chenge88d5ce2009-07-02 07:28:31 +00009229 bool isLegal = false;
Evan Chenge6c835f2009-08-14 20:09:37 +00009230 if (Subtarget->isThumb2())
Evan Chenge88d5ce2009-07-02 07:28:31 +00009231 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Cheng28dad2a2010-05-18 21:31:17 +00009232 isInc, DAG);
Jim Grosbach764ab522009-08-11 15:33:49 +00009233 else
Evan Chenge88d5ce2009-07-02 07:28:31 +00009234 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
9235 isInc, DAG);
9236 if (!isLegal)
9237 return false;
9238
Evan Cheng28dad2a2010-05-18 21:31:17 +00009239 if (Ptr != Base) {
9240 // Swap base ptr and offset to catch more post-index load / store when
9241 // it's legal. In Thumb2 mode, offset must be an immediate.
9242 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
9243 !Subtarget->isThumb2())
9244 std::swap(Base, Offset);
9245
9246 // Post-indexed load / store update the base pointer.
9247 if (Ptr != Base)
9248 return false;
9249 }
9250
Evan Chenge88d5ce2009-07-02 07:28:31 +00009251 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
9252 return true;
Evan Chenga8e29892007-01-19 07:51:42 +00009253}
9254
Dan Gohman475871a2008-07-27 21:46:04 +00009255void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Bob Wilson2dc4f542009-03-20 22:42:55 +00009256 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009257 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00009258 const SelectionDAG &DAG,
Evan Chenga8e29892007-01-19 07:51:42 +00009259 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009260 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
Evan Chenga8e29892007-01-19 07:51:42 +00009261 switch (Op.getOpcode()) {
9262 default: break;
9263 case ARMISD::CMOV: {
9264 // Bits are known zero/one if known on the LHS and RHS.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009265 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009266 if (KnownZero == 0 && KnownOne == 0) return;
9267
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00009268 APInt KnownZeroRHS, KnownOneRHS;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009269 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Chenga8e29892007-01-19 07:51:42 +00009270 KnownZero &= KnownZeroRHS;
9271 KnownOne &= KnownOneRHS;
9272 return;
9273 }
9274 }
9275}
9276
9277//===----------------------------------------------------------------------===//
9278// ARM Inline Assembly Support
9279//===----------------------------------------------------------------------===//
9280
Evan Cheng55d42002011-01-08 01:24:27 +00009281bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
9282 // Looking for "rev" which is V6+.
9283 if (!Subtarget->hasV6Ops())
9284 return false;
9285
9286 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9287 std::string AsmStr = IA->getAsmString();
9288 SmallVector<StringRef, 4> AsmPieces;
9289 SplitString(AsmStr, AsmPieces, ";\n");
9290
9291 switch (AsmPieces.size()) {
9292 default: return false;
9293 case 1:
9294 AsmStr = AsmPieces[0];
9295 AsmPieces.clear();
9296 SplitString(AsmStr, AsmPieces, " \t,");
9297
9298 // rev $0, $1
9299 if (AsmPieces.size() == 3 &&
9300 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
9301 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009302 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng55d42002011-01-08 01:24:27 +00009303 if (Ty && Ty->getBitWidth() == 32)
9304 return IntrinsicLowering::LowerToByteSwap(CI);
9305 }
9306 break;
9307 }
9308
9309 return false;
9310}
9311
Evan Chenga8e29892007-01-19 07:51:42 +00009312/// getConstraintType - Given a constraint letter, return the type of
9313/// constraint it is for this target.
9314ARMTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009315ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
9316 if (Constraint.size() == 1) {
9317 switch (Constraint[0]) {
9318 default: break;
9319 case 'l': return C_RegisterClass;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009320 case 'w': return C_RegisterClass;
Eric Christopher73744df2011-06-30 23:23:01 +00009321 case 'h': return C_RegisterClass;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009322 case 'x': return C_RegisterClass;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009323 case 't': return C_RegisterClass;
Eric Christopher5e653c92011-07-01 01:00:07 +00009324 case 'j': return C_Other; // Constant for movw.
Eric Christopheref7f1e72011-07-29 21:18:58 +00009325 // An address with a single base register. Due to the way we
9326 // currently handle addresses it is the same as an 'r' memory constraint.
9327 case 'Q': return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00009328 }
Eric Christopher1312ca82011-06-21 22:10:57 +00009329 } else if (Constraint.size() == 2) {
9330 switch (Constraint[0]) {
9331 default: break;
9332 // All 'U+' constraints are addresses.
9333 case 'U': return C_Memory;
9334 }
Evan Chenga8e29892007-01-19 07:51:42 +00009335 }
Chris Lattner4234f572007-03-25 02:14:49 +00009336 return TargetLowering::getConstraintType(Constraint);
Evan Chenga8e29892007-01-19 07:51:42 +00009337}
9338
John Thompson44ab89e2010-10-29 17:29:13 +00009339/// Examine constraint type and operand type and determine a weight value.
9340/// This object must already have been set up with the operand type
9341/// and the current alternative constraint selected.
9342TargetLowering::ConstraintWeight
9343ARMTargetLowering::getSingleConstraintMatchWeight(
9344 AsmOperandInfo &info, const char *constraint) const {
9345 ConstraintWeight weight = CW_Invalid;
9346 Value *CallOperandVal = info.CallOperandVal;
9347 // If we don't have a value, we can't do a match,
9348 // but allow it at the lowest weight.
9349 if (CallOperandVal == NULL)
9350 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009351 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00009352 // Look at the constraint type.
9353 switch (*constraint) {
9354 default:
9355 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
9356 break;
9357 case 'l':
9358 if (type->isIntegerTy()) {
9359 if (Subtarget->isThumb())
9360 weight = CW_SpecificReg;
9361 else
9362 weight = CW_Register;
9363 }
9364 break;
9365 case 'w':
9366 if (type->isFloatingPointTy())
9367 weight = CW_Register;
9368 break;
9369 }
9370 return weight;
9371}
9372
Eric Christopher35e6d4d2011-06-30 23:50:52 +00009373typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
9374RCPair
Evan Chenga8e29892007-01-19 07:51:42 +00009375ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009376 EVT VT) const {
Evan Chenga8e29892007-01-19 07:51:42 +00009377 if (Constraint.size() == 1) {
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009378 // GCC ARM Constraint Letters
Evan Chenga8e29892007-01-19 07:51:42 +00009379 switch (Constraint[0]) {
Eric Christopher73744df2011-06-30 23:23:01 +00009380 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen09bf0032010-01-14 18:19:56 +00009381 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009382 return RCPair(0U, &ARM::tGPRRegClass);
9383 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopher73744df2011-06-30 23:23:01 +00009384 case 'h': // High regs or no regs.
9385 if (Subtarget->isThumb())
Craig Topper420761a2012-04-20 07:30:17 +00009386 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopher1070f822011-07-01 00:19:27 +00009387 break;
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009388 case 'r':
Craig Topper420761a2012-04-20 07:30:17 +00009389 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009390 case 'w':
Owen Anderson825b72b2009-08-11 20:47:22 +00009391 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009392 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson5afffae2009-12-18 01:03:29 +00009393 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009394 return RCPair(0U, &ARM::DPRRegClass);
Evan Chengd831cda2009-12-08 23:06:22 +00009395 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009396 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattnerc4e3f8e2007-04-02 17:24:08 +00009397 break;
Eric Christopher89bd71f2011-07-01 00:14:47 +00009398 case 'x':
9399 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009400 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009401 if (VT.getSizeInBits() == 64)
Craig Topper420761a2012-04-20 07:30:17 +00009402 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009403 if (VT.getSizeInBits() == 128)
Craig Topper420761a2012-04-20 07:30:17 +00009404 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopher89bd71f2011-07-01 00:14:47 +00009405 break;
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009406 case 't':
9407 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00009408 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherd5dc9ec2011-07-01 00:30:46 +00009409 break;
Evan Chenga8e29892007-01-19 07:51:42 +00009410 }
9411 }
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009412 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topper420761a2012-04-20 07:30:17 +00009413 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson33cc5cb2010-03-15 23:09:18 +00009414
Evan Chenga8e29892007-01-19 07:51:42 +00009415 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9416}
9417
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009418/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9419/// vector. If it is invalid, don't add anything to Ops.
9420void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00009421 std::string &Constraint,
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009422 std::vector<SDValue>&Ops,
9423 SelectionDAG &DAG) const {
9424 SDValue Result(0, 0);
9425
Eric Christopher100c8332011-06-02 23:16:42 +00009426 // Currently only support length 1 constraints.
9427 if (Constraint.length() != 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00009428
Eric Christopher100c8332011-06-02 23:16:42 +00009429 char ConstraintLetter = Constraint[0];
9430 switch (ConstraintLetter) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009431 default: break;
Eric Christopher5e653c92011-07-01 01:00:07 +00009432 case 'j':
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009433 case 'I': case 'J': case 'K': case 'L':
9434 case 'M': case 'N': case 'O':
9435 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
9436 if (!C)
9437 return;
9438
9439 int64_t CVal64 = C->getSExtValue();
9440 int CVal = (int) CVal64;
9441 // None of these constraints allow values larger than 32 bits. Check
9442 // that the value fits in an int.
9443 if (CVal != CVal64)
9444 return;
9445
Eric Christopher100c8332011-06-02 23:16:42 +00009446 switch (ConstraintLetter) {
Eric Christopher5e653c92011-07-01 01:00:07 +00009447 case 'j':
Andrew Trick3af7a672011-09-20 03:06:13 +00009448 // Constant suitable for movw, must be between 0 and
9449 // 65535.
9450 if (Subtarget->hasV6T2Ops())
9451 if (CVal >= 0 && CVal <= 65535)
9452 break;
9453 return;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009454 case 'I':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009455 if (Subtarget->isThumb1Only()) {
9456 // This must be a constant between 0 and 255, for ADD
9457 // immediates.
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009458 if (CVal >= 0 && CVal <= 255)
9459 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009460 } else if (Subtarget->isThumb2()) {
9461 // A constant that can be used as an immediate value in a
9462 // data-processing instruction.
9463 if (ARM_AM::getT2SOImmVal(CVal) != -1)
9464 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009465 } else {
9466 // A constant that can be used as an immediate value in a
9467 // data-processing instruction.
9468 if (ARM_AM::getSOImmVal(CVal) != -1)
9469 break;
9470 }
9471 return;
9472
9473 case 'J':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009474 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009475 // This must be a constant between -255 and -1, for negated ADD
9476 // immediates. This can be used in GCC with an "n" modifier that
9477 // prints the negated value, for use with SUB instructions. It is
9478 // not useful otherwise but is implemented for compatibility.
9479 if (CVal >= -255 && CVal <= -1)
9480 break;
9481 } else {
9482 // This must be a constant between -4095 and 4095. It is not clear
9483 // what this constraint is intended for. Implemented for
9484 // compatibility with GCC.
9485 if (CVal >= -4095 && CVal <= 4095)
9486 break;
9487 }
9488 return;
9489
9490 case 'K':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009491 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009492 // A 32-bit value where only one byte has a nonzero value. Exclude
9493 // zero to match GCC. This constraint is used by GCC internally for
9494 // constants that can be loaded with a move/shift combination.
9495 // It is not useful otherwise but is implemented for compatibility.
9496 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
9497 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009498 } else if (Subtarget->isThumb2()) {
9499 // A constant whose bitwise inverse can be used as an immediate
9500 // value in a data-processing instruction. This can be used in GCC
9501 // with a "B" modifier that prints the inverted value, for use with
9502 // BIC and MVN instructions. It is not useful otherwise but is
9503 // implemented for compatibility.
9504 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
9505 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009506 } else {
9507 // A constant whose bitwise inverse can be used as an immediate
9508 // value in a data-processing instruction. This can be used in GCC
9509 // with a "B" modifier that prints the inverted value, for use with
9510 // BIC and MVN instructions. It is not useful otherwise but is
9511 // implemented for compatibility.
9512 if (ARM_AM::getSOImmVal(~CVal) != -1)
9513 break;
9514 }
9515 return;
9516
9517 case 'L':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009518 if (Subtarget->isThumb1Only()) {
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009519 // This must be a constant between -7 and 7,
9520 // for 3-operand ADD/SUB immediate instructions.
9521 if (CVal >= -7 && CVal < 7)
9522 break;
David Goodwinf1daf7d2009-07-08 23:10:31 +00009523 } else if (Subtarget->isThumb2()) {
9524 // A constant whose negation can be used as an immediate value in a
9525 // data-processing instruction. This can be used in GCC with an "n"
9526 // modifier that prints the negated value, for use with SUB
9527 // instructions. It is not useful otherwise but is implemented for
9528 // compatibility.
9529 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
9530 break;
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009531 } else {
9532 // A constant whose negation can be used as an immediate value in a
9533 // data-processing instruction. This can be used in GCC with an "n"
9534 // modifier that prints the negated value, for use with SUB
9535 // instructions. It is not useful otherwise but is implemented for
9536 // compatibility.
9537 if (ARM_AM::getSOImmVal(-CVal) != -1)
9538 break;
9539 }
9540 return;
9541
9542 case 'M':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009543 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009544 // This must be a multiple of 4 between 0 and 1020, for
9545 // ADD sp + immediate.
9546 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
9547 break;
9548 } else {
9549 // A power of two or a constant between 0 and 32. This is used in
9550 // GCC for the shift amount on shifted register operands, but it is
9551 // useful in general for any shift amounts.
9552 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
9553 break;
9554 }
9555 return;
9556
9557 case 'N':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009558 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009559 // This must be a constant between 0 and 31, for shift amounts.
9560 if (CVal >= 0 && CVal <= 31)
9561 break;
9562 }
9563 return;
9564
9565 case 'O':
David Goodwinf1daf7d2009-07-08 23:10:31 +00009566 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009567 // This must be a multiple of 4 between -508 and 508, for
9568 // ADD/SUB sp = sp + immediate.
9569 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
9570 break;
9571 }
9572 return;
9573 }
9574 Result = DAG.getTargetConstant(CVal, Op.getValueType());
9575 break;
9576 }
9577
9578 if (Result.getNode()) {
9579 Ops.push_back(Result);
9580 return;
9581 }
Dale Johannesen1784d162010-06-25 21:55:36 +00009582 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsonbf6396b2009-04-01 17:58:54 +00009583}
Anton Korobeynikov48e19352009-09-23 19:04:09 +00009584
9585bool
9586ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
9587 // The ARM target isn't yet aware of offsets.
9588 return false;
9589}
Evan Cheng39382422009-10-28 01:44:26 +00009590
Jim Grosbach469bbdb2010-07-16 23:05:05 +00009591bool ARM::isBitFieldInvertedMask(unsigned v) {
9592 if (v == 0xffffffff)
9593 return 0;
9594 // there can be 1's on either or both "outsides", all the "inside"
9595 // bits must be 0's
9596 unsigned int lsb = 0, msb = 31;
9597 while (v & (1 << msb)) --msb;
9598 while (v & (1 << lsb)) ++lsb;
9599 for (unsigned int i = lsb; i <= msb; ++i) {
9600 if (v & (1 << i))
9601 return 0;
9602 }
9603 return 1;
9604}
9605
Evan Cheng39382422009-10-28 01:44:26 +00009606/// isFPImmLegal - Returns true if the target can instruction select the
9607/// specified FP immediate natively. If false, the legalizer will
9608/// materialize the FP immediate as a load from a constant pool.
9609bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
9610 if (!Subtarget->hasVFP3())
9611 return false;
9612 if (VT == MVT::f32)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009613 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009614 if (VT == MVT::f64)
Jim Grosbach4ebbf7b2011-09-30 00:50:06 +00009615 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng39382422009-10-28 01:44:26 +00009616 return false;
9617}
Bob Wilson65ffec42010-09-21 17:56:22 +00009618
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009619/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson65ffec42010-09-21 17:56:22 +00009620/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
9621/// specified in the intrinsic calls.
9622bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
9623 const CallInst &I,
9624 unsigned Intrinsic) const {
9625 switch (Intrinsic) {
9626 case Intrinsic::arm_neon_vld1:
9627 case Intrinsic::arm_neon_vld2:
9628 case Intrinsic::arm_neon_vld3:
9629 case Intrinsic::arm_neon_vld4:
9630 case Intrinsic::arm_neon_vld2lane:
9631 case Intrinsic::arm_neon_vld3lane:
9632 case Intrinsic::arm_neon_vld4lane: {
9633 Info.opc = ISD::INTRINSIC_W_CHAIN;
9634 // Conservatively set memVT to the entire set of vectors loaded.
9635 uint64_t NumElts = getTargetData()->getTypeAllocSize(I.getType()) / 8;
9636 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9637 Info.ptrVal = I.getArgOperand(0);
9638 Info.offset = 0;
9639 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9640 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9641 Info.vol = false; // volatile loads with NEON intrinsics not supported
9642 Info.readMem = true;
9643 Info.writeMem = false;
9644 return true;
9645 }
9646 case Intrinsic::arm_neon_vst1:
9647 case Intrinsic::arm_neon_vst2:
9648 case Intrinsic::arm_neon_vst3:
9649 case Intrinsic::arm_neon_vst4:
9650 case Intrinsic::arm_neon_vst2lane:
9651 case Intrinsic::arm_neon_vst3lane:
9652 case Intrinsic::arm_neon_vst4lane: {
9653 Info.opc = ISD::INTRINSIC_VOID;
9654 // Conservatively set memVT to the entire set of vectors stored.
9655 unsigned NumElts = 0;
9656 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009657 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson65ffec42010-09-21 17:56:22 +00009658 if (!ArgTy->isVectorTy())
9659 break;
9660 NumElts += getTargetData()->getTypeAllocSize(ArgTy) / 8;
9661 }
9662 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
9663 Info.ptrVal = I.getArgOperand(0);
9664 Info.offset = 0;
9665 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
9666 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
9667 Info.vol = false; // volatile stores with NEON intrinsics not supported
9668 Info.readMem = false;
9669 Info.writeMem = true;
9670 return true;
9671 }
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009672 case Intrinsic::arm_strexd: {
9673 Info.opc = ISD::INTRINSIC_W_CHAIN;
9674 Info.memVT = MVT::i64;
9675 Info.ptrVal = I.getArgOperand(2);
9676 Info.offset = 0;
9677 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009678 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009679 Info.readMem = false;
9680 Info.writeMem = true;
9681 return true;
9682 }
9683 case Intrinsic::arm_ldrexd: {
9684 Info.opc = ISD::INTRINSIC_W_CHAIN;
9685 Info.memVT = MVT::i64;
9686 Info.ptrVal = I.getArgOperand(0);
9687 Info.offset = 0;
9688 Info.align = 8;
Bruno Cardoso Lopesc75448c2011-06-16 18:11:32 +00009689 Info.vol = true;
Bruno Cardoso Lopesa0112d02011-05-28 04:07:29 +00009690 Info.readMem = true;
9691 Info.writeMem = false;
9692 return true;
9693 }
Bob Wilson65ffec42010-09-21 17:56:22 +00009694 default:
9695 break;
9696 }
9697
9698 return false;
9699}