1 //===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that ARM uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "arm-isel"
16 #include "ARMISelLowering.h"
18 #include "ARMCallingConv.h"
19 #include "ARMConstantPoolValue.h"
20 #include "ARMMachineFunctionInfo.h"
21 #include "ARMPerfectShuffle.h"
22 #include "ARMSubtarget.h"
23 #include "ARMTargetMachine.h"
24 #include "ARMTargetObjectFile.h"
25 #include "MCTargetDesc/ARMAddressingModes.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/CodeGen/CallingConvLower.h"
29 #include "llvm/CodeGen/IntrinsicLowering.h"
30 #include "llvm/CodeGen/MachineBasicBlock.h"
31 #include "llvm/CodeGen/MachineFrameInfo.h"
32 #include "llvm/CodeGen/MachineFunction.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/CodeGen/MachineModuleInfo.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/SelectionDAG.h"
37 #include "llvm/IR/CallingConv.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/Function.h"
40 #include "llvm/IR/GlobalValue.h"
41 #include "llvm/IR/Instruction.h"
42 #include "llvm/IR/Instructions.h"
43 #include "llvm/IR/Intrinsics.h"
44 #include "llvm/IR/Type.h"
45 #include "llvm/MC/MCSectionMachO.h"
46 #include "llvm/Support/CommandLine.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/MathExtras.h"
49 #include "llvm/Support/raw_ostream.h"
50 #include "llvm/Target/TargetOptions.h"
53 STATISTIC(NumTailCalls, "Number of tail calls");
54 STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
55 STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
57 // This option should go away when tail calls fully work.
59 EnableARMTailCalls("arm-tail-calls", cl::Hidden,
60 cl::desc("Generate tail calls (TEMPORARY OPTION)."),
64 EnableARMLongCalls("arm-long-calls", cl::Hidden,
65 cl::desc("Generate calls via indirect call instructions"),
69 ARMInterworking("arm-interworking", cl::Hidden,
70 cl::desc("Enable / disable ARM interworking (for debugging only)"),
74 class ARMCCState : public CCState {
76 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
77 const TargetMachine &TM, SmallVectorImpl<CCValAssign> &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.");
88 // The APCS parameter registers.
89 static const uint16_t GPRArgRegs[] = {
90 ARM::R0, ARM::R1, ARM::R2, ARM::R3
93 void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
94 MVT PromotedBitwiseVT) {
95 if (VT != PromotedLdStVT) {
96 setOperationAction(ISD::LOAD, VT, Promote);
97 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
99 setOperationAction(ISD::STORE, VT, Promote);
100 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
103 MVT ElemTy = VT.getVectorElementType();
104 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
105 setOperationAction(ISD::SETCC, VT, Custom);
106 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
107 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
108 if (ElemTy == MVT::i32) {
109 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);
114 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);
119 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::VSELECT, VT, Expand);
126 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
127 if (VT.isInteger()) {
128 setOperationAction(ISD::SHL, VT, Custom);
129 setOperationAction(ISD::SRA, VT, Custom);
130 setOperationAction(ISD::SRL, VT, Custom);
133 // Promote all bit-wise operations.
134 if (VT.isInteger() && VT != PromotedBitwiseVT) {
135 setOperationAction(ISD::AND, VT, Promote);
136 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
137 setOperationAction(ISD::OR, VT, Promote);
138 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
139 setOperationAction(ISD::XOR, VT, Promote);
140 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
143 // Neon does not support vector divide/remainder operations.
144 setOperationAction(ISD::SDIV, VT, Expand);
145 setOperationAction(ISD::UDIV, VT, Expand);
146 setOperationAction(ISD::FDIV, VT, Expand);
147 setOperationAction(ISD::SREM, VT, Expand);
148 setOperationAction(ISD::UREM, VT, Expand);
149 setOperationAction(ISD::FREM, VT, Expand);
152 void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
153 addRegisterClass(VT, &ARM::DPRRegClass);
154 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
157 void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
158 addRegisterClass(VT, &ARM::QPRRegClass);
159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
162 static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
163 if (TM.getSubtarget<ARMSubtarget>().isTargetDarwin())
164 return new TargetLoweringObjectFileMachO();
166 return new ARMElfTargetObjectFile();
169 ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
170 : TargetLowering(TM, createTLOF(TM)) {
171 Subtarget = &TM.getSubtarget<ARMSubtarget>();
172 RegInfo = TM.getRegisterInfo();
173 Itins = TM.getInstrItineraryData();
175 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
177 if (Subtarget->isTargetDarwin()) {
178 // Uses VFP for Thumb libfuncs if available.
179 if (Subtarget->isThumb() && Subtarget->hasVFP2()) {
180 // Single-precision floating-point arithmetic.
181 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
182 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
183 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
184 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
186 // Double-precision floating-point arithmetic.
187 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
188 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
189 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
190 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
192 // Single-precision comparisons.
193 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
194 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
195 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
196 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
197 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
198 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
199 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
200 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
202 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
208 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
209 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
211 // Double-precision comparisons.
212 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
213 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
214 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
215 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
216 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
217 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
218 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
219 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
221 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
227 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
228 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
230 // Floating-point to integer conversions.
231 // i64 conversions are done via library routines even when generating VFP
232 // instructions, so use the same ones.
233 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
234 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
235 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
236 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
238 // Conversions between floating types.
239 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
240 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
242 // Integer to floating-point conversions.
243 // i64 conversions are done via library routines even when generating VFP
244 // instructions, so use the same ones.
245 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
246 // e.g., __floatunsidf vs. __floatunssidfvfp.
247 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
248 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
249 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
250 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
254 // These libcalls are not available in 32-bit.
255 setLibcallName(RTLIB::SHL_I128, 0);
256 setLibcallName(RTLIB::SRL_I128, 0);
257 setLibcallName(RTLIB::SRA_I128, 0);
259 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetDarwin()) {
260 // Double-precision floating-point arithmetic helper functions
261 // RTABI chapter 4.1.2, Table 2
262 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
263 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
264 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
265 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
266 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
267 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
268 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
269 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
271 // Double-precision floating-point comparison helper functions
272 // RTABI chapter 4.1.2, Table 3
273 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
274 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
275 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
276 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
277 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
278 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
279 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
280 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
281 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
282 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
283 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
284 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
285 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
286 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
287 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
288 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
289 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
292 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
295 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
296 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
298 // Single-precision floating-point arithmetic helper functions
299 // RTABI chapter 4.1.2, Table 4
300 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
301 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
302 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
303 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
304 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
305 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
306 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
307 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
309 // Single-precision floating-point comparison helper functions
310 // RTABI chapter 4.1.2, Table 5
311 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
312 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
313 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
314 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
315 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
316 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
317 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
318 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
319 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
320 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
321 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
322 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
323 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
324 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
325 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
326 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
327 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
329 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
333 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
334 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
336 // Floating-point to integer conversions.
337 // RTABI chapter 4.1.2, Table 6
338 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
339 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
340 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
341 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
342 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
343 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
344 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
345 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
346 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
348 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
349 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
352 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
353 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
355 // Conversions between floating types.
356 // RTABI chapter 4.1.2, Table 7
357 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
358 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
359 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
360 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
362 // Integer to floating-point conversions.
363 // RTABI chapter 4.1.2, Table 8
364 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
365 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
366 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
367 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
368 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
369 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
370 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
371 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
372 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
378 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
379 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
381 // Long long helper functions
382 // RTABI chapter 4.2, Table 9
383 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
384 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
385 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
386 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
387 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
394 // Integer division functions
395 // RTABI chapter 4.3.1
396 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
397 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
398 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
399 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
400 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
401 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
402 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
403 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
404 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
405 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
406 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
407 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
408 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
409 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
410 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
411 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
414 // RTABI chapter 4.3.4
415 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
416 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
417 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
418 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
419 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
420 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
423 // Use divmod compiler-rt calls for iOS 5.0 and later.
424 if (Subtarget->getTargetTriple().getOS() == Triple::IOS &&
425 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
426 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
427 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
430 if (Subtarget->isThumb1Only())
431 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
433 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
434 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
435 !Subtarget->isThumb1Only()) {
436 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
437 if (!Subtarget->isFPOnlySP())
438 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
440 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
443 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
444 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
445 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
446 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
447 setTruncStoreAction((MVT::SimpleValueType)VT,
448 (MVT::SimpleValueType)InnerVT, Expand);
449 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
450 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
451 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
454 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
455 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
457 if (Subtarget->hasNEON()) {
458 addDRTypeForNEON(MVT::v2f32);
459 addDRTypeForNEON(MVT::v8i8);
460 addDRTypeForNEON(MVT::v4i16);
461 addDRTypeForNEON(MVT::v2i32);
462 addDRTypeForNEON(MVT::v1i64);
464 addQRTypeForNEON(MVT::v4f32);
465 addQRTypeForNEON(MVT::v2f64);
466 addQRTypeForNEON(MVT::v16i8);
467 addQRTypeForNEON(MVT::v8i16);
468 addQRTypeForNEON(MVT::v4i32);
469 addQRTypeForNEON(MVT::v2i64);
471 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
472 // neither Neon nor VFP support any arithmetic operations on it.
473 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
474 // supported for v4f32.
475 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
476 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
477 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
478 // FIXME: Code duplication: FDIV and FREM are expanded always, see
479 // ARMTargetLowering::addTypeForNEON method for details.
480 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
481 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
482 // FIXME: Create unittest.
483 // In another words, find a way when "copysign" appears in DAG with vector
485 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
486 // FIXME: Code duplication: SETCC has custom operation action, see
487 // ARMTargetLowering::addTypeForNEON method for details.
488 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
489 // FIXME: Create unittest for FNEG and for FABS.
490 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
491 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
492 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
493 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
494 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
495 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
496 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
497 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
498 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
499 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
500 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
501 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
502 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
503 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
504 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
505 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
506 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
507 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
508 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
510 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
511 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
512 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
513 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
514 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
515 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
516 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
517 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
518 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
519 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
520 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
521 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
522 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
523 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
524 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
526 // Mark v2f32 intrinsics.
527 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
528 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
529 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
530 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
531 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
532 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
533 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
534 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
535 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
536 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
537 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
538 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
539 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
540 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
541 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
543 // Neon does not support some operations on v1i64 and v2i64 types.
544 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
545 // Custom handling for some quad-vector types to detect VMULL.
546 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
547 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
548 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
549 // Custom handling for some vector types to avoid expensive expansions
550 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
551 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
552 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
553 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
554 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
555 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
556 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
557 // a destination type that is wider than the source, and nor does
558 // it have a FP_TO_[SU]INT instruction with a narrower destination than
560 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
561 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
562 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
563 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
565 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
566 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
568 // Custom expand long extensions to vectors.
569 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
570 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
571 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
572 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
573 setOperationAction(ISD::SIGN_EXTEND, MVT::v16i32, Custom);
574 setOperationAction(ISD::ZERO_EXTEND, MVT::v16i32, Custom);
575 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i64, Custom);
576 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i64, Custom);
578 // NEON does not have single instruction CTPOP for vectors with element
579 // types wider than 8-bits. However, custom lowering can leverage the
580 // v8i8/v16i8 vcnt instruction.
581 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
582 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
583 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
584 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
586 // NEON only has FMA instructions as of VFP4.
587 if (!Subtarget->hasVFP4()) {
588 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
589 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
592 setTargetDAGCombine(ISD::INTRINSIC_VOID);
593 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
594 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
595 setTargetDAGCombine(ISD::SHL);
596 setTargetDAGCombine(ISD::SRL);
597 setTargetDAGCombine(ISD::SRA);
598 setTargetDAGCombine(ISD::SIGN_EXTEND);
599 setTargetDAGCombine(ISD::ZERO_EXTEND);
600 setTargetDAGCombine(ISD::ANY_EXTEND);
601 setTargetDAGCombine(ISD::SELECT_CC);
602 setTargetDAGCombine(ISD::BUILD_VECTOR);
603 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
604 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
605 setTargetDAGCombine(ISD::STORE);
606 setTargetDAGCombine(ISD::FP_TO_SINT);
607 setTargetDAGCombine(ISD::FP_TO_UINT);
608 setTargetDAGCombine(ISD::FDIV);
610 // It is legal to extload from v4i8 to v4i16 or v4i32.
611 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
612 MVT::v4i16, MVT::v2i16,
614 for (unsigned i = 0; i < 6; ++i) {
615 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
616 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
617 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
621 // ARM and Thumb2 support UMLAL/SMLAL.
622 if (!Subtarget->isThumb1Only())
623 setTargetDAGCombine(ISD::ADDC);
626 computeRegisterProperties();
628 // ARM does not have f32 extending load.
629 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
631 // ARM does not have i1 sign extending load.
632 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
634 // ARM supports all 4 flavors of integer indexed load / store.
635 if (!Subtarget->isThumb1Only()) {
636 for (unsigned im = (unsigned)ISD::PRE_INC;
637 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
638 setIndexedLoadAction(im, MVT::i1, Legal);
639 setIndexedLoadAction(im, MVT::i8, Legal);
640 setIndexedLoadAction(im, MVT::i16, Legal);
641 setIndexedLoadAction(im, MVT::i32, Legal);
642 setIndexedStoreAction(im, MVT::i1, Legal);
643 setIndexedStoreAction(im, MVT::i8, Legal);
644 setIndexedStoreAction(im, MVT::i16, Legal);
645 setIndexedStoreAction(im, MVT::i32, Legal);
649 // i64 operation support.
650 setOperationAction(ISD::MUL, MVT::i64, Expand);
651 setOperationAction(ISD::MULHU, MVT::i32, Expand);
652 if (Subtarget->isThumb1Only()) {
653 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
654 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
656 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
657 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
658 setOperationAction(ISD::MULHS, MVT::i32, Expand);
660 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
661 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
662 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
663 setOperationAction(ISD::SRL, MVT::i64, Custom);
664 setOperationAction(ISD::SRA, MVT::i64, Custom);
666 if (!Subtarget->isThumb1Only()) {
667 // FIXME: We should do this for Thumb1 as well.
668 setOperationAction(ISD::ADDC, MVT::i32, Custom);
669 setOperationAction(ISD::ADDE, MVT::i32, Custom);
670 setOperationAction(ISD::SUBC, MVT::i32, Custom);
671 setOperationAction(ISD::SUBE, MVT::i32, Custom);
674 // ARM does not have ROTL.
675 setOperationAction(ISD::ROTL, MVT::i32, Expand);
676 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
677 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
678 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
679 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
681 // These just redirect to CTTZ and CTLZ on ARM.
682 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
683 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
685 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
687 // Only ARMv6 has BSWAP.
688 if (!Subtarget->hasV6Ops())
689 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
691 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
692 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
693 // These are expanded into libcalls if the cpu doesn't have HW divider.
694 setOperationAction(ISD::SDIV, MVT::i32, Expand);
695 setOperationAction(ISD::UDIV, MVT::i32, Expand);
698 // FIXME: Also set divmod for SREM on EABI
699 setOperationAction(ISD::SREM, MVT::i32, Expand);
700 setOperationAction(ISD::UREM, MVT::i32, Expand);
701 // Register based DivRem for AEABI (RTABI 4.2)
702 if (Subtarget->isTargetAEABI()) {
703 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
704 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
705 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
706 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
707 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
708 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
709 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
710 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
712 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
713 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
714 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
715 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
716 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
717 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
718 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
719 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
721 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
722 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
724 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
725 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
728 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
729 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
730 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
731 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
732 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
734 setOperationAction(ISD::TRAP, MVT::Other, Legal);
736 // Use the default implementation.
737 setOperationAction(ISD::VASTART, MVT::Other, Custom);
738 setOperationAction(ISD::VAARG, MVT::Other, Expand);
739 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
740 setOperationAction(ISD::VAEND, MVT::Other, Expand);
741 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
742 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
744 if (!Subtarget->isTargetDarwin()) {
745 // Non-Darwin platforms may return values in these registers via the
746 // personality function.
747 setExceptionPointerRegister(ARM::R0);
748 setExceptionSelectorRegister(ARM::R1);
751 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
752 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
753 // the default expansion.
754 // FIXME: This should be checking for v6k, not just v6.
755 if (Subtarget->hasDataBarrier() ||
756 (Subtarget->hasV6Ops() && !Subtarget->isThumb())) {
757 // membarrier needs custom lowering; the rest are legal and handled
759 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
760 // Custom lowering for 64-bit ops
761 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
762 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
763 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
764 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
765 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
766 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
767 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom);
768 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom);
769 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom);
770 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom);
771 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
772 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
773 setInsertFencesForAtomic(true);
775 // Set them all for expansion, which will force libcalls.
776 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
777 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
778 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
779 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
780 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
781 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
782 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
783 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
784 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
785 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
786 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
787 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
788 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
789 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
790 // Unordered/Monotonic case.
791 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
792 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
795 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
797 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
798 if (!Subtarget->hasV6Ops()) {
799 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
800 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
802 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
804 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
805 !Subtarget->isThumb1Only()) {
806 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
807 // iff target supports vfp2.
808 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
809 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
812 // We want to custom lower some of our intrinsics.
813 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
814 if (Subtarget->isTargetDarwin()) {
815 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
816 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
817 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
820 setOperationAction(ISD::SETCC, MVT::i32, Expand);
821 setOperationAction(ISD::SETCC, MVT::f32, Expand);
822 setOperationAction(ISD::SETCC, MVT::f64, Expand);
823 setOperationAction(ISD::SELECT, MVT::i32, Custom);
824 setOperationAction(ISD::SELECT, MVT::f32, Custom);
825 setOperationAction(ISD::SELECT, MVT::f64, Custom);
826 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
827 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
828 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
830 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
831 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
832 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
833 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
834 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
836 // We don't support sin/cos/fmod/copysign/pow
837 setOperationAction(ISD::FSIN, MVT::f64, Expand);
838 setOperationAction(ISD::FSIN, MVT::f32, Expand);
839 setOperationAction(ISD::FCOS, MVT::f32, Expand);
840 setOperationAction(ISD::FCOS, MVT::f64, Expand);
841 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
842 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
843 setOperationAction(ISD::FREM, MVT::f64, Expand);
844 setOperationAction(ISD::FREM, MVT::f32, Expand);
845 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
846 !Subtarget->isThumb1Only()) {
847 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
848 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
850 setOperationAction(ISD::FPOW, MVT::f64, Expand);
851 setOperationAction(ISD::FPOW, MVT::f32, Expand);
853 if (!Subtarget->hasVFP4()) {
854 setOperationAction(ISD::FMA, MVT::f64, Expand);
855 setOperationAction(ISD::FMA, MVT::f32, Expand);
858 // Various VFP goodness
859 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
860 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
861 if (Subtarget->hasVFP2()) {
862 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
863 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
864 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
865 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
867 // Special handling for half-precision FP.
868 if (!Subtarget->hasFP16()) {
869 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
870 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
874 // We have target-specific dag combine patterns for the following nodes:
875 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
876 setTargetDAGCombine(ISD::ADD);
877 setTargetDAGCombine(ISD::SUB);
878 setTargetDAGCombine(ISD::MUL);
879 setTargetDAGCombine(ISD::AND);
880 setTargetDAGCombine(ISD::OR);
881 setTargetDAGCombine(ISD::XOR);
883 if (Subtarget->hasV6Ops())
884 setTargetDAGCombine(ISD::SRL);
886 setStackPointerRegisterToSaveRestore(ARM::SP);
888 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
889 !Subtarget->hasVFP2())
890 setSchedulingPreference(Sched::RegPressure);
892 setSchedulingPreference(Sched::Hybrid);
894 //// temporary - rewrite interface to use type
895 MaxStoresPerMemset = 8;
896 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
897 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
898 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
899 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
900 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
902 // On ARM arguments smaller than 4 bytes are extended, so all arguments
903 // are at least 4 bytes aligned.
904 setMinStackArgumentAlignment(4);
906 // Prefer likely predicted branches to selects on out-of-order cores.
907 PredictableSelectIsExpensive = Subtarget->isLikeA9();
909 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
912 // FIXME: It might make sense to define the representative register class as the
913 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is
914 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
915 // SPR's representative would be DPR_VFP2. This should work well if register
916 // pressure tracking were modified such that a register use would increment the
917 // pressure of the register class's representative and all of it's super
918 // classes' representatives transitively. We have not implemented this because
919 // of the difficulty prior to coalescing of modeling operand register classes
920 // due to the common occurrence of cross class copies and subregister insertions
922 std::pair<const TargetRegisterClass*, uint8_t>
923 ARMTargetLowering::findRepresentativeClass(MVT VT) const{
924 const TargetRegisterClass *RRC = 0;
926 switch (VT.SimpleTy) {
928 return TargetLowering::findRepresentativeClass(VT);
929 // Use DPR as representative register class for all floating point
930 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
931 // the cost is 1 for both f32 and f64.
932 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
933 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
934 RRC = &ARM::DPRRegClass;
935 // When NEON is used for SP, only half of the register file is available
936 // because operations that define both SP and DP results will be constrained
937 // to the VFP2 class (D0-D15). We currently model this constraint prior to
938 // coalescing by double-counting the SP regs. See the FIXME above.
939 if (Subtarget->useNEONForSinglePrecisionFP())
942 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
943 case MVT::v4f32: case MVT::v2f64:
944 RRC = &ARM::DPRRegClass;
948 RRC = &ARM::DPRRegClass;
952 RRC = &ARM::DPRRegClass;
956 return std::make_pair(RRC, Cost);
959 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
962 case ARMISD::Wrapper: return "ARMISD::Wrapper";
963 case ARMISD::WrapperDYN: return "ARMISD::WrapperDYN";
964 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
965 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
966 case ARMISD::CALL: return "ARMISD::CALL";
967 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
968 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
969 case ARMISD::tCALL: return "ARMISD::tCALL";
970 case ARMISD::BRCOND: return "ARMISD::BRCOND";
971 case ARMISD::BR_JT: return "ARMISD::BR_JT";
972 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
973 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
974 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
975 case ARMISD::CMP: return "ARMISD::CMP";
976 case ARMISD::CMN: return "ARMISD::CMN";
977 case ARMISD::CMPZ: return "ARMISD::CMPZ";
978 case ARMISD::CMPFP: return "ARMISD::CMPFP";
979 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
980 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
981 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
983 case ARMISD::CMOV: return "ARMISD::CMOV";
985 case ARMISD::RBIT: return "ARMISD::RBIT";
987 case ARMISD::FTOSI: return "ARMISD::FTOSI";
988 case ARMISD::FTOUI: return "ARMISD::FTOUI";
989 case ARMISD::SITOF: return "ARMISD::SITOF";
990 case ARMISD::UITOF: return "ARMISD::UITOF";
992 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
993 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
994 case ARMISD::RRX: return "ARMISD::RRX";
996 case ARMISD::ADDC: return "ARMISD::ADDC";
997 case ARMISD::ADDE: return "ARMISD::ADDE";
998 case ARMISD::SUBC: return "ARMISD::SUBC";
999 case ARMISD::SUBE: return "ARMISD::SUBE";
1001 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1002 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
1004 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1005 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1007 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
1009 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
1011 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1013 case ARMISD::MEMBARRIER: return "ARMISD::MEMBARRIER";
1014 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
1016 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1018 case ARMISD::VCEQ: return "ARMISD::VCEQ";
1019 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
1020 case ARMISD::VCGE: return "ARMISD::VCGE";
1021 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1022 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
1023 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1024 case ARMISD::VCGT: return "ARMISD::VCGT";
1025 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1026 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
1027 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1028 case ARMISD::VTST: return "ARMISD::VTST";
1030 case ARMISD::VSHL: return "ARMISD::VSHL";
1031 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1032 case ARMISD::VSHRu: return "ARMISD::VSHRu";
1033 case ARMISD::VSHLLs: return "ARMISD::VSHLLs";
1034 case ARMISD::VSHLLu: return "ARMISD::VSHLLu";
1035 case ARMISD::VSHLLi: return "ARMISD::VSHLLi";
1036 case ARMISD::VSHRN: return "ARMISD::VSHRN";
1037 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1038 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1039 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1040 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1041 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1042 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1043 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1044 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1045 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1046 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1047 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1048 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1049 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1050 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
1051 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
1052 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
1053 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
1054 case ARMISD::VDUP: return "ARMISD::VDUP";
1055 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
1056 case ARMISD::VEXT: return "ARMISD::VEXT";
1057 case ARMISD::VREV64: return "ARMISD::VREV64";
1058 case ARMISD::VREV32: return "ARMISD::VREV32";
1059 case ARMISD::VREV16: return "ARMISD::VREV16";
1060 case ARMISD::VZIP: return "ARMISD::VZIP";
1061 case ARMISD::VUZP: return "ARMISD::VUZP";
1062 case ARMISD::VTRN: return "ARMISD::VTRN";
1063 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1064 case ARMISD::VTBL2: return "ARMISD::VTBL2";
1065 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1066 case ARMISD::VMULLu: return "ARMISD::VMULLu";
1067 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1068 case ARMISD::SMLAL: return "ARMISD::SMLAL";
1069 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
1070 case ARMISD::FMAX: return "ARMISD::FMAX";
1071 case ARMISD::FMIN: return "ARMISD::FMIN";
1072 case ARMISD::BFI: return "ARMISD::BFI";
1073 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1074 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
1075 case ARMISD::VBSL: return "ARMISD::VBSL";
1076 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1077 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1078 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
1079 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1080 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1081 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1082 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1083 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1084 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1085 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1086 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1087 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1088 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1089 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1090 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1091 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1092 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1093 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1094 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1095 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
1097 case ARMISD::ATOMADD64_DAG: return "ATOMADD64_DAG";
1098 case ARMISD::ATOMSUB64_DAG: return "ATOMSUB64_DAG";
1099 case ARMISD::ATOMOR64_DAG: return "ATOMOR64_DAG";
1100 case ARMISD::ATOMXOR64_DAG: return "ATOMXOR64_DAG";
1101 case ARMISD::ATOMAND64_DAG: return "ATOMAND64_DAG";
1102 case ARMISD::ATOMNAND64_DAG: return "ATOMNAND64_DAG";
1103 case ARMISD::ATOMSWAP64_DAG: return "ATOMSWAP64_DAG";
1104 case ARMISD::ATOMCMPXCHG64_DAG: return "ATOMCMPXCHG64_DAG";
1105 case ARMISD::ATOMMIN64_DAG: return "ATOMMIN64_DAG";
1106 case ARMISD::ATOMUMIN64_DAG: return "ATOMUMIN64_DAG";
1107 case ARMISD::ATOMMAX64_DAG: return "ATOMMAX64_DAG";
1108 case ARMISD::ATOMUMAX64_DAG: return "ATOMUMAX64_DAG";
1112 EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1113 if (!VT.isVector()) return getPointerTy();
1114 return VT.changeVectorElementTypeToInteger();
1117 /// getRegClassFor - Return the register class that should be used for the
1118 /// specified value type.
1119 const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
1120 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1121 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1122 // load / store 4 to 8 consecutive D registers.
1123 if (Subtarget->hasNEON()) {
1124 if (VT == MVT::v4i64)
1125 return &ARM::QQPRRegClass;
1126 if (VT == MVT::v8i64)
1127 return &ARM::QQQQPRRegClass;
1129 return TargetLowering::getRegClassFor(VT);
1132 // Create a fast isel object.
1134 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1135 const TargetLibraryInfo *libInfo) const {
1136 return ARM::createFastISel(funcInfo, libInfo);
1139 /// getMaximalGlobalOffset - Returns the maximal possible offset which can
1140 /// be used for loads / stores from the global.
1141 unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1142 return (Subtarget->isThumb1Only() ? 127 : 4095);
1145 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
1146 unsigned NumVals = N->getNumValues();
1148 return Sched::RegPressure;
1150 for (unsigned i = 0; i != NumVals; ++i) {
1151 EVT VT = N->getValueType(i);
1152 if (VT == MVT::Glue || VT == MVT::Other)
1154 if (VT.isFloatingPoint() || VT.isVector())
1158 if (!N->isMachineOpcode())
1159 return Sched::RegPressure;
1161 // Load are scheduled for latency even if there instruction itinerary
1162 // is not available.
1163 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1164 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
1166 if (MCID.getNumDefs() == 0)
1167 return Sched::RegPressure;
1168 if (!Itins->isEmpty() &&
1169 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
1172 return Sched::RegPressure;
1175 //===----------------------------------------------------------------------===//
1177 //===----------------------------------------------------------------------===//
1179 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1180 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1182 default: llvm_unreachable("Unknown condition code!");
1183 case ISD::SETNE: return ARMCC::NE;
1184 case ISD::SETEQ: return ARMCC::EQ;
1185 case ISD::SETGT: return ARMCC::GT;
1186 case ISD::SETGE: return ARMCC::GE;
1187 case ISD::SETLT: return ARMCC::LT;
1188 case ISD::SETLE: return ARMCC::LE;
1189 case ISD::SETUGT: return ARMCC::HI;
1190 case ISD::SETUGE: return ARMCC::HS;
1191 case ISD::SETULT: return ARMCC::LO;
1192 case ISD::SETULE: return ARMCC::LS;
1196 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1197 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
1198 ARMCC::CondCodes &CondCode2) {
1199 CondCode2 = ARMCC::AL;
1201 default: llvm_unreachable("Unknown FP condition!");
1203 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1205 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1207 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1208 case ISD::SETOLT: CondCode = ARMCC::MI; break;
1209 case ISD::SETOLE: CondCode = ARMCC::LS; break;
1210 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1211 case ISD::SETO: CondCode = ARMCC::VC; break;
1212 case ISD::SETUO: CondCode = ARMCC::VS; break;
1213 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1214 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1215 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1217 case ISD::SETULT: CondCode = ARMCC::LT; break;
1219 case ISD::SETULE: CondCode = ARMCC::LE; break;
1221 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1225 //===----------------------------------------------------------------------===//
1226 // Calling Convention Implementation
1227 //===----------------------------------------------------------------------===//
1229 #include "ARMGenCallingConv.inc"
1231 /// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1232 /// given CallingConvention value.
1233 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1235 bool isVarArg) const {
1238 llvm_unreachable("Unsupported calling convention");
1239 case CallingConv::Fast:
1240 if (Subtarget->hasVFP2() && !isVarArg) {
1241 if (!Subtarget->isAAPCS_ABI())
1242 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1243 // For AAPCS ABI targets, just use VFP variant of the calling convention.
1244 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1247 case CallingConv::C: {
1248 // Use target triple & subtarget features to do actual dispatch.
1249 if (!Subtarget->isAAPCS_ABI())
1250 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1251 else if (Subtarget->hasVFP2() &&
1252 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1254 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1255 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1257 case CallingConv::ARM_AAPCS_VFP:
1259 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1261 case CallingConv::ARM_AAPCS:
1262 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1263 case CallingConv::ARM_APCS:
1264 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1265 case CallingConv::GHC:
1266 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
1270 /// LowerCallResult - Lower the result values of a call into the
1271 /// appropriate copies out of appropriate physical registers.
1273 ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
1274 CallingConv::ID CallConv, bool isVarArg,
1275 const SmallVectorImpl<ISD::InputArg> &Ins,
1276 SDLoc dl, SelectionDAG &DAG,
1277 SmallVectorImpl<SDValue> &InVals,
1278 bool isThisReturn, SDValue ThisVal) const {
1280 // Assign locations to each value returned by this call.
1281 SmallVector<CCValAssign, 16> RVLocs;
1282 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1283 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
1284 CCInfo.AnalyzeCallResult(Ins,
1285 CCAssignFnForNode(CallConv, /* Return*/ true,
1288 // Copy all of the result registers out of their specified physreg.
1289 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1290 CCValAssign VA = RVLocs[i];
1292 // Pass 'this' value directly from the argument to return value, to avoid
1293 // reg unit interference
1294 if (i == 0 && isThisReturn) {
1295 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1296 "unexpected return calling convention register assignment");
1297 InVals.push_back(ThisVal);
1302 if (VA.needsCustom()) {
1303 // Handle f64 or half of a v2f64.
1304 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
1306 Chain = Lo.getValue(1);
1307 InFlag = Lo.getValue(2);
1308 VA = RVLocs[++i]; // skip ahead to next loc
1309 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
1311 Chain = Hi.getValue(1);
1312 InFlag = Hi.getValue(2);
1313 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
1315 if (VA.getLocVT() == MVT::v2f64) {
1316 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1317 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1318 DAG.getConstant(0, MVT::i32));
1320 VA = RVLocs[++i]; // skip ahead to next loc
1321 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
1322 Chain = Lo.getValue(1);
1323 InFlag = Lo.getValue(2);
1324 VA = RVLocs[++i]; // skip ahead to next loc
1325 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
1326 Chain = Hi.getValue(1);
1327 InFlag = Hi.getValue(2);
1328 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
1329 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1330 DAG.getConstant(1, MVT::i32));
1333 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1335 Chain = Val.getValue(1);
1336 InFlag = Val.getValue(2);
1339 switch (VA.getLocInfo()) {
1340 default: llvm_unreachable("Unknown loc info!");
1341 case CCValAssign::Full: break;
1342 case CCValAssign::BCvt:
1343 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
1347 InVals.push_back(Val);
1353 /// LowerMemOpCallTo - Store the argument to the stack.
1355 ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1356 SDValue StackPtr, SDValue Arg,
1357 SDLoc dl, SelectionDAG &DAG,
1358 const CCValAssign &VA,
1359 ISD::ArgFlagsTy Flags) const {
1360 unsigned LocMemOffset = VA.getLocMemOffset();
1361 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1362 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
1363 return DAG.getStore(Chain, dl, Arg, PtrOff,
1364 MachinePointerInfo::getStack(LocMemOffset),
1368 void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
1369 SDValue Chain, SDValue &Arg,
1370 RegsToPassVector &RegsToPass,
1371 CCValAssign &VA, CCValAssign &NextVA,
1373 SmallVectorImpl<SDValue> &MemOpChains,
1374 ISD::ArgFlagsTy Flags) const {
1376 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
1377 DAG.getVTList(MVT::i32, MVT::i32), Arg);
1378 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd));
1380 if (NextVA.isRegLoc())
1381 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1)));
1383 assert(NextVA.isMemLoc());
1384 if (StackPtr.getNode() == 0)
1385 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1387 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1),
1393 /// LowerCall - Lowering a call into a callseq_start <-
1394 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1397 ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
1398 SmallVectorImpl<SDValue> &InVals) const {
1399 SelectionDAG &DAG = CLI.DAG;
1401 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1402 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1403 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1404 SDValue Chain = CLI.Chain;
1405 SDValue Callee = CLI.Callee;
1406 bool &isTailCall = CLI.IsTailCall;
1407 CallingConv::ID CallConv = CLI.CallConv;
1408 bool doesNotRet = CLI.DoesNotReturn;
1409 bool isVarArg = CLI.IsVarArg;
1411 MachineFunction &MF = DAG.getMachineFunction();
1412 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1413 bool isThisReturn = false;
1414 bool isSibCall = false;
1415 // Disable tail calls if they're not supported.
1416 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
1419 // Check if it's really possible to do a tail call.
1420 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1421 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
1422 Outs, OutVals, Ins, DAG);
1423 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1424 // detected sibcalls.
1431 // Analyze operands of the call, assigning locations to each operand.
1432 SmallVector<CCValAssign, 16> ArgLocs;
1433 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1434 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
1435 CCInfo.AnalyzeCallOperands(Outs,
1436 CCAssignFnForNode(CallConv, /* Return*/ false,
1439 // Get a count of how many bytes are to be pushed on the stack.
1440 unsigned NumBytes = CCInfo.getNextStackOffset();
1442 // For tail calls, memory operands are available in our caller's stack.
1446 // Adjust the stack pointer for the new arguments...
1447 // These operations are automatically eliminated by the prolog/epilog pass
1449 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1452 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1454 RegsToPassVector RegsToPass;
1455 SmallVector<SDValue, 8> MemOpChains;
1457 // Walk the register/memloc assignments, inserting copies/loads. In the case
1458 // of tail call optimization, arguments are handled later.
1459 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1461 ++i, ++realArgIdx) {
1462 CCValAssign &VA = ArgLocs[i];
1463 SDValue Arg = OutVals[realArgIdx];
1464 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
1465 bool isByVal = Flags.isByVal();
1467 // Promote the value if needed.
1468 switch (VA.getLocInfo()) {
1469 default: llvm_unreachable("Unknown loc info!");
1470 case CCValAssign::Full: break;
1471 case CCValAssign::SExt:
1472 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1474 case CCValAssign::ZExt:
1475 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1477 case CCValAssign::AExt:
1478 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1480 case CCValAssign::BCvt:
1481 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
1485 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
1486 if (VA.needsCustom()) {
1487 if (VA.getLocVT() == MVT::v2f64) {
1488 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1489 DAG.getConstant(0, MVT::i32));
1490 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1491 DAG.getConstant(1, MVT::i32));
1493 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
1494 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1496 VA = ArgLocs[++i]; // skip ahead to next loc
1497 if (VA.isRegLoc()) {
1498 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
1499 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1501 assert(VA.isMemLoc());
1503 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1504 dl, DAG, VA, Flags));
1507 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
1508 StackPtr, MemOpChains, Flags);
1510 } else if (VA.isRegLoc()) {
1511 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1512 assert(VA.getLocVT() == MVT::i32 &&
1513 "unexpected calling convention register assignment");
1514 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
1515 "unexpected use of 'returned'");
1516 isThisReturn = true;
1518 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1519 } else if (isByVal) {
1520 assert(VA.isMemLoc());
1521 unsigned offset = 0;
1523 // True if this byval aggregate will be split between registers
1525 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1526 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1528 if (CurByValIdx < ByValArgsCount) {
1530 unsigned RegBegin, RegEnd;
1531 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1533 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1535 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
1536 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1537 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1538 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1539 MachinePointerInfo(),
1540 false, false, false, 0);
1541 MemOpChains.push_back(Load.getValue(1));
1542 RegsToPass.push_back(std::make_pair(j, Load));
1545 // If parameter size outsides register area, "offset" value
1546 // helps us to calculate stack slot for remained part properly.
1547 offset = RegEnd - RegBegin;
1549 CCInfo.nextInRegsParam();
1552 if (Flags.getByValSize() > 4*offset) {
1553 unsigned LocMemOffset = VA.getLocMemOffset();
1554 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1555 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1557 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1558 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1559 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1561 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
1563 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
1564 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
1565 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1566 Ops, array_lengthof(Ops)));
1568 } else if (!isSibCall) {
1569 assert(VA.isMemLoc());
1571 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1572 dl, DAG, VA, Flags));
1576 if (!MemOpChains.empty())
1577 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1578 &MemOpChains[0], MemOpChains.size());
1580 // Build a sequence of copy-to-reg nodes chained together with token chain
1581 // and flag operands which copy the outgoing args into the appropriate regs.
1583 // Tail call byval lowering might overwrite argument registers so in case of
1584 // tail call optimization the copies to registers are lowered later.
1586 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1587 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1588 RegsToPass[i].second, InFlag);
1589 InFlag = Chain.getValue(1);
1592 // For tail calls lower the arguments to the 'real' stack slot.
1594 // Force all the incoming stack arguments to be loaded from the stack
1595 // before any new outgoing arguments are stored to the stack, because the
1596 // outgoing stack slots may alias the incoming argument stack slots, and
1597 // the alias isn't otherwise explicit. This is slightly more conservative
1598 // than necessary, because it means that each store effectively depends
1599 // on every argument instead of just those arguments it would clobber.
1601 // Do not flag preceding copytoreg stuff together with the following stuff.
1603 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1604 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1605 RegsToPass[i].second, InFlag);
1606 InFlag = Chain.getValue(1);
1611 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1612 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1613 // node so that legalize doesn't hack it.
1614 bool isDirect = false;
1615 bool isARMFunc = false;
1616 bool isLocalARMFunc = false;
1617 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1619 if (EnableARMLongCalls) {
1620 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1621 && "long-calls with non-static relocation model!");
1622 // Handle a global address or an external symbol. If it's not one of
1623 // those, the target's already in a register, so we don't need to do
1625 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1626 const GlobalValue *GV = G->getGlobal();
1627 // Create a constant pool entry for the callee address
1628 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
1629 ARMConstantPoolValue *CPV =
1630 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1632 // Get the address of the callee into a register
1633 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1634 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1635 Callee = DAG.getLoad(getPointerTy(), dl,
1636 DAG.getEntryNode(), CPAddr,
1637 MachinePointerInfo::getConstantPool(),
1638 false, false, false, 0);
1639 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1640 const char *Sym = S->getSymbol();
1642 // Create a constant pool entry for the callee address
1643 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
1644 ARMConstantPoolValue *CPV =
1645 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1646 ARMPCLabelIndex, 0);
1647 // Get the address of the callee into a register
1648 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1649 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1650 Callee = DAG.getLoad(getPointerTy(), dl,
1651 DAG.getEntryNode(), CPAddr,
1652 MachinePointerInfo::getConstantPool(),
1653 false, false, false, 0);
1655 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1656 const GlobalValue *GV = G->getGlobal();
1658 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
1659 bool isStub = (isExt && Subtarget->isTargetDarwin()) &&
1660 getTargetMachine().getRelocationModel() != Reloc::Static;
1661 isARMFunc = !Subtarget->isThumb() || isStub;
1662 // ARM call to a local ARM function is predicable.
1663 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
1664 // tBX takes a register source operand.
1665 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
1666 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
1667 ARMConstantPoolValue *CPV =
1668 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 4);
1669 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1670 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1671 Callee = DAG.getLoad(getPointerTy(), dl,
1672 DAG.getEntryNode(), CPAddr,
1673 MachinePointerInfo::getConstantPool(),
1674 false, false, false, 0);
1675 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1676 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
1677 getPointerTy(), Callee, PICLabel);
1679 // On ELF targets for PIC code, direct calls should go through the PLT
1680 unsigned OpFlags = 0;
1681 if (Subtarget->isTargetELF() &&
1682 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1683 OpFlags = ARMII::MO_PLT;
1684 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1686 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1688 bool isStub = Subtarget->isTargetDarwin() &&
1689 getTargetMachine().getRelocationModel() != Reloc::Static;
1690 isARMFunc = !Subtarget->isThumb() || isStub;
1691 // tBX takes a register source operand.
1692 const char *Sym = S->getSymbol();
1693 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
1694 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
1695 ARMConstantPoolValue *CPV =
1696 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1697 ARMPCLabelIndex, 4);
1698 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1699 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1700 Callee = DAG.getLoad(getPointerTy(), dl,
1701 DAG.getEntryNode(), CPAddr,
1702 MachinePointerInfo::getConstantPool(),
1703 false, false, false, 0);
1704 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
1705 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
1706 getPointerTy(), Callee, PICLabel);
1708 unsigned OpFlags = 0;
1709 // On ELF targets for PIC code, direct calls should go through the PLT
1710 if (Subtarget->isTargetELF() &&
1711 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1712 OpFlags = ARMII::MO_PLT;
1713 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1717 // FIXME: handle tail calls differently.
1719 bool HasMinSizeAttr = MF.getFunction()->getAttributes().
1720 hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
1721 if (Subtarget->isThumb()) {
1722 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
1723 CallOpc = ARMISD::CALL_NOLINK;
1725 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1727 if (!isDirect && !Subtarget->hasV5TOps())
1728 CallOpc = ARMISD::CALL_NOLINK;
1729 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
1730 // Emit regular call when code size is the priority
1732 // "mov lr, pc; b _foo" to avoid confusing the RSP
1733 CallOpc = ARMISD::CALL_NOLINK;
1735 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
1738 std::vector<SDValue> Ops;
1739 Ops.push_back(Chain);
1740 Ops.push_back(Callee);
1742 // Add argument registers to the end of the list so that they are known live
1744 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1745 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1746 RegsToPass[i].second.getValueType()));
1748 // Add a register mask operand representing the call-preserved registers.
1749 const uint32_t *Mask;
1750 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1751 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1753 // For 'this' returns, use the R0-preserving mask if applicable
1754 Mask = ARI->getThisReturnPreservedMask(CallConv);
1756 // Set isThisReturn to false if the calling convention is not one that
1757 // allows 'returned' to be modeled in this way, so LowerCallResult does
1758 // not try to pass 'this' straight through
1759 isThisReturn = false;
1760 Mask = ARI->getCallPreservedMask(CallConv);
1763 Mask = ARI->getCallPreservedMask(CallConv);
1765 assert(Mask && "Missing call preserved mask for calling convention");
1766 Ops.push_back(DAG.getRegisterMask(Mask));
1768 if (InFlag.getNode())
1769 Ops.push_back(InFlag);
1771 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1773 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
1775 // Returns a chain and a flag for retval copy to use.
1776 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
1777 InFlag = Chain.getValue(1);
1779 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1780 DAG.getIntPtrConstant(0, true), InFlag, dl);
1782 InFlag = Chain.getValue(1);
1784 // Handle result values, copying them out of physregs into vregs that we
1786 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
1787 InVals, isThisReturn,
1788 isThisReturn ? OutVals[0] : SDValue());
1791 /// HandleByVal - Every parameter *after* a byval parameter is passed
1792 /// on the stack. Remember the next parameter register to allocate,
1793 /// and then confiscate the rest of the parameter registers to insure
1796 ARMTargetLowering::HandleByVal(
1797 CCState *State, unsigned &size, unsigned Align) const {
1798 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1799 assert((State->getCallOrPrologue() == Prologue ||
1800 State->getCallOrPrologue() == Call) &&
1801 "unhandled ParmContext");
1803 // For in-prologue parameters handling, we also introduce stack offset
1804 // for byval registers: see CallingConvLower.cpp, CCState::HandleByVal.
1805 // This behaviour outsides AAPCS rules (5.5 Parameters Passing) of how
1806 // NSAA should be evaluted (NSAA means "next stacked argument address").
1807 // So: NextStackOffset = NSAAOffset + SizeOfByValParamsStoredInRegs.
1808 // Then: NSAAOffset = NextStackOffset - SizeOfByValParamsStoredInRegs.
1809 unsigned NSAAOffset = State->getNextStackOffset();
1810 if (State->getCallOrPrologue() != Call) {
1811 for (unsigned i = 0, e = State->getInRegsParamsCount(); i != e; ++i) {
1813 State->getInRegsParamInfo(i, RB, RE);
1814 assert(NSAAOffset >= (RE-RB)*4 &&
1815 "Stack offset for byval regs doesn't introduced anymore?");
1816 NSAAOffset -= (RE-RB)*4;
1819 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
1820 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1821 unsigned AlignInRegs = Align / 4;
1822 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1823 for (unsigned i = 0; i < Waste; ++i)
1824 reg = State->AllocateReg(GPRArgRegs, 4);
1827 unsigned excess = 4 * (ARM::R4 - reg);
1829 // Special case when NSAA != SP and parameter size greater than size of
1830 // all remained GPR regs. In that case we can't split parameter, we must
1831 // send it to stack. We also must set NCRN to R4, so waste all
1832 // remained registers.
1833 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1834 while (State->AllocateReg(GPRArgRegs, 4))
1839 // First register for byval parameter is the first register that wasn't
1840 // allocated before this method call, so it would be "reg".
1841 // If parameter is small enough to be saved in range [reg, r4), then
1842 // the end (first after last) register would be reg + param-size-in-regs,
1843 // else parameter would be splitted between registers and stack,
1844 // end register would be r4 in this case.
1845 unsigned ByValRegBegin = reg;
1846 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
1847 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1848 // Note, first register is allocated in the beginning of function already,
1849 // allocate remained amount of registers we need.
1850 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1851 State->AllocateReg(GPRArgRegs, 4);
1852 // At a call site, a byval parameter that is split between
1853 // registers and memory needs its size truncated here. In a
1854 // function prologue, such byval parameters are reassembled in
1855 // memory, and are not truncated.
1856 if (State->getCallOrPrologue() == Call) {
1857 // Make remained size equal to 0 in case, when
1858 // the whole structure may be stored into registers.
1868 /// MatchingStackOffset - Return true if the given stack call argument is
1869 /// already available in the same position (relatively) of the caller's
1870 /// incoming argument stack.
1872 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1873 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
1874 const TargetInstrInfo *TII) {
1875 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1877 if (Arg.getOpcode() == ISD::CopyFromReg) {
1878 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
1879 if (!TargetRegisterInfo::isVirtualRegister(VR))
1881 MachineInstr *Def = MRI->getVRegDef(VR);
1884 if (!Flags.isByVal()) {
1885 if (!TII->isLoadFromStackSlot(Def, FI))
1890 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1891 if (Flags.isByVal())
1892 // ByVal argument is passed in as a pointer but it's now being
1893 // dereferenced. e.g.
1894 // define @foo(%struct.X* %A) {
1895 // tail call @bar(%struct.X* byval %A)
1898 SDValue Ptr = Ld->getBasePtr();
1899 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1902 FI = FINode->getIndex();
1906 assert(FI != INT_MAX);
1907 if (!MFI->isFixedObjectIndex(FI))
1909 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1912 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
1913 /// for tail call optimization. Targets which want to do tail call
1914 /// optimization should implement this function.
1916 ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1917 CallingConv::ID CalleeCC,
1919 bool isCalleeStructRet,
1920 bool isCallerStructRet,
1921 const SmallVectorImpl<ISD::OutputArg> &Outs,
1922 const SmallVectorImpl<SDValue> &OutVals,
1923 const SmallVectorImpl<ISD::InputArg> &Ins,
1924 SelectionDAG& DAG) const {
1925 const Function *CallerF = DAG.getMachineFunction().getFunction();
1926 CallingConv::ID CallerCC = CallerF->getCallingConv();
1927 bool CCMatch = CallerCC == CalleeCC;
1929 // Look for obvious safe cases to perform tail call optimization that do not
1930 // require ABI changes. This is what gcc calls sibcall.
1932 // Do not sibcall optimize vararg calls unless the call site is not passing
1934 if (isVarArg && !Outs.empty())
1937 // Also avoid sibcall optimization if either caller or callee uses struct
1938 // return semantics.
1939 if (isCalleeStructRet || isCallerStructRet)
1942 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
1943 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1944 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1945 // support in the assembler and linker to be used. This would need to be
1946 // fixed to fully support tail calls in Thumb1.
1948 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1949 // LR. This means if we need to reload LR, it takes an extra instructions,
1950 // which outweighs the value of the tail call; but here we don't know yet
1951 // whether LR is going to be used. Probably the right approach is to
1952 // generate the tail call here and turn it back into CALL/RET in
1953 // emitEpilogue if LR is used.
1955 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1956 // but we need to make sure there are enough registers; the only valid
1957 // registers are the 4 used for parameters. We don't currently do this
1959 if (Subtarget->isThumb1Only())
1962 // If the calling conventions do not match, then we'd better make sure the
1963 // results are returned in the same way as what the caller expects.
1965 SmallVector<CCValAssign, 16> RVLocs1;
1966 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1967 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
1968 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1970 SmallVector<CCValAssign, 16> RVLocs2;
1971 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1972 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
1973 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1975 if (RVLocs1.size() != RVLocs2.size())
1977 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1978 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1980 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1982 if (RVLocs1[i].isRegLoc()) {
1983 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1986 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1992 // If Caller's vararg or byval argument has been split between registers and
1993 // stack, do not perform tail call, since part of the argument is in caller's
1995 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1996 getInfo<ARMFunctionInfo>();
1997 if (AFI_Caller->getArgRegsSaveSize())
2000 // If the callee takes no arguments then go on to check the results of the
2002 if (!Outs.empty()) {
2003 // Check if stack adjustment is needed. For now, do not do this if any
2004 // argument is passed on the stack.
2005 SmallVector<CCValAssign, 16> ArgLocs;
2006 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2007 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
2008 CCInfo.AnalyzeCallOperands(Outs,
2009 CCAssignFnForNode(CalleeCC, false, isVarArg));
2010 if (CCInfo.getNextStackOffset()) {
2011 MachineFunction &MF = DAG.getMachineFunction();
2013 // Check if the arguments are already laid out in the right way as
2014 // the caller's fixed stack objects.
2015 MachineFrameInfo *MFI = MF.getFrameInfo();
2016 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2017 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
2018 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2020 ++i, ++realArgIdx) {
2021 CCValAssign &VA = ArgLocs[i];
2022 EVT RegVT = VA.getLocVT();
2023 SDValue Arg = OutVals[realArgIdx];
2024 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
2025 if (VA.getLocInfo() == CCValAssign::Indirect)
2027 if (VA.needsCustom()) {
2028 // f64 and vector types are split into multiple registers or
2029 // register/stack-slot combinations. The types will not match
2030 // the registers; give up on memory f64 refs until we figure
2031 // out what to do about this.
2034 if (!ArgLocs[++i].isRegLoc())
2036 if (RegVT == MVT::v2f64) {
2037 if (!ArgLocs[++i].isRegLoc())
2039 if (!ArgLocs[++i].isRegLoc())
2042 } else if (!VA.isRegLoc()) {
2043 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2055 ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2056 MachineFunction &MF, bool isVarArg,
2057 const SmallVectorImpl<ISD::OutputArg> &Outs,
2058 LLVMContext &Context) const {
2059 SmallVector<CCValAssign, 16> RVLocs;
2060 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
2061 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2066 ARMTargetLowering::LowerReturn(SDValue Chain,
2067 CallingConv::ID CallConv, bool isVarArg,
2068 const SmallVectorImpl<ISD::OutputArg> &Outs,
2069 const SmallVectorImpl<SDValue> &OutVals,
2070 SDLoc dl, SelectionDAG &DAG) const {
2072 // CCValAssign - represent the assignment of the return value to a location.
2073 SmallVector<CCValAssign, 16> RVLocs;
2075 // CCState - Info about the registers and stack slots.
2076 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2077 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
2079 // Analyze outgoing return values.
2080 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2084 SmallVector<SDValue, 4> RetOps;
2085 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
2087 // Copy the result values into the output registers.
2088 for (unsigned i = 0, realRVLocIdx = 0;
2090 ++i, ++realRVLocIdx) {
2091 CCValAssign &VA = RVLocs[i];
2092 assert(VA.isRegLoc() && "Can only return in registers!");
2094 SDValue Arg = OutVals[realRVLocIdx];
2096 switch (VA.getLocInfo()) {
2097 default: llvm_unreachable("Unknown loc info!");
2098 case CCValAssign::Full: break;
2099 case CCValAssign::BCvt:
2100 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
2104 if (VA.needsCustom()) {
2105 if (VA.getLocVT() == MVT::v2f64) {
2106 // Extract the first half and return it in two registers.
2107 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2108 DAG.getConstant(0, MVT::i32));
2109 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
2110 DAG.getVTList(MVT::i32, MVT::i32), Half);
2112 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), HalfGPRs, Flag);
2113 Flag = Chain.getValue(1);
2114 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2115 VA = RVLocs[++i]; // skip ahead to next loc
2116 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2117 HalfGPRs.getValue(1), Flag);
2118 Flag = Chain.getValue(1);
2119 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2120 VA = RVLocs[++i]; // skip ahead to next loc
2122 // Extract the 2nd half and fall through to handle it as an f64 value.
2123 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2124 DAG.getConstant(1, MVT::i32));
2126 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2128 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
2129 DAG.getVTList(MVT::i32, MVT::i32), &Arg, 1);
2130 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd, Flag);
2131 Flag = Chain.getValue(1);
2132 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2133 VA = RVLocs[++i]; // skip ahead to next loc
2134 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), fmrrd.getValue(1),
2137 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2139 // Guarantee that all emitted copies are
2140 // stuck together, avoiding something bad.
2141 Flag = Chain.getValue(1);
2142 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2145 // Update chain and glue.
2148 RetOps.push_back(Flag);
2150 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other,
2151 RetOps.data(), RetOps.size());
2154 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
2155 if (N->getNumValues() != 1)
2157 if (!N->hasNUsesOfValue(1, 0))
2160 SDValue TCChain = Chain;
2161 SDNode *Copy = *N->use_begin();
2162 if (Copy->getOpcode() == ISD::CopyToReg) {
2163 // If the copy has a glue operand, we conservatively assume it isn't safe to
2164 // perform a tail call.
2165 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2167 TCChain = Copy->getOperand(0);
2168 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2169 SDNode *VMov = Copy;
2170 // f64 returned in a pair of GPRs.
2171 SmallPtrSet<SDNode*, 2> Copies;
2172 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2174 if (UI->getOpcode() != ISD::CopyToReg)
2178 if (Copies.size() > 2)
2181 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2183 SDValue UseChain = UI->getOperand(0);
2184 if (Copies.count(UseChain.getNode()))
2191 } else if (Copy->getOpcode() == ISD::BITCAST) {
2192 // f32 returned in a single GPR.
2193 if (!Copy->hasOneUse())
2195 Copy = *Copy->use_begin();
2196 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
2198 TCChain = Copy->getOperand(0);
2203 bool HasRet = false;
2204 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2206 if (UI->getOpcode() != ARMISD::RET_FLAG)
2218 bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
2219 if (!EnableARMTailCalls && !Subtarget->supportsTailCall())
2222 if (!CI->isTailCall())
2225 return !Subtarget->isThumb1Only();
2228 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2229 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2230 // one of the above mentioned nodes. It has to be wrapped because otherwise
2231 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2232 // be used to form addressing mode. These wrapped nodes will be selected
2234 static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
2235 EVT PtrVT = Op.getValueType();
2236 // FIXME there is no actual debug info here
2238 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2240 if (CP->isMachineConstantPoolEntry())
2241 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2242 CP->getAlignment());
2244 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2245 CP->getAlignment());
2246 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
2249 unsigned ARMTargetLowering::getJumpTableEncoding() const {
2250 return MachineJumpTableInfo::EK_Inline;
2253 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2254 SelectionDAG &DAG) const {
2255 MachineFunction &MF = DAG.getMachineFunction();
2256 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2257 unsigned ARMPCLabelIndex = 0;
2259 EVT PtrVT = getPointerTy();
2260 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
2261 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2263 if (RelocM == Reloc::Static) {
2264 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2266 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2267 ARMPCLabelIndex = AFI->createPICLabelUId();
2268 ARMConstantPoolValue *CPV =
2269 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2270 ARMCP::CPBlockAddress, PCAdj);
2271 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2273 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2274 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
2275 MachinePointerInfo::getConstantPool(),
2276 false, false, false, 0);
2277 if (RelocM == Reloc::Static)
2279 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
2280 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
2283 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
2285 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
2286 SelectionDAG &DAG) const {
2288 EVT PtrVT = getPointerTy();
2289 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2290 MachineFunction &MF = DAG.getMachineFunction();
2291 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2292 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2293 ARMConstantPoolValue *CPV =
2294 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2295 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
2296 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2297 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
2298 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
2299 MachinePointerInfo::getConstantPool(),
2300 false, false, false, 0);
2301 SDValue Chain = Argument.getValue(1);
2303 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
2304 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
2306 // call __tls_get_addr.
2309 Entry.Node = Argument;
2310 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
2311 Args.push_back(Entry);
2312 // FIXME: is there useful debug info available here?
2313 TargetLowering::CallLoweringInfo CLI(Chain,
2314 (Type *) Type::getInt32Ty(*DAG.getContext()),
2315 false, false, false, false,
2316 0, CallingConv::C, /*isTailCall=*/false,
2317 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
2318 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
2319 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
2320 return CallResult.first;
2323 // Lower ISD::GlobalTLSAddress using the "initial exec" or
2324 // "local exec" model.
2326 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
2328 TLSModel::Model model) const {
2329 const GlobalValue *GV = GA->getGlobal();
2332 SDValue Chain = DAG.getEntryNode();
2333 EVT PtrVT = getPointerTy();
2334 // Get the Thread Pointer
2335 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2337 if (model == TLSModel::InitialExec) {
2338 MachineFunction &MF = DAG.getMachineFunction();
2339 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2340 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2341 // Initial exec model.
2342 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2343 ARMConstantPoolValue *CPV =
2344 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2345 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2347 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2348 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
2349 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
2350 MachinePointerInfo::getConstantPool(),
2351 false, false, false, 0);
2352 Chain = Offset.getValue(1);
2354 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
2355 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
2357 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
2358 MachinePointerInfo::getConstantPool(),
2359 false, false, false, 0);
2362 assert(model == TLSModel::LocalExec);
2363 ARMConstantPoolValue *CPV =
2364 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
2365 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2366 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
2367 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
2368 MachinePointerInfo::getConstantPool(),
2369 false, false, false, 0);
2372 // The address of the thread local variable is the add of the thread
2373 // pointer with the offset of the variable.
2374 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
2378 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
2379 // TODO: implement the "local dynamic" model
2380 assert(Subtarget->isTargetELF() &&
2381 "TLS not implemented for non-ELF targets");
2382 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2384 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2387 case TLSModel::GeneralDynamic:
2388 case TLSModel::LocalDynamic:
2389 return LowerToTLSGeneralDynamicModel(GA, DAG);
2390 case TLSModel::InitialExec:
2391 case TLSModel::LocalExec:
2392 return LowerToTLSExecModels(GA, DAG, model);
2394 llvm_unreachable("bogus TLS model");
2397 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
2398 SelectionDAG &DAG) const {
2399 EVT PtrVT = getPointerTy();
2401 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2402 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2403 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
2404 ARMConstantPoolValue *CPV =
2405 ARMConstantPoolConstant::Create(GV,
2406 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
2407 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2408 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2409 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
2411 MachinePointerInfo::getConstantPool(),
2412 false, false, false, 0);
2413 SDValue Chain = Result.getValue(1);
2414 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2415 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
2417 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
2418 MachinePointerInfo::getGOT(),
2419 false, false, false, 0);
2423 // If we have T2 ops, we can materialize the address directly via movt/movw
2424 // pair. This is always cheaper.
2425 if (Subtarget->useMovt()) {
2427 // FIXME: Once remat is capable of dealing with instructions with register
2428 // operands, expand this into two nodes.
2429 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2430 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2432 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2433 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2434 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2435 MachinePointerInfo::getConstantPool(),
2436 false, false, false, 0);
2440 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
2441 SelectionDAG &DAG) const {
2442 EVT PtrVT = getPointerTy();
2444 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2445 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2447 // FIXME: Enable this for static codegen when tool issues are fixed. Also
2448 // update ARMFastISel::ARMMaterializeGV.
2449 if (Subtarget->useMovt() && RelocM != Reloc::Static) {
2451 // FIXME: Once remat is capable of dealing with instructions with register
2452 // operands, expand this into two nodes.
2453 if (RelocM == Reloc::Static)
2454 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2455 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2457 unsigned Wrapper = (RelocM == Reloc::PIC_)
2458 ? ARMISD::WrapperPIC : ARMISD::WrapperDYN;
2459 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT,
2460 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
2461 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2462 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2463 MachinePointerInfo::getGOT(),
2464 false, false, false, 0);
2468 unsigned ARMPCLabelIndex = 0;
2470 if (RelocM == Reloc::Static) {
2471 CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2473 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
2474 ARMPCLabelIndex = AFI->createPICLabelUId();
2475 unsigned PCAdj = (RelocM != Reloc::PIC_) ? 0 : (Subtarget->isThumb()?4:8);
2476 ARMConstantPoolValue *CPV =
2477 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue,
2479 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2481 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2483 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2484 MachinePointerInfo::getConstantPool(),
2485 false, false, false, 0);
2486 SDValue Chain = Result.getValue(1);
2488 if (RelocM == Reloc::PIC_) {
2489 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
2490 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2493 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
2494 Result = DAG.getLoad(PtrVT, dl, Chain, Result, MachinePointerInfo::getGOT(),
2495 false, false, false, 0);
2500 SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
2501 SelectionDAG &DAG) const {
2502 assert(Subtarget->isTargetELF() &&
2503 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
2504 MachineFunction &MF = DAG.getMachineFunction();
2505 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2506 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2507 EVT PtrVT = getPointerTy();
2509 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2510 ARMConstantPoolValue *CPV =
2511 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2512 ARMPCLabelIndex, PCAdj);
2513 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2514 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2515 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2516 MachinePointerInfo::getConstantPool(),
2517 false, false, false, 0);
2518 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
2519 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2523 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
2525 SDValue Val = DAG.getConstant(0, MVT::i32);
2526 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2527 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
2528 Op.getOperand(1), Val);
2532 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
2534 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2535 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2539 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
2540 const ARMSubtarget *Subtarget) const {
2541 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2544 default: return SDValue(); // Don't custom lower most intrinsics.
2545 case Intrinsic::arm_thread_pointer: {
2546 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2547 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2549 case Intrinsic::eh_sjlj_lsda: {
2550 MachineFunction &MF = DAG.getMachineFunction();
2551 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2552 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2553 EVT PtrVT = getPointerTy();
2554 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2556 unsigned PCAdj = (RelocM != Reloc::PIC_)
2557 ? 0 : (Subtarget->isThumb() ? 4 : 8);
2558 ARMConstantPoolValue *CPV =
2559 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2560 ARMCP::CPLSDA, PCAdj);
2561 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2562 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2564 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2565 MachinePointerInfo::getConstantPool(),
2566 false, false, false, 0);
2568 if (RelocM == Reloc::PIC_) {
2569 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
2570 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2574 case Intrinsic::arm_neon_vmulls:
2575 case Intrinsic::arm_neon_vmullu: {
2576 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2577 ? ARMISD::VMULLs : ARMISD::VMULLu;
2578 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2579 Op.getOperand(1), Op.getOperand(2));
2584 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2585 const ARMSubtarget *Subtarget) {
2586 // FIXME: handle "fence singlethread" more efficiently.
2588 if (!Subtarget->hasDataBarrier()) {
2589 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2590 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2592 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
2593 "Unexpected ISD::MEMBARRIER encountered. Should be libcall!");
2594 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
2595 DAG.getConstant(0, MVT::i32));
2598 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2599 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
2600 unsigned Domain = ARM_MB::ISH;
2601 if (Subtarget->isSwift() && Ord == Release) {
2602 // Swift happens to implement ISHST barriers in a way that's compatible with
2603 // Release semantics but weaker than ISH so we'd be fools not to use
2604 // it. Beware: other processors probably don't!
2605 Domain = ARM_MB::ISHST;
2608 return DAG.getNode(ARMISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0),
2609 DAG.getConstant(Domain, MVT::i32));
2612 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2613 const ARMSubtarget *Subtarget) {
2614 // ARM pre v5TE and Thumb1 does not have preload instructions.
2615 if (!(Subtarget->isThumb2() ||
2616 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2617 // Just preserve the chain.
2618 return Op.getOperand(0);
2621 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2623 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2624 // ARMv7 with MP extension has PLDW.
2625 return Op.getOperand(0);
2627 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2628 if (Subtarget->isThumb()) {
2630 isRead = ~isRead & 1;
2631 isData = ~isData & 1;
2634 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
2635 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2636 DAG.getConstant(isData, MVT::i32));
2639 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2640 MachineFunction &MF = DAG.getMachineFunction();
2641 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2643 // vastart just stores the address of the VarArgsFrameIndex slot into the
2644 // memory location argument.
2646 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2647 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2648 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2649 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2650 MachinePointerInfo(SV), false, false, 0);
2654 ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2655 SDValue &Root, SelectionDAG &DAG,
2657 MachineFunction &MF = DAG.getMachineFunction();
2658 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2660 const TargetRegisterClass *RC;
2661 if (AFI->isThumb1OnlyFunction())
2662 RC = &ARM::tGPRRegClass;
2664 RC = &ARM::GPRRegClass;
2666 // Transform the arguments stored in physical registers into virtual ones.
2667 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
2668 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
2671 if (NextVA.isMemLoc()) {
2672 MachineFrameInfo *MFI = MF.getFrameInfo();
2673 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
2675 // Create load node to retrieve arguments from the stack.
2676 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2677 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
2678 MachinePointerInfo::getFixedStack(FI),
2679 false, false, false, 0);
2681 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
2682 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
2685 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
2689 ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
2690 unsigned InRegsParamRecordIdx,
2692 unsigned &ArgRegsSize,
2693 unsigned &ArgRegsSaveSize)
2696 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2697 unsigned RBegin, REnd;
2698 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2699 NumGPRs = REnd - RBegin;
2701 unsigned int firstUnalloced;
2702 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2703 sizeof(GPRArgRegs) /
2704 sizeof(GPRArgRegs[0]));
2705 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2708 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
2709 ArgRegsSize = NumGPRs * 4;
2711 // If parameter is split between stack and GPRs...
2712 if (NumGPRs && Align == 8 &&
2713 (ArgRegsSize < ArgSize ||
2714 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
2715 // Add padding for part of param recovered from GPRs, so
2716 // its last byte must be at address K*8 - 1.
2717 // We need to do it, since remained (stack) part of parameter has
2718 // stack alignment, and we need to "attach" "GPRs head" without gaps
2721 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2722 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2724 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2726 ((ArgRegsSize + AFI->getArgRegsSaveSize() + Align - 1) & ~(Align-1)) -
2727 (ArgRegsSize + AFI->getArgRegsSaveSize());
2728 ArgRegsSaveSize = ArgRegsSize + Padding;
2730 // We don't need to extend regs save size for byval parameters if they
2731 // are passed via GPRs only.
2732 ArgRegsSaveSize = ArgRegsSize;
2735 // The remaining GPRs hold either the beginning of variable-argument
2736 // data, or the beginning of an aggregate passed by value (usually
2737 // byval). Either way, we allocate stack slots adjacent to the data
2738 // provided by our caller, and store the unallocated registers there.
2739 // If this is a variadic function, the va_list pointer will begin with
2740 // these values; otherwise, this reassembles a (byval) structure that
2741 // was split between registers and memory.
2742 // Return: The frame index registers were stored into.
2744 ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
2745 SDLoc dl, SDValue &Chain,
2746 const Value *OrigArg,
2747 unsigned InRegsParamRecordIdx,
2748 unsigned OffsetFromOrigArg,
2751 bool ForceMutable) const {
2753 // Currently, two use-cases possible:
2754 // Case #1. Non var-args function, and we meet first byval parameter.
2755 // Setup first unallocated register as first byval register;
2756 // eat all remained registers
2757 // (these two actions are performed by HandleByVal method).
2758 // Then, here, we initialize stack frame with
2759 // "store-reg" instructions.
2760 // Case #2. Var-args function, that doesn't contain byval parameters.
2761 // The same: eat all remained unallocated registers,
2762 // initialize stack frame.
2764 MachineFunction &MF = DAG.getMachineFunction();
2765 MachineFrameInfo *MFI = MF.getFrameInfo();
2766 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2767 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2768 unsigned RBegin, REnd;
2769 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2770 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2771 firstRegToSaveIndex = RBegin - ARM::R0;
2772 lastRegToSaveIndex = REnd - ARM::R0;
2774 firstRegToSaveIndex = CCInfo.getFirstUnallocated
2775 (GPRArgRegs, array_lengthof(GPRArgRegs));
2776 lastRegToSaveIndex = 4;
2779 unsigned ArgRegsSize, ArgRegsSaveSize;
2780 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2781 ArgRegsSize, ArgRegsSaveSize);
2783 // Store any by-val regs to their spots on the stack so that they may be
2784 // loaded by deferencing the result of formal parameter pointer or va_next.
2785 // Note: once stack area for byval/varargs registers
2786 // was initialized, it can't be initialized again.
2787 if (ArgRegsSaveSize) {
2789 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2792 assert(AFI->getStoredByValParamsPadding() == 0 &&
2793 "The only parameter may be padded.");
2794 AFI->setStoredByValParamsPadding(Padding);
2797 int FrameIndex = MFI->CreateFixedObject(
2799 Padding + ArgOffset,
2801 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
2803 SmallVector<SDValue, 4> MemOps;
2804 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2805 ++firstRegToSaveIndex, ++i) {
2806 const TargetRegisterClass *RC;
2807 if (AFI->isThumb1OnlyFunction())
2808 RC = &ARM::tGPRRegClass;
2810 RC = &ARM::GPRRegClass;
2812 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2813 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2815 DAG.getStore(Val.getValue(1), dl, Val, FIN,
2816 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
2818 MemOps.push_back(Store);
2819 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2820 DAG.getConstant(4, getPointerTy()));
2823 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2825 if (!MemOps.empty())
2826 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2827 &MemOps[0], MemOps.size());
2830 // This will point to the next argument passed via stack.
2831 return MFI->CreateFixedObject(
2832 4, AFI->getStoredByValParamsPadding() + ArgOffset, !ForceMutable);
2835 // Setup stack frame, the va_list pointer will start from.
2837 ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
2838 SDLoc dl, SDValue &Chain,
2840 bool ForceMutable) const {
2841 MachineFunction &MF = DAG.getMachineFunction();
2842 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2844 // Try to store any remaining integer argument regs
2845 // to their spots on the stack so that they may be loaded by deferencing
2846 // the result of va_next.
2847 // If there is no regs to be stored, just point address after last
2848 // argument passed via stack.
2850 StoreByValRegs(CCInfo, DAG, dl, Chain, 0, CCInfo.getInRegsParamsCount(),
2851 0, ArgOffset, 0, ForceMutable);
2853 AFI->setVarArgsFrameIndex(FrameIndex);
2857 ARMTargetLowering::LowerFormalArguments(SDValue Chain,
2858 CallingConv::ID CallConv, bool isVarArg,
2859 const SmallVectorImpl<ISD::InputArg>
2861 SDLoc dl, SelectionDAG &DAG,
2862 SmallVectorImpl<SDValue> &InVals)
2864 MachineFunction &MF = DAG.getMachineFunction();
2865 MachineFrameInfo *MFI = MF.getFrameInfo();
2867 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2869 // Assign locations to all of the incoming arguments.
2870 SmallVector<CCValAssign, 16> ArgLocs;
2871 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2872 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
2873 CCInfo.AnalyzeFormalArguments(Ins,
2874 CCAssignFnForNode(CallConv, /* Return*/ false,
2877 SmallVector<SDValue, 16> ArgValues;
2878 int lastInsIndex = -1;
2880 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2881 unsigned CurArgIdx = 0;
2883 // Initially ArgRegsSaveSize is zero.
2884 // Then we increase this value each time we meet byval parameter.
2885 // We also increase this value in case of varargs function.
2886 AFI->setArgRegsSaveSize(0);
2888 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2889 CCValAssign &VA = ArgLocs[i];
2890 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2891 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
2892 // Arguments stored in registers.
2893 if (VA.isRegLoc()) {
2894 EVT RegVT = VA.getLocVT();
2896 if (VA.needsCustom()) {
2897 // f64 and vector types are split up into multiple registers or
2898 // combinations of registers and stack slots.
2899 if (VA.getLocVT() == MVT::v2f64) {
2900 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
2902 VA = ArgLocs[++i]; // skip ahead to next loc
2904 if (VA.isMemLoc()) {
2905 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
2906 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2907 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
2908 MachinePointerInfo::getFixedStack(FI),
2909 false, false, false, 0);
2911 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2914 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2915 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
2916 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
2917 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
2918 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2920 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
2923 const TargetRegisterClass *RC;
2925 if (RegVT == MVT::f32)
2926 RC = &ARM::SPRRegClass;
2927 else if (RegVT == MVT::f64)
2928 RC = &ARM::DPRRegClass;
2929 else if (RegVT == MVT::v2f64)
2930 RC = &ARM::QPRRegClass;
2931 else if (RegVT == MVT::i32)
2932 RC = AFI->isThumb1OnlyFunction() ?
2933 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2934 (const TargetRegisterClass*)&ARM::GPRRegClass;
2936 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
2938 // Transform the arguments in physical registers into virtual ones.
2939 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
2940 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
2943 // If this is an 8 or 16-bit value, it is really passed promoted
2944 // to 32 bits. Insert an assert[sz]ext to capture this, then
2945 // truncate to the right size.
2946 switch (VA.getLocInfo()) {
2947 default: llvm_unreachable("Unknown loc info!");
2948 case CCValAssign::Full: break;
2949 case CCValAssign::BCvt:
2950 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
2952 case CCValAssign::SExt:
2953 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2954 DAG.getValueType(VA.getValVT()));
2955 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2957 case CCValAssign::ZExt:
2958 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2959 DAG.getValueType(VA.getValVT()));
2960 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2964 InVals.push_back(ArgValue);
2966 } else { // VA.isRegLoc()
2969 assert(VA.isMemLoc());
2970 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
2972 int index = ArgLocs[i].getValNo();
2974 // Some Ins[] entries become multiple ArgLoc[] entries.
2975 // Process them only once.
2976 if (index != lastInsIndex)
2978 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2979 // FIXME: For now, all byval parameter objects are marked mutable.
2980 // This can be changed with more analysis.
2981 // In case of tail call optimization mark all arguments mutable.
2982 // Since they could be overwritten by lowering of arguments in case of
2984 if (Flags.isByVal()) {
2985 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
2986 int FrameIndex = StoreByValRegs(
2987 CCInfo, DAG, dl, Chain, CurOrigArg,
2989 Ins[VA.getValNo()].PartOffset,
2990 VA.getLocMemOffset(),
2991 Flags.getByValSize(),
2992 true /*force mutable frames*/);
2993 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
2994 CCInfo.nextInRegsParam();
2996 unsigned FIOffset = VA.getLocMemOffset() +
2997 AFI->getStoredByValParamsPadding();
2998 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
3001 // Create load nodes to retrieve arguments from the stack.
3002 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3003 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3004 MachinePointerInfo::getFixedStack(FI),
3005 false, false, false, 0));
3007 lastInsIndex = index;
3014 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
3015 CCInfo.getNextStackOffset());
3020 /// isFloatingPointZero - Return true if this is +0.0.
3021 static bool isFloatingPointZero(SDValue Op) {
3022 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
3023 return CFP->getValueAPF().isPosZero();
3024 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
3025 // Maybe this has already been legalized into the constant pool?
3026 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
3027 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
3028 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
3029 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
3030 return CFP->getValueAPF().isPosZero();
3036 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3037 /// the given operands.
3039 ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
3040 SDValue &ARMcc, SelectionDAG &DAG,
3042 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
3043 unsigned C = RHSC->getZExtValue();
3044 if (!isLegalICmpImmediate(C)) {
3045 // Constant does not fit, try adjusting it by one?
3050 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
3051 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
3052 RHS = DAG.getConstant(C-1, MVT::i32);
3057 if (C != 0 && isLegalICmpImmediate(C-1)) {
3058 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
3059 RHS = DAG.getConstant(C-1, MVT::i32);
3064 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
3065 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
3066 RHS = DAG.getConstant(C+1, MVT::i32);
3071 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
3072 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
3073 RHS = DAG.getConstant(C+1, MVT::i32);
3080 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3081 ARMISD::NodeType CompareType;
3084 CompareType = ARMISD::CMP;
3089 CompareType = ARMISD::CMPZ;
3092 ARMcc = DAG.getConstant(CondCode, MVT::i32);
3093 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
3096 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
3098 ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
3101 if (!isFloatingPointZero(RHS))
3102 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
3104 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3105 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
3108 /// duplicateCmp - Glue values can have only one use, so this function
3109 /// duplicates a comparison node.
3111 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3112 unsigned Opc = Cmp.getOpcode();
3114 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3115 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3117 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3118 Cmp = Cmp.getOperand(0);
3119 Opc = Cmp.getOpcode();
3120 if (Opc == ARMISD::CMPFP)
3121 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3123 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3124 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3126 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3129 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3130 SDValue Cond = Op.getOperand(0);
3131 SDValue SelectTrue = Op.getOperand(1);
3132 SDValue SelectFalse = Op.getOperand(2);
3137 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3138 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3140 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3141 const ConstantSDNode *CMOVTrue =
3142 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3143 const ConstantSDNode *CMOVFalse =
3144 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3146 if (CMOVTrue && CMOVFalse) {
3147 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3148 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3152 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3154 False = SelectFalse;
3155 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3160 if (True.getNode() && False.getNode()) {
3161 EVT VT = Op.getValueType();
3162 SDValue ARMcc = Cond.getOperand(2);
3163 SDValue CCR = Cond.getOperand(3);
3164 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
3165 assert(True.getValueType() == VT);
3166 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
3171 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3172 // undefined bits before doing a full-word comparison with zero.
3173 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3174 DAG.getConstant(1, Cond.getValueType()));
3176 return DAG.getSelectCC(dl, Cond,
3177 DAG.getConstant(0, Cond.getValueType()),
3178 SelectTrue, SelectFalse, ISD::SETNE);
3181 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
3182 EVT VT = Op.getValueType();
3183 SDValue LHS = Op.getOperand(0);
3184 SDValue RHS = Op.getOperand(1);
3185 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
3186 SDValue TrueVal = Op.getOperand(2);
3187 SDValue FalseVal = Op.getOperand(3);
3190 if (LHS.getValueType() == MVT::i32) {
3192 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3193 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3194 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,Cmp);
3197 ARMCC::CondCodes CondCode, CondCode2;
3198 FPCCToARMCC(CC, CondCode, CondCode2);
3200 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3201 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
3202 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3203 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
3205 if (CondCode2 != ARMCC::AL) {
3206 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
3207 // FIXME: Needs another CMP because flag can have but one use.
3208 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
3209 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
3210 Result, TrueVal, ARMcc2, CCR, Cmp2);
3215 /// canChangeToInt - Given the fp compare operand, return true if it is suitable
3216 /// to morph to an integer compare sequence.
3217 static bool canChangeToInt(SDValue Op, bool &SeenZero,
3218 const ARMSubtarget *Subtarget) {
3219 SDNode *N = Op.getNode();
3220 if (!N->hasOneUse())
3221 // Otherwise it requires moving the value from fp to integer registers.
3223 if (!N->getNumValues())
3225 EVT VT = Op.getValueType();
3226 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3227 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3228 // vmrs are very slow, e.g. cortex-a8.
3231 if (isFloatingPointZero(Op)) {
3235 return ISD::isNormalLoad(N);
3238 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3239 if (isFloatingPointZero(Op))
3240 return DAG.getConstant(0, MVT::i32);
3242 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
3243 return DAG.getLoad(MVT::i32, SDLoc(Op),
3244 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
3245 Ld->isVolatile(), Ld->isNonTemporal(),
3246 Ld->isInvariant(), Ld->getAlignment());
3248 llvm_unreachable("Unknown VFP cmp argument!");
3251 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3252 SDValue &RetVal1, SDValue &RetVal2) {
3253 if (isFloatingPointZero(Op)) {
3254 RetVal1 = DAG.getConstant(0, MVT::i32);
3255 RetVal2 = DAG.getConstant(0, MVT::i32);
3259 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3260 SDValue Ptr = Ld->getBasePtr();
3261 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
3262 Ld->getChain(), Ptr,
3263 Ld->getPointerInfo(),
3264 Ld->isVolatile(), Ld->isNonTemporal(),
3265 Ld->isInvariant(), Ld->getAlignment());
3267 EVT PtrType = Ptr.getValueType();
3268 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
3269 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
3270 PtrType, Ptr, DAG.getConstant(4, PtrType));
3271 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
3272 Ld->getChain(), NewPtr,
3273 Ld->getPointerInfo().getWithOffset(4),
3274 Ld->isVolatile(), Ld->isNonTemporal(),
3275 Ld->isInvariant(), NewAlign);
3279 llvm_unreachable("Unknown VFP cmp argument!");
3282 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3283 /// f32 and even f64 comparisons to integer ones.
3285 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3286 SDValue Chain = Op.getOperand(0);
3287 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3288 SDValue LHS = Op.getOperand(2);
3289 SDValue RHS = Op.getOperand(3);
3290 SDValue Dest = Op.getOperand(4);
3293 bool LHSSeenZero = false;
3294 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3295 bool RHSSeenZero = false;
3296 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3297 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
3298 // If unsafe fp math optimization is enabled and there are no other uses of
3299 // the CMP operands, and the condition code is EQ or NE, we can optimize it
3300 // to an integer comparison.
3301 if (CC == ISD::SETOEQ)
3303 else if (CC == ISD::SETUNE)
3306 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
3308 if (LHS.getValueType() == MVT::f32) {
3309 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3310 bitcastf32Toi32(LHS, DAG), Mask);
3311 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3312 bitcastf32Toi32(RHS, DAG), Mask);
3313 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3314 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3315 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3316 Chain, Dest, ARMcc, CCR, Cmp);
3321 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3322 expandf64Toi32(RHS, DAG, RHS1, RHS2);
3323 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3324 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
3325 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3326 ARMcc = DAG.getConstant(CondCode, MVT::i32);
3327 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
3328 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
3329 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops, 7);
3335 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3336 SDValue Chain = Op.getOperand(0);
3337 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3338 SDValue LHS = Op.getOperand(2);
3339 SDValue RHS = Op.getOperand(3);
3340 SDValue Dest = Op.getOperand(4);
3343 if (LHS.getValueType() == MVT::i32) {
3345 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3346 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3347 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3348 Chain, Dest, ARMcc, CCR, Cmp);
3351 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
3353 if (getTargetMachine().Options.UnsafeFPMath &&
3354 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3355 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3356 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3357 if (Result.getNode())
3361 ARMCC::CondCodes CondCode, CondCode2;
3362 FPCCToARMCC(CC, CondCode, CondCode2);
3364 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3365 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
3366 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3367 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
3368 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
3369 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
3370 if (CondCode2 != ARMCC::AL) {
3371 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3372 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
3373 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops, 5);
3378 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
3379 SDValue Chain = Op.getOperand(0);
3380 SDValue Table = Op.getOperand(1);
3381 SDValue Index = Op.getOperand(2);
3384 EVT PTy = getPointerTy();
3385 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3386 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
3387 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
3388 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
3389 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
3390 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3391 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
3392 if (Subtarget->isThumb2()) {
3393 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3394 // which does another jump to the destination. This also makes it easier
3395 // to translate it to TBB / TBH later.
3396 // FIXME: This might not work if the function is extremely large.
3397 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
3398 Addr, Op.getOperand(2), JTI, UId);
3400 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
3401 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
3402 MachinePointerInfo::getJumpTable(),
3403 false, false, false, 0);
3404 Chain = Addr.getValue(1);
3405 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
3406 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
3408 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
3409 MachinePointerInfo::getJumpTable(),
3410 false, false, false, 0);
3411 Chain = Addr.getValue(1);
3412 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
3416 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3417 EVT VT = Op.getValueType();
3420 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3421 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3423 return DAG.UnrollVectorOp(Op.getNode());
3426 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3427 "Invalid type for custom lowering!");
3428 if (VT != MVT::v4i16)
3429 return DAG.UnrollVectorOp(Op.getNode());
3431 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3432 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
3435 static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
3436 EVT VT = Op.getValueType();
3438 return LowerVectorFP_TO_INT(Op, DAG);
3443 switch (Op.getOpcode()) {
3444 default: llvm_unreachable("Invalid opcode!");
3445 case ISD::FP_TO_SINT:
3446 Opc = ARMISD::FTOSI;
3448 case ISD::FP_TO_UINT:
3449 Opc = ARMISD::FTOUI;
3452 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
3453 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
3456 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3457 EVT VT = Op.getValueType();
3460 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3461 if (VT.getVectorElementType() == MVT::f32)
3463 return DAG.UnrollVectorOp(Op.getNode());
3466 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3467 "Invalid type for custom lowering!");
3468 if (VT != MVT::v4f32)
3469 return DAG.UnrollVectorOp(Op.getNode());
3473 switch (Op.getOpcode()) {
3474 default: llvm_unreachable("Invalid opcode!");
3475 case ISD::SINT_TO_FP:
3476 CastOpc = ISD::SIGN_EXTEND;
3477 Opc = ISD::SINT_TO_FP;
3479 case ISD::UINT_TO_FP:
3480 CastOpc = ISD::ZERO_EXTEND;
3481 Opc = ISD::UINT_TO_FP;
3485 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3486 return DAG.getNode(Opc, dl, VT, Op);
3489 static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3490 EVT VT = Op.getValueType();
3492 return LowerVectorINT_TO_FP(Op, DAG);
3497 switch (Op.getOpcode()) {
3498 default: llvm_unreachable("Invalid opcode!");
3499 case ISD::SINT_TO_FP:
3500 Opc = ARMISD::SITOF;
3502 case ISD::UINT_TO_FP:
3503 Opc = ARMISD::UITOF;
3507 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
3508 return DAG.getNode(Opc, dl, VT, Op);
3511 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
3512 // Implement fcopysign with a fabs and a conditional fneg.
3513 SDValue Tmp0 = Op.getOperand(0);
3514 SDValue Tmp1 = Op.getOperand(1);
3516 EVT VT = Op.getValueType();
3517 EVT SrcVT = Tmp1.getValueType();
3518 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3519 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3520 bool UseNEON = !InGPR && Subtarget->hasNEON();
3523 // Use VBSL to copy the sign bit.
3524 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3525 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3526 DAG.getTargetConstant(EncodedVal, MVT::i32));
3527 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3529 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3530 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3531 DAG.getConstant(32, MVT::i32));
3532 else /*if (VT == MVT::f32)*/
3533 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3534 if (SrcVT == MVT::f32) {
3535 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3537 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3538 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3539 DAG.getConstant(32, MVT::i32));
3540 } else if (VT == MVT::f32)
3541 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3542 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3543 DAG.getConstant(32, MVT::i32));
3544 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3545 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3547 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3549 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3550 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3551 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
3553 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3554 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3555 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
3556 if (VT == MVT::f32) {
3557 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3558 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3559 DAG.getConstant(0, MVT::i32));
3561 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3567 // Bitcast operand 1 to i32.
3568 if (SrcVT == MVT::f64)
3569 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3570 &Tmp1, 1).getValue(1);
3571 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3573 // Or in the signbit with integer operations.
3574 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3575 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3576 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3577 if (VT == MVT::f32) {
3578 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3579 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3580 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3581 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
3584 // f64: Or the high part with signbit and then combine two parts.
3585 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
3587 SDValue Lo = Tmp0.getValue(0);
3588 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3589 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3590 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
3593 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3594 MachineFunction &MF = DAG.getMachineFunction();
3595 MachineFrameInfo *MFI = MF.getFrameInfo();
3596 MFI->setReturnAddressIsTaken(true);
3598 EVT VT = Op.getValueType();
3600 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3602 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3603 SDValue Offset = DAG.getConstant(4, MVT::i32);
3604 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3605 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
3606 MachinePointerInfo(), false, false, false, 0);
3609 // Return LR, which contains the return address. Mark it an implicit live-in.
3610 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
3611 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3614 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
3615 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3616 MFI->setFrameAddressIsTaken(true);
3618 EVT VT = Op.getValueType();
3619 SDLoc dl(Op); // FIXME probably not meaningful
3620 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3621 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetDarwin())
3622 ? ARM::R7 : ARM::R11;
3623 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3625 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3626 MachinePointerInfo(),
3627 false, false, false, 0);
3631 /// Custom Expand long vector extensions, where size(DestVec) > 2*size(SrcVec),
3632 /// and size(DestVec) > 128-bits.
3633 /// This is achieved by doing the one extension from the SrcVec, splitting the
3634 /// result, extending these parts, and then concatenating these into the
3636 static SDValue ExpandVectorExtension(SDNode *N, SelectionDAG &DAG) {
3637 SDValue Op = N->getOperand(0);
3638 EVT SrcVT = Op.getValueType();
3639 EVT DestVT = N->getValueType(0);
3641 assert(DestVT.getSizeInBits() > 128 &&
3642 "Custom sext/zext expansion needs >128-bit vector.");
3643 // If this is a normal length extension, use the default expansion.
3644 if (SrcVT.getSizeInBits()*4 != DestVT.getSizeInBits() &&
3645 SrcVT.getSizeInBits()*8 != DestVT.getSizeInBits())
3649 unsigned SrcEltSize = SrcVT.getVectorElementType().getSizeInBits();
3650 unsigned DestEltSize = DestVT.getVectorElementType().getSizeInBits();
3651 unsigned NumElts = SrcVT.getVectorNumElements();
3652 LLVMContext &Ctx = *DAG.getContext();
3653 SDValue Mid, SplitLo, SplitHi, ExtLo, ExtHi;
3655 EVT MidVT = EVT::getVectorVT(Ctx, EVT::getIntegerVT(Ctx, SrcEltSize*2),
3657 EVT SplitVT = EVT::getVectorVT(Ctx, EVT::getIntegerVT(Ctx, SrcEltSize*2),
3659 EVT ExtVT = EVT::getVectorVT(Ctx, EVT::getIntegerVT(Ctx, DestEltSize),
3662 Mid = DAG.getNode(N->getOpcode(), dl, MidVT, Op);
3663 SplitLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SplitVT, Mid,
3664 DAG.getIntPtrConstant(0));
3665 SplitHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SplitVT, Mid,
3666 DAG.getIntPtrConstant(NumElts/2));
3667 ExtLo = DAG.getNode(N->getOpcode(), dl, ExtVT, SplitLo);
3668 ExtHi = DAG.getNode(N->getOpcode(), dl, ExtVT, SplitHi);
3669 return DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, ExtLo, ExtHi);
3672 /// ExpandBITCAST - If the target supports VFP, this function is called to
3673 /// expand a bit convert where either the source or destination type is i64 to
3674 /// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3675 /// operand type is illegal (e.g., v2f32 for a target that doesn't support
3676 /// vectors), since the legalizer won't know what to do with that.
3677 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
3678 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3680 SDValue Op = N->getOperand(0);
3682 // This function is only supposed to be called for i64 types, either as the
3683 // source or destination of the bit convert.
3684 EVT SrcVT = Op.getValueType();
3685 EVT DstVT = N->getValueType(0);
3686 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
3687 "ExpandBITCAST called for non-i64 type");
3689 // Turn i64->f64 into VMOVDRR.
3690 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
3691 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3692 DAG.getConstant(0, MVT::i32));
3693 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3694 DAG.getConstant(1, MVT::i32));
3695 return DAG.getNode(ISD::BITCAST, dl, DstVT,
3696 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
3699 // Turn f64->i64 into VMOVRRD.
3700 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
3701 SDValue Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3702 DAG.getVTList(MVT::i32, MVT::i32), &Op, 1);
3703 // Merge the pieces into a single i64 value.
3704 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3710 /// getZeroVector - Returns a vector of specified type with all zero elements.
3711 /// Zero vectors are used to represent vector negation and in those cases
3712 /// will be implemented with the NEON VNEG instruction. However, VNEG does
3713 /// not support i64 elements, so sometimes the zero vectors will need to be
3714 /// explicitly constructed. Regardless, use a canonical VMOV to create the
3716 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
3717 assert(VT.isVector() && "Expected a vector type");
3718 // The canonical modified immediate encoding of a zero vector is....0!
3719 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3720 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3721 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
3722 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
3725 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3726 /// i32 values and take a 2 x i32 value to shift plus a shift amount.
3727 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3728 SelectionDAG &DAG) const {
3729 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3730 EVT VT = Op.getValueType();
3731 unsigned VTBits = VT.getSizeInBits();
3733 SDValue ShOpLo = Op.getOperand(0);
3734 SDValue ShOpHi = Op.getOperand(1);
3735 SDValue ShAmt = Op.getOperand(2);
3737 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
3739 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3741 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3742 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3743 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3744 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3745 DAG.getConstant(VTBits, MVT::i32));
3746 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3747 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3748 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
3750 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3751 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
3753 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
3754 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
3757 SDValue Ops[2] = { Lo, Hi };
3758 return DAG.getMergeValues(Ops, 2, dl);
3761 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
3762 /// i32 values and take a 2 x i32 value to shift plus a shift amount.
3763 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
3764 SelectionDAG &DAG) const {
3765 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3766 EVT VT = Op.getValueType();
3767 unsigned VTBits = VT.getSizeInBits();
3769 SDValue ShOpLo = Op.getOperand(0);
3770 SDValue ShOpHi = Op.getOperand(1);
3771 SDValue ShAmt = Op.getOperand(2);
3774 assert(Op.getOpcode() == ISD::SHL_PARTS);
3775 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3776 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3777 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
3778 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3779 DAG.getConstant(VTBits, MVT::i32));
3780 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
3781 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
3783 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
3784 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3785 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
3787 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
3788 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
3791 SDValue Ops[2] = { Lo, Hi };
3792 return DAG.getMergeValues(Ops, 2, dl);
3795 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
3796 SelectionDAG &DAG) const {
3797 // The rounding mode is in bits 23:22 of the FPSCR.
3798 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
3799 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
3800 // so that the shift + and get folded into a bitfield extract.
3802 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
3803 DAG.getConstant(Intrinsic::arm_get_fpscr,
3805 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
3806 DAG.getConstant(1U << 22, MVT::i32));
3807 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
3808 DAG.getConstant(22, MVT::i32));
3809 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
3810 DAG.getConstant(3, MVT::i32));
3813 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
3814 const ARMSubtarget *ST) {
3815 EVT VT = N->getValueType(0);
3818 if (!ST->hasV6T2Ops())
3821 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
3822 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
3825 /// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
3826 /// for each 16-bit element from operand, repeated. The basic idea is to
3827 /// leverage vcnt to get the 8-bit counts, gather and add the results.
3829 /// Trace for v4i16:
3830 /// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3831 /// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
3832 /// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi)
3833 /// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
3834 /// [b0 b1 b2 b3 b4 b5 b6 b7]
3835 /// +[b1 b0 b3 b2 b5 b4 b7 b6]
3836 /// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
3837 /// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
3838 static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
3839 EVT VT = N->getValueType(0);
3842 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
3843 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
3844 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
3845 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
3846 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
3847 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
3850 /// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
3851 /// bit-count for each 16-bit element from the operand. We need slightly
3852 /// different sequencing for v4i16 and v8i16 to stay within NEON's available
3853 /// 64/128-bit registers.
3855 /// Trace for v4i16:
3856 /// input = [v0 v1 v2 v3 ] (vi 16-bit element)
3857 /// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
3858 /// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
3859 /// v4i16:Extracted = [k0 k1 k2 k3 ]
3860 static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
3861 EVT VT = N->getValueType(0);
3864 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
3865 if (VT.is64BitVector()) {
3866 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
3867 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
3868 DAG.getIntPtrConstant(0));
3870 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
3871 BitCounts, DAG.getIntPtrConstant(0));
3872 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
3876 /// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
3877 /// bit-count for each 32-bit element from the operand. The idea here is
3878 /// to split the vector into 16-bit elements, leverage the 16-bit count
3879 /// routine, and then combine the results.
3881 /// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
3882 /// input = [v0 v1 ] (vi: 32-bit elements)
3883 /// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
3884 /// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
3885 /// vrev: N0 = [k1 k0 k3 k2 ]
3887 /// N1 =+[k1 k0 k3 k2 ]
3889 /// N2 =+[k1 k3 k0 k2 ]
3891 /// Extended =+[k1 k3 k0 k2 ]
3893 /// Extracted=+[k1 k3 ]
3895 static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
3896 EVT VT = N->getValueType(0);
3899 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
3901 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
3902 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
3903 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
3904 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
3905 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
3907 if (VT.is64BitVector()) {
3908 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
3909 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
3910 DAG.getIntPtrConstant(0));
3912 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
3913 DAG.getIntPtrConstant(0));
3914 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
3918 static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
3919 const ARMSubtarget *ST) {
3920 EVT VT = N->getValueType(0);
3922 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
3923 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
3924 VT == MVT::v4i16 || VT == MVT::v8i16) &&
3925 "Unexpected type for custom ctpop lowering");
3927 if (VT.getVectorElementType() == MVT::i32)
3928 return lowerCTPOP32BitElements(N, DAG);
3930 return lowerCTPOP16BitElements(N, DAG);
3933 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
3934 const ARMSubtarget *ST) {
3935 EVT VT = N->getValueType(0);
3941 // Lower vector shifts on NEON to use VSHL.
3942 assert(ST->hasNEON() && "unexpected vector shift");
3944 // Left shifts translate directly to the vshiftu intrinsic.
3945 if (N->getOpcode() == ISD::SHL)
3946 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3947 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
3948 N->getOperand(0), N->getOperand(1));
3950 assert((N->getOpcode() == ISD::SRA ||
3951 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
3953 // NEON uses the same intrinsics for both left and right shifts. For
3954 // right shifts, the shift amounts are negative, so negate the vector of
3956 EVT ShiftVT = N->getOperand(1).getValueType();
3957 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
3958 getZeroVector(ShiftVT, DAG, dl),
3960 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
3961 Intrinsic::arm_neon_vshifts :
3962 Intrinsic::arm_neon_vshiftu);
3963 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
3964 DAG.getConstant(vshiftInt, MVT::i32),
3965 N->getOperand(0), NegatedCount);
3968 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
3969 const ARMSubtarget *ST) {
3970 EVT VT = N->getValueType(0);
3973 // We can get here for a node like i32 = ISD::SHL i32, i64
3977 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
3978 "Unknown shift to lower!");
3980 // We only lower SRA, SRL of 1 here, all others use generic lowering.
3981 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
3982 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
3985 // If we are in thumb mode, we don't have RRX.
3986 if (ST->isThumb1Only()) return SDValue();
3988 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
3989 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
3990 DAG.getConstant(0, MVT::i32));
3991 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
3992 DAG.getConstant(1, MVT::i32));
3994 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
3995 // captures the result into a carry flag.
3996 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
3997 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), &Hi, 1);
3999 // The low part is an ARMISD::RRX operand, which shifts the carry in.
4000 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
4002 // Merge the pieces into a single i64 value.
4003 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
4006 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4007 SDValue TmpOp0, TmpOp1;
4008 bool Invert = false;
4012 SDValue Op0 = Op.getOperand(0);
4013 SDValue Op1 = Op.getOperand(1);
4014 SDValue CC = Op.getOperand(2);
4015 EVT VT = Op.getValueType();
4016 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4019 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
4020 switch (SetCCOpcode) {
4021 default: llvm_unreachable("Illegal FP comparison");
4023 case ISD::SETNE: Invert = true; // Fallthrough
4025 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4027 case ISD::SETLT: Swap = true; // Fallthrough
4029 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4031 case ISD::SETLE: Swap = true; // Fallthrough
4033 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4034 case ISD::SETUGE: Swap = true; // Fallthrough
4035 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4036 case ISD::SETUGT: Swap = true; // Fallthrough
4037 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4038 case ISD::SETUEQ: Invert = true; // Fallthrough
4040 // Expand this to (OLT | OGT).
4044 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4045 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4047 case ISD::SETUO: Invert = true; // Fallthrough
4049 // Expand this to (OLT | OGE).
4053 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4054 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4058 // Integer comparisons.
4059 switch (SetCCOpcode) {
4060 default: llvm_unreachable("Illegal integer comparison");
4061 case ISD::SETNE: Invert = true;
4062 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4063 case ISD::SETLT: Swap = true;
4064 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4065 case ISD::SETLE: Swap = true;
4066 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4067 case ISD::SETULT: Swap = true;
4068 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4069 case ISD::SETULE: Swap = true;
4070 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4073 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
4074 if (Opc == ARMISD::VCEQ) {
4077 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4079 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4082 // Ignore bitconvert.
4083 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
4084 AndOp = AndOp.getOperand(0);
4086 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4088 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4089 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
4096 std::swap(Op0, Op1);
4098 // If one of the operands is a constant vector zero, attempt to fold the
4099 // comparison to a specialized compare-against-zero form.
4101 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4103 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4104 if (Opc == ARMISD::VCGE)
4105 Opc = ARMISD::VCLEZ;
4106 else if (Opc == ARMISD::VCGT)
4107 Opc = ARMISD::VCLTZ;
4112 if (SingleOp.getNode()) {
4115 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4117 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4119 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4121 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4123 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4125 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4128 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4132 Result = DAG.getNOT(dl, Result, VT);
4137 /// isNEONModifiedImm - Check if the specified splat value corresponds to a
4138 /// valid vector constant for a NEON instruction with a "modified immediate"
4139 /// operand (e.g., VMOV). If so, return the encoded value.
4140 static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4141 unsigned SplatBitSize, SelectionDAG &DAG,
4142 EVT &VT, bool is128Bits, NEONModImmType type) {
4143 unsigned OpCmode, Imm;
4145 // SplatBitSize is set to the smallest size that splats the vector, so a
4146 // zero vector will always have SplatBitSize == 8. However, NEON modified
4147 // immediate instructions others than VMOV do not support the 8-bit encoding
4148 // of a zero vector, and the default encoding of zero is supposed to be the
4153 switch (SplatBitSize) {
4155 if (type != VMOVModImm)
4157 // Any 1-byte value is OK. Op=0, Cmode=1110.
4158 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
4161 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
4165 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
4166 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
4167 if ((SplatBits & ~0xff) == 0) {
4168 // Value = 0x00nn: Op=x, Cmode=100x.
4173 if ((SplatBits & ~0xff00) == 0) {
4174 // Value = 0xnn00: Op=x, Cmode=101x.
4176 Imm = SplatBits >> 8;
4182 // NEON's 32-bit VMOV supports splat values where:
4183 // * only one byte is nonzero, or
4184 // * the least significant byte is 0xff and the second byte is nonzero, or
4185 // * the least significant 2 bytes are 0xff and the third is nonzero.
4186 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
4187 if ((SplatBits & ~0xff) == 0) {
4188 // Value = 0x000000nn: Op=x, Cmode=000x.
4193 if ((SplatBits & ~0xff00) == 0) {
4194 // Value = 0x0000nn00: Op=x, Cmode=001x.
4196 Imm = SplatBits >> 8;
4199 if ((SplatBits & ~0xff0000) == 0) {
4200 // Value = 0x00nn0000: Op=x, Cmode=010x.
4202 Imm = SplatBits >> 16;
4205 if ((SplatBits & ~0xff000000) == 0) {
4206 // Value = 0xnn000000: Op=x, Cmode=011x.
4208 Imm = SplatBits >> 24;
4212 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4213 if (type == OtherModImm) return SDValue();
4215 if ((SplatBits & ~0xffff) == 0 &&
4216 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4217 // Value = 0x0000nnff: Op=x, Cmode=1100.
4219 Imm = SplatBits >> 8;
4224 if ((SplatBits & ~0xffffff) == 0 &&
4225 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4226 // Value = 0x00nnffff: Op=x, Cmode=1101.
4228 Imm = SplatBits >> 16;
4229 SplatBits |= 0xffff;
4233 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4234 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4235 // VMOV.I32. A (very) minor optimization would be to replicate the value
4236 // and fall through here to test for a valid 64-bit splat. But, then the
4237 // caller would also need to check and handle the change in size.
4241 if (type != VMOVModImm)
4243 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
4244 uint64_t BitMask = 0xff;
4246 unsigned ImmMask = 1;
4248 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
4249 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
4252 } else if ((SplatBits & BitMask) != 0) {
4258 // Op=1, Cmode=1110.
4261 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
4266 llvm_unreachable("unexpected size for isNEONModifiedImm");
4269 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4270 return DAG.getTargetConstant(EncodedVal, MVT::i32);
4273 SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4274 const ARMSubtarget *ST) const {
4278 bool IsDouble = Op.getValueType() == MVT::f64;
4279 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
4281 // Try splatting with a VMOV.f32...
4282 APFloat FPVal = CFP->getValueAPF();
4283 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4286 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4287 // We have code in place to select a valid ConstantFP already, no need to
4292 // It's a float and we are trying to use NEON operations where
4293 // possible. Lower it to a splat followed by an extract.
4295 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4296 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4298 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4299 DAG.getConstant(0, MVT::i32));
4302 // The rest of our options are NEON only, make sure that's allowed before
4304 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4308 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4310 // It wouldn't really be worth bothering for doubles except for one very
4311 // important value, which does happen to match: 0.0. So make sure we don't do
4313 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4316 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4317 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4319 if (NewVal != SDValue()) {
4321 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4324 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4326 // It's a float: cast and extract a vector element.
4327 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4329 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4330 DAG.getConstant(0, MVT::i32));
4333 // Finally, try a VMVN.i32
4334 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4336 if (NewVal != SDValue()) {
4338 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
4341 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4343 // It's a float: cast and extract a vector element.
4344 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4346 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4347 DAG.getConstant(0, MVT::i32));
4353 // check if an VEXT instruction can handle the shuffle mask when the
4354 // vector sources of the shuffle are the same.
4355 static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4356 unsigned NumElts = VT.getVectorNumElements();
4358 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4364 // If this is a VEXT shuffle, the immediate value is the index of the first
4365 // element. The other shuffle indices must be the successive elements after
4367 unsigned ExpectedElt = Imm;
4368 for (unsigned i = 1; i < NumElts; ++i) {
4369 // Increment the expected index. If it wraps around, just follow it
4370 // back to index zero and keep going.
4372 if (ExpectedElt == NumElts)
4375 if (M[i] < 0) continue; // ignore UNDEF indices
4376 if (ExpectedElt != static_cast<unsigned>(M[i]))
4384 static bool isVEXTMask(ArrayRef<int> M, EVT VT,
4385 bool &ReverseVEXT, unsigned &Imm) {
4386 unsigned NumElts = VT.getVectorNumElements();
4387 ReverseVEXT = false;
4389 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4395 // If this is a VEXT shuffle, the immediate value is the index of the first
4396 // element. The other shuffle indices must be the successive elements after
4398 unsigned ExpectedElt = Imm;
4399 for (unsigned i = 1; i < NumElts; ++i) {
4400 // Increment the expected index. If it wraps around, it may still be
4401 // a VEXT but the source vectors must be swapped.
4403 if (ExpectedElt == NumElts * 2) {
4408 if (M[i] < 0) continue; // ignore UNDEF indices
4409 if (ExpectedElt != static_cast<unsigned>(M[i]))
4413 // Adjust the index value if the source operands will be swapped.
4420 /// isVREVMask - Check if a vector shuffle corresponds to a VREV
4421 /// instruction with the specified blocksize. (The order of the elements
4422 /// within each block of the vector is reversed.)
4423 static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
4424 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4425 "Only possible block sizes for VREV are: 16, 32, 64");
4427 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4431 unsigned NumElts = VT.getVectorNumElements();
4432 unsigned BlockElts = M[0] + 1;
4433 // If the first shuffle index is UNDEF, be optimistic.
4435 BlockElts = BlockSize / EltSz;
4437 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4440 for (unsigned i = 0; i < NumElts; ++i) {
4441 if (M[i] < 0) continue; // ignore UNDEF indices
4442 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
4449 static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
4450 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4451 // range, then 0 is placed into the resulting vector. So pretty much any mask
4452 // of 8 elements can work here.
4453 return VT == MVT::v8i8 && M.size() == 8;
4456 static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
4457 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4461 unsigned NumElts = VT.getVectorNumElements();
4462 WhichResult = (M[0] == 0 ? 0 : 1);
4463 for (unsigned i = 0; i < NumElts; i += 2) {
4464 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4465 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
4471 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4472 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4473 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
4474 static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
4475 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4479 unsigned NumElts = VT.getVectorNumElements();
4480 WhichResult = (M[0] == 0 ? 0 : 1);
4481 for (unsigned i = 0; i < NumElts; i += 2) {
4482 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4483 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
4489 static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
4490 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4494 unsigned NumElts = VT.getVectorNumElements();
4495 WhichResult = (M[0] == 0 ? 0 : 1);
4496 for (unsigned i = 0; i != NumElts; ++i) {
4497 if (M[i] < 0) continue; // ignore UNDEF indices
4498 if ((unsigned) M[i] != 2 * i + WhichResult)
4502 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4503 if (VT.is64BitVector() && EltSz == 32)
4509 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4510 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4511 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
4512 static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
4513 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4517 unsigned Half = VT.getVectorNumElements() / 2;
4518 WhichResult = (M[0] == 0 ? 0 : 1);
4519 for (unsigned j = 0; j != 2; ++j) {
4520 unsigned Idx = WhichResult;
4521 for (unsigned i = 0; i != Half; ++i) {
4522 int MIdx = M[i + j * Half];
4523 if (MIdx >= 0 && (unsigned) MIdx != Idx)
4529 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4530 if (VT.is64BitVector() && EltSz == 32)
4536 static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
4537 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4541 unsigned NumElts = VT.getVectorNumElements();
4542 WhichResult = (M[0] == 0 ? 0 : 1);
4543 unsigned Idx = WhichResult * NumElts / 2;
4544 for (unsigned i = 0; i != NumElts; i += 2) {
4545 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4546 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
4551 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4552 if (VT.is64BitVector() && EltSz == 32)
4558 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4559 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4560 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
4561 static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
4562 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4566 unsigned NumElts = VT.getVectorNumElements();
4567 WhichResult = (M[0] == 0 ? 0 : 1);
4568 unsigned Idx = WhichResult * NumElts / 2;
4569 for (unsigned i = 0; i != NumElts; i += 2) {
4570 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4571 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
4576 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4577 if (VT.is64BitVector() && EltSz == 32)
4583 /// \return true if this is a reverse operation on an vector.
4584 static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4585 unsigned NumElts = VT.getVectorNumElements();
4586 // Make sure the mask has the right size.
4587 if (NumElts != M.size())
4590 // Look for <15, ..., 3, -1, 1, 0>.
4591 for (unsigned i = 0; i != NumElts; ++i)
4592 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4598 // If N is an integer constant that can be moved into a register in one
4599 // instruction, return an SDValue of such a constant (will become a MOV
4600 // instruction). Otherwise return null.
4601 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
4602 const ARMSubtarget *ST, SDLoc dl) {
4604 if (!isa<ConstantSDNode>(N))
4606 Val = cast<ConstantSDNode>(N)->getZExtValue();
4608 if (ST->isThumb1Only()) {
4609 if (Val <= 255 || ~Val <= 255)
4610 return DAG.getConstant(Val, MVT::i32);
4612 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4613 return DAG.getConstant(Val, MVT::i32);
4618 // If this is a case we can't handle, return null and let the default
4619 // expansion code take care of it.
4620 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4621 const ARMSubtarget *ST) const {
4622 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
4624 EVT VT = Op.getValueType();
4626 APInt SplatBits, SplatUndef;
4627 unsigned SplatBitSize;
4629 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
4630 if (SplatBitSize <= 64) {
4631 // Check if an immediate VMOV works.
4633 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
4634 SplatUndef.getZExtValue(), SplatBitSize,
4635 DAG, VmovVT, VT.is128BitVector(),
4637 if (Val.getNode()) {
4638 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
4639 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
4642 // Try an immediate VMVN.
4643 uint64_t NegatedImm = (~SplatBits).getZExtValue();
4644 Val = isNEONModifiedImm(NegatedImm,
4645 SplatUndef.getZExtValue(), SplatBitSize,
4646 DAG, VmovVT, VT.is128BitVector(),
4648 if (Val.getNode()) {
4649 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
4650 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
4653 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
4654 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
4655 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
4657 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4658 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4664 // Scan through the operands to see if only one value is used.
4666 // As an optimisation, even if more than one value is used it may be more
4667 // profitable to splat with one value then change some lanes.
4669 // Heuristically we decide to do this if the vector has a "dominant" value,
4670 // defined as splatted to more than half of the lanes.
4671 unsigned NumElts = VT.getVectorNumElements();
4672 bool isOnlyLowElement = true;
4673 bool usesOnlyOneValue = true;
4674 bool hasDominantValue = false;
4675 bool isConstant = true;
4677 // Map of the number of times a particular SDValue appears in the
4679 DenseMap<SDValue, unsigned> ValueCounts;
4681 for (unsigned i = 0; i < NumElts; ++i) {
4682 SDValue V = Op.getOperand(i);
4683 if (V.getOpcode() == ISD::UNDEF)
4686 isOnlyLowElement = false;
4687 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4690 ValueCounts.insert(std::make_pair(V, 0));
4691 unsigned &Count = ValueCounts[V];
4693 // Is this value dominant? (takes up more than half of the lanes)
4694 if (++Count > (NumElts / 2)) {
4695 hasDominantValue = true;
4699 if (ValueCounts.size() != 1)
4700 usesOnlyOneValue = false;
4701 if (!Value.getNode() && ValueCounts.size() > 0)
4702 Value = ValueCounts.begin()->first;
4704 if (ValueCounts.size() == 0)
4705 return DAG.getUNDEF(VT);
4707 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
4708 // Keep going if we are hitting this case.
4709 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
4710 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4712 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4714 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4715 // i32 and try again.
4716 if (hasDominantValue && EltSize <= 32) {
4720 // If we are VDUPing a value that comes directly from a vector, that will
4721 // cause an unnecessary move to and from a GPR, where instead we could
4722 // just use VDUPLANE. We can only do this if the lane being extracted
4723 // is at a constant index, as the VDUP from lane instructions only have
4724 // constant-index forms.
4725 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4726 isa<ConstantSDNode>(Value->getOperand(1))) {
4727 // We need to create a new undef vector to use for the VDUPLANE if the
4728 // size of the vector from which we get the value is different than the
4729 // size of the vector that we need to create. We will insert the element
4730 // such that the register coalescer will remove unnecessary copies.
4731 if (VT != Value->getOperand(0).getValueType()) {
4732 ConstantSDNode *constIndex;
4733 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4734 assert(constIndex && "The index is not a constant!");
4735 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4736 VT.getVectorNumElements();
4737 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4738 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4739 Value, DAG.getConstant(index, MVT::i32)),
4740 DAG.getConstant(index, MVT::i32));
4742 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4743 Value->getOperand(0), Value->getOperand(1));
4745 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4747 if (!usesOnlyOneValue) {
4748 // The dominant value was splatted as 'N', but we now have to insert
4749 // all differing elements.
4750 for (unsigned I = 0; I < NumElts; ++I) {
4751 if (Op.getOperand(I) == Value)
4753 SmallVector<SDValue, 3> Ops;
4755 Ops.push_back(Op.getOperand(I));
4756 Ops.push_back(DAG.getConstant(I, MVT::i32));
4757 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, &Ops[0], 3);
4762 if (VT.getVectorElementType().isFloatingPoint()) {
4763 SmallVector<SDValue, 8> Ops;
4764 for (unsigned i = 0; i < NumElts; ++i)
4765 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
4767 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
4768 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], NumElts);
4769 Val = LowerBUILD_VECTOR(Val, DAG, ST);
4771 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
4773 if (usesOnlyOneValue) {
4774 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
4775 if (isConstant && Val.getNode())
4776 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
4780 // If all elements are constants and the case above didn't get hit, fall back
4781 // to the default expansion, which will generate a load from the constant
4786 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
4788 SDValue shuffle = ReconstructShuffle(Op, DAG);
4789 if (shuffle != SDValue())
4793 // Vectors with 32- or 64-bit elements can be built by directly assigning
4794 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
4795 // will be legalized.
4796 if (EltSize >= 32) {
4797 // Do the expansion with floating-point types, since that is what the VFP
4798 // registers are defined to use, and since i64 is not legal.
4799 EVT EltVT = EVT::getFloatingPointVT(EltSize);
4800 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
4801 SmallVector<SDValue, 8> Ops;
4802 for (unsigned i = 0; i < NumElts; ++i)
4803 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
4804 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
4805 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
4808 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
4809 // know the default expansion would otherwise fall back on something even
4810 // worse. For a vector with one or two non-undef values, that's
4811 // scalar_to_vector for the elements followed by a shuffle (provided the
4812 // shuffle is valid for the target) and materialization element by element
4813 // on the stack followed by a load for everything else.
4814 if (!isConstant && !usesOnlyOneValue) {
4815 SDValue Vec = DAG.getUNDEF(VT);
4816 for (unsigned i = 0 ; i < NumElts; ++i) {
4817 SDValue V = Op.getOperand(i);
4818 if (V.getOpcode() == ISD::UNDEF)
4820 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
4821 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
4829 // Gather data to see if the operation can be modelled as a
4830 // shuffle in combination with VEXTs.
4831 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
4832 SelectionDAG &DAG) const {
4834 EVT VT = Op.getValueType();
4835 unsigned NumElts = VT.getVectorNumElements();
4837 SmallVector<SDValue, 2> SourceVecs;
4838 SmallVector<unsigned, 2> MinElts;
4839 SmallVector<unsigned, 2> MaxElts;
4841 for (unsigned i = 0; i < NumElts; ++i) {
4842 SDValue V = Op.getOperand(i);
4843 if (V.getOpcode() == ISD::UNDEF)
4845 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
4846 // A shuffle can only come from building a vector from various
4847 // elements of other vectors.
4849 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
4850 VT.getVectorElementType()) {
4851 // This code doesn't know how to handle shuffles where the vector
4852 // element types do not match (this happens because type legalization
4853 // promotes the return type of EXTRACT_VECTOR_ELT).
4854 // FIXME: It might be appropriate to extend this code to handle
4855 // mismatched types.
4859 // Record this extraction against the appropriate vector if possible...
4860 SDValue SourceVec = V.getOperand(0);
4861 // If the element number isn't a constant, we can't effectively
4862 // analyze what's going on.
4863 if (!isa<ConstantSDNode>(V.getOperand(1)))
4865 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
4866 bool FoundSource = false;
4867 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
4868 if (SourceVecs[j] == SourceVec) {
4869 if (MinElts[j] > EltNo)
4871 if (MaxElts[j] < EltNo)
4878 // Or record a new source if not...
4880 SourceVecs.push_back(SourceVec);
4881 MinElts.push_back(EltNo);
4882 MaxElts.push_back(EltNo);
4886 // Currently only do something sane when at most two source vectors
4888 if (SourceVecs.size() > 2)
4891 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
4892 int VEXTOffsets[2] = {0, 0};
4894 // This loop extracts the usage patterns of the source vectors
4895 // and prepares appropriate SDValues for a shuffle if possible.
4896 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
4897 if (SourceVecs[i].getValueType() == VT) {
4898 // No VEXT necessary
4899 ShuffleSrcs[i] = SourceVecs[i];
4902 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
4903 // It probably isn't worth padding out a smaller vector just to
4904 // break it down again in a shuffle.
4908 // Since only 64-bit and 128-bit vectors are legal on ARM and
4909 // we've eliminated the other cases...
4910 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
4911 "unexpected vector sizes in ReconstructShuffle");
4913 if (MaxElts[i] - MinElts[i] >= NumElts) {
4914 // Span too large for a VEXT to cope
4918 if (MinElts[i] >= NumElts) {
4919 // The extraction can just take the second half
4920 VEXTOffsets[i] = NumElts;
4921 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4923 DAG.getIntPtrConstant(NumElts));
4924 } else if (MaxElts[i] < NumElts) {
4925 // The extraction can just take the first half
4927 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4929 DAG.getIntPtrConstant(0));
4931 // An actual VEXT is needed
4932 VEXTOffsets[i] = MinElts[i];
4933 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4935 DAG.getIntPtrConstant(0));
4936 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
4938 DAG.getIntPtrConstant(NumElts));
4939 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
4940 DAG.getConstant(VEXTOffsets[i], MVT::i32));
4944 SmallVector<int, 8> Mask;
4946 for (unsigned i = 0; i < NumElts; ++i) {
4947 SDValue Entry = Op.getOperand(i);
4948 if (Entry.getOpcode() == ISD::UNDEF) {
4953 SDValue ExtractVec = Entry.getOperand(0);
4954 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
4955 .getOperand(1))->getSExtValue();
4956 if (ExtractVec == SourceVecs[0]) {
4957 Mask.push_back(ExtractElt - VEXTOffsets[0]);
4959 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
4963 // Final check before we try to produce nonsense...
4964 if (isShuffleMaskLegal(Mask, VT))
4965 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
4971 /// isShuffleMaskLegal - Targets can use this to indicate that they only
4972 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4973 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4974 /// are assumed to be legal.
4976 ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
4978 if (VT.getVectorNumElements() == 4 &&
4979 (VT.is128BitVector() || VT.is64BitVector())) {
4980 unsigned PFIndexes[4];
4981 for (unsigned i = 0; i != 4; ++i) {
4985 PFIndexes[i] = M[i];
4988 // Compute the index in the perfect shuffle table.
4989 unsigned PFTableIndex =
4990 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
4991 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4992 unsigned Cost = (PFEntry >> 30);
4999 unsigned Imm, WhichResult;
5001 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5002 return (EltSize >= 32 ||
5003 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
5004 isVREVMask(M, VT, 64) ||
5005 isVREVMask(M, VT, 32) ||
5006 isVREVMask(M, VT, 16) ||
5007 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
5008 isVTBLMask(M, VT) ||
5009 isVTRNMask(M, VT, WhichResult) ||
5010 isVUZPMask(M, VT, WhichResult) ||
5011 isVZIPMask(M, VT, WhichResult) ||
5012 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5013 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
5014 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5015 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
5018 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5019 /// the specified operations to build the shuffle.
5020 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5021 SDValue RHS, SelectionDAG &DAG,
5023 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5024 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5025 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5028 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5037 OP_VUZPL, // VUZP, left result
5038 OP_VUZPR, // VUZP, right result
5039 OP_VZIPL, // VZIP, left result
5040 OP_VZIPR, // VZIP, right result
5041 OP_VTRNL, // VTRN, left result
5042 OP_VTRNR // VTRN, right result
5045 if (OpNum == OP_COPY) {
5046 if (LHSID == (1*9+2)*9+3) return LHS;
5047 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5051 SDValue OpLHS, OpRHS;
5052 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5053 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5054 EVT VT = OpLHS.getValueType();
5057 default: llvm_unreachable("Unknown shuffle opcode!");
5059 // VREV divides the vector in half and swaps within the half.
5060 if (VT.getVectorElementType() == MVT::i32 ||
5061 VT.getVectorElementType() == MVT::f32)
5062 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5063 // vrev <4 x i16> -> VREV32
5064 if (VT.getVectorElementType() == MVT::i16)
5065 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5066 // vrev <4 x i8> -> VREV16
5067 assert(VT.getVectorElementType() == MVT::i8);
5068 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
5073 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5074 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
5078 return DAG.getNode(ARMISD::VEXT, dl, VT,
5080 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5083 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5084 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5087 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5088 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5091 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5092 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
5096 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
5097 ArrayRef<int> ShuffleMask,
5098 SelectionDAG &DAG) {
5099 // Check to see if we can use the VTBL instruction.
5100 SDValue V1 = Op.getOperand(0);
5101 SDValue V2 = Op.getOperand(1);
5104 SmallVector<SDValue, 8> VTBLMask;
5105 for (ArrayRef<int>::iterator
5106 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5107 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5109 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5110 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
5111 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
5114 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
5115 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8,
5119 static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5120 SelectionDAG &DAG) {
5122 SDValue OpLHS = Op.getOperand(0);
5123 EVT VT = OpLHS.getValueType();
5125 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5126 "Expect an v8i16/v16i8 type");
5127 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5128 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5129 // extract the first 8 bytes into the top double word and the last 8 bytes
5130 // into the bottom double word. The v8i16 case is similar.
5131 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5132 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5133 DAG.getConstant(ExtractNum, MVT::i32));
5136 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
5137 SDValue V1 = Op.getOperand(0);
5138 SDValue V2 = Op.getOperand(1);
5140 EVT VT = Op.getValueType();
5141 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
5143 // Convert shuffles that are directly supported on NEON to target-specific
5144 // DAG nodes, instead of keeping them as shuffles and matching them again
5145 // during code selection. This is more efficient and avoids the possibility
5146 // of inconsistencies between legalization and selection.
5147 // FIXME: floating-point vectors should be canonicalized to integer vectors
5148 // of the same time so that they get CSEd properly.
5149 ArrayRef<int> ShuffleMask = SVN->getMask();
5151 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5152 if (EltSize <= 32) {
5153 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5154 int Lane = SVN->getSplatIndex();
5155 // If this is undef splat, generate it via "just" vdup, if possible.
5156 if (Lane == -1) Lane = 0;
5158 // Test if V1 is a SCALAR_TO_VECTOR.
5159 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5160 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5162 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5163 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5165 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5166 !isa<ConstantSDNode>(V1.getOperand(0))) {
5167 bool IsScalarToVector = true;
5168 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5169 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5170 IsScalarToVector = false;
5173 if (IsScalarToVector)
5174 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5176 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5177 DAG.getConstant(Lane, MVT::i32));
5182 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5185 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5186 DAG.getConstant(Imm, MVT::i32));
5189 if (isVREVMask(ShuffleMask, VT, 64))
5190 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5191 if (isVREVMask(ShuffleMask, VT, 32))
5192 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5193 if (isVREVMask(ShuffleMask, VT, 16))
5194 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5196 if (V2->getOpcode() == ISD::UNDEF &&
5197 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5198 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5199 DAG.getConstant(Imm, MVT::i32));
5202 // Check for Neon shuffles that modify both input vectors in place.
5203 // If both results are used, i.e., if there are two shuffles with the same
5204 // source operands and with masks corresponding to both results of one of
5205 // these operations, DAG memoization will ensure that a single node is
5206 // used for both shuffles.
5207 unsigned WhichResult;
5208 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5209 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5210 V1, V2).getValue(WhichResult);
5211 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5212 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5213 V1, V2).getValue(WhichResult);
5214 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5215 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5216 V1, V2).getValue(WhichResult);
5218 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5219 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5220 V1, V1).getValue(WhichResult);
5221 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5222 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5223 V1, V1).getValue(WhichResult);
5224 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5225 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5226 V1, V1).getValue(WhichResult);
5229 // If the shuffle is not directly supported and it has 4 elements, use
5230 // the PerfectShuffle-generated table to synthesize it from other shuffles.
5231 unsigned NumElts = VT.getVectorNumElements();
5233 unsigned PFIndexes[4];
5234 for (unsigned i = 0; i != 4; ++i) {
5235 if (ShuffleMask[i] < 0)
5238 PFIndexes[i] = ShuffleMask[i];
5241 // Compute the index in the perfect shuffle table.
5242 unsigned PFTableIndex =
5243 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5244 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5245 unsigned Cost = (PFEntry >> 30);
5248 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5251 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
5252 if (EltSize >= 32) {
5253 // Do the expansion with floating-point types, since that is what the VFP
5254 // registers are defined to use, and since i64 is not legal.
5255 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5256 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
5257 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5258 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
5259 SmallVector<SDValue, 8> Ops;
5260 for (unsigned i = 0; i < NumElts; ++i) {
5261 if (ShuffleMask[i] < 0)
5262 Ops.push_back(DAG.getUNDEF(EltVT));
5264 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5265 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5266 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5269 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, &Ops[0],NumElts);
5270 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
5273 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5274 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5276 if (VT == MVT::v8i8) {
5277 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5278 if (NewOp.getNode())
5285 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5286 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5287 SDValue Lane = Op.getOperand(2);
5288 if (!isa<ConstantSDNode>(Lane))
5294 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5295 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
5296 SDValue Lane = Op.getOperand(1);
5297 if (!isa<ConstantSDNode>(Lane))
5300 SDValue Vec = Op.getOperand(0);
5301 if (Op.getValueType() == MVT::i32 &&
5302 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
5304 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5310 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5311 // The only time a CONCAT_VECTORS operation can have legal types is when
5312 // two 64-bit vectors are concatenated to a 128-bit vector.
5313 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5314 "unexpected CONCAT_VECTORS");
5316 SDValue Val = DAG.getUNDEF(MVT::v2f64);
5317 SDValue Op0 = Op.getOperand(0);
5318 SDValue Op1 = Op.getOperand(1);
5319 if (Op0.getOpcode() != ISD::UNDEF)
5320 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
5321 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
5322 DAG.getIntPtrConstant(0));
5323 if (Op1.getOpcode() != ISD::UNDEF)
5324 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
5325 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
5326 DAG.getIntPtrConstant(1));
5327 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
5330 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5331 /// element has been zero/sign-extended, depending on the isSigned parameter,
5332 /// from an integer type half its size.
5333 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5335 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5336 EVT VT = N->getValueType(0);
5337 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5338 SDNode *BVN = N->getOperand(0).getNode();
5339 if (BVN->getValueType(0) != MVT::v4i32 ||
5340 BVN->getOpcode() != ISD::BUILD_VECTOR)
5342 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5343 unsigned HiElt = 1 - LoElt;
5344 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5345 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5346 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5347 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5348 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5351 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5352 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5355 if (Hi0->isNullValue() && Hi1->isNullValue())
5361 if (N->getOpcode() != ISD::BUILD_VECTOR)
5364 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5365 SDNode *Elt = N->getOperand(i).getNode();
5366 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5367 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5368 unsigned HalfSize = EltSize / 2;
5370 if (!isIntN(HalfSize, C->getSExtValue()))
5373 if (!isUIntN(HalfSize, C->getZExtValue()))
5384 /// isSignExtended - Check if a node is a vector value that is sign-extended
5385 /// or a constant BUILD_VECTOR with sign-extended elements.
5386 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5387 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5389 if (isExtendedBUILD_VECTOR(N, DAG, true))
5394 /// isZeroExtended - Check if a node is a vector value that is zero-extended
5395 /// or a constant BUILD_VECTOR with zero-extended elements.
5396 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5397 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5399 if (isExtendedBUILD_VECTOR(N, DAG, false))
5404 static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5405 if (OrigVT.getSizeInBits() >= 64)
5408 assert(OrigVT.isSimple() && "Expecting a simple value type");
5410 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5411 switch (OrigSimpleTy) {
5412 default: llvm_unreachable("Unexpected Vector Type");
5421 /// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5422 /// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5423 /// We insert the required extension here to get the vector to fill a D register.
5424 static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5427 unsigned ExtOpcode) {
5428 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5429 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5430 // 64-bits we need to insert a new extension so that it will be 64-bits.
5431 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5432 if (OrigTy.getSizeInBits() >= 64)
5435 // Must extend size to at least 64 bits to be used as an operand for VMULL.
5436 EVT NewVT = getExtensionTo64Bits(OrigTy);
5438 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
5441 /// SkipLoadExtensionForVMULL - return a load of the original vector size that
5442 /// does not do any sign/zero extension. If the original vector is less
5443 /// than 64 bits, an appropriate extension will be added after the load to
5444 /// reach a total size of 64 bits. We have to add the extension separately
5445 /// because ARM does not have a sign/zero extending load for vectors.
5446 static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
5447 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5449 // The load already has the right type.
5450 if (ExtendedTy == LD->getMemoryVT())
5451 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
5452 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5453 LD->isNonTemporal(), LD->isInvariant(),
5454 LD->getAlignment());
5456 // We need to create a zextload/sextload. We cannot just create a load
5457 // followed by a zext/zext node because LowerMUL is also run during normal
5458 // operation legalization where we can't create illegal types.
5459 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
5460 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
5461 LD->getMemoryVT(), LD->isVolatile(),
5462 LD->isNonTemporal(), LD->getAlignment());
5465 /// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5466 /// extending load, or BUILD_VECTOR with extended elements, return the
5467 /// unextended value. The unextended vector should be 64 bits so that it can
5468 /// be used as an operand to a VMULL instruction. If the original vector size
5469 /// before extension is less than 64 bits we add a an extension to resize
5470 /// the vector to 64 bits.
5471 static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
5472 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
5473 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5474 N->getOperand(0)->getValueType(0),
5478 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
5479 return SkipLoadExtensionForVMULL(LD, DAG);
5481 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5482 // have been legalized as a BITCAST from v4i32.
5483 if (N->getOpcode() == ISD::BITCAST) {
5484 SDNode *BVN = N->getOperand(0).getNode();
5485 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5486 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5487 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5488 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
5489 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5491 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5492 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5493 EVT VT = N->getValueType(0);
5494 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5495 unsigned NumElts = VT.getVectorNumElements();
5496 MVT TruncVT = MVT::getIntegerVT(EltSize);
5497 SmallVector<SDValue, 8> Ops;
5498 for (unsigned i = 0; i != NumElts; ++i) {
5499 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5500 const APInt &CInt = C->getAPIntValue();
5501 // Element types smaller than 32 bits are not legal, so use i32 elements.
5502 // The values are implicitly truncated so sext vs. zext doesn't matter.
5503 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
5505 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
5506 MVT::getVectorVT(TruncVT, NumElts), Ops.data(), NumElts);
5509 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5510 unsigned Opcode = N->getOpcode();
5511 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5512 SDNode *N0 = N->getOperand(0).getNode();
5513 SDNode *N1 = N->getOperand(1).getNode();
5514 return N0->hasOneUse() && N1->hasOneUse() &&
5515 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5520 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5521 unsigned Opcode = N->getOpcode();
5522 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5523 SDNode *N0 = N->getOperand(0).getNode();
5524 SDNode *N1 = N->getOperand(1).getNode();
5525 return N0->hasOneUse() && N1->hasOneUse() &&
5526 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5531 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5532 // Multiplications are only custom-lowered for 128-bit vectors so that
5533 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5534 EVT VT = Op.getValueType();
5535 assert(VT.is128BitVector() && VT.isInteger() &&
5536 "unexpected type for custom-lowering ISD::MUL");
5537 SDNode *N0 = Op.getOperand(0).getNode();
5538 SDNode *N1 = Op.getOperand(1).getNode();
5539 unsigned NewOpc = 0;
5541 bool isN0SExt = isSignExtended(N0, DAG);
5542 bool isN1SExt = isSignExtended(N1, DAG);
5543 if (isN0SExt && isN1SExt)
5544 NewOpc = ARMISD::VMULLs;
5546 bool isN0ZExt = isZeroExtended(N0, DAG);
5547 bool isN1ZExt = isZeroExtended(N1, DAG);
5548 if (isN0ZExt && isN1ZExt)
5549 NewOpc = ARMISD::VMULLu;
5550 else if (isN1SExt || isN1ZExt) {
5551 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5552 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5553 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5554 NewOpc = ARMISD::VMULLs;
5556 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5557 NewOpc = ARMISD::VMULLu;
5559 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5561 NewOpc = ARMISD::VMULLu;
5567 if (VT == MVT::v2i64)
5568 // Fall through to expand this. It is not legal.
5571 // Other vector multiplications are legal.
5576 // Legalize to a VMULL instruction.
5579 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
5581 Op0 = SkipExtensionForVMULL(N0, DAG);
5582 assert(Op0.getValueType().is64BitVector() &&
5583 Op1.getValueType().is64BitVector() &&
5584 "unexpected types for extended operands to VMULL");
5585 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5588 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5589 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5596 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5597 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
5598 EVT Op1VT = Op1.getValueType();
5599 return DAG.getNode(N0->getOpcode(), DL, VT,
5600 DAG.getNode(NewOpc, DL, VT,
5601 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5602 DAG.getNode(NewOpc, DL, VT,
5603 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
5607 LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
5609 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5610 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5611 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5612 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5613 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5614 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5615 // Get reciprocal estimate.
5616 // float4 recip = vrecpeq_f32(yf);
5617 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
5618 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5619 // Because char has a smaller range than uchar, we can actually get away
5620 // without any newton steps. This requires that we use a weird bias
5621 // of 0xb000, however (again, this has been exhaustively tested).
5622 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5623 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5624 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5625 Y = DAG.getConstant(0xb000, MVT::i32);
5626 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5627 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5628 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5629 // Convert back to short.
5630 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5631 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5636 LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
5638 // Convert to float.
5639 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5640 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5641 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5642 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5643 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5644 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
5646 // Use reciprocal estimate and one refinement step.
5647 // float4 recip = vrecpeq_f32(yf);
5648 // recip *= vrecpsq_f32(yf, recip);
5649 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
5650 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
5651 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
5652 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5654 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5655 // Because short has a smaller range than ushort, we can actually get away
5656 // with only a single newton step. This requires that we use a weird bias
5657 // of 89, however (again, this has been exhaustively tested).
5658 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
5659 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5660 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5661 N1 = DAG.getConstant(0x89, MVT::i32);
5662 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5663 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5664 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5665 // Convert back to integer and return.
5666 // return vmovn_s32(vcvt_s32_f32(result));
5667 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5668 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5672 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5673 EVT VT = Op.getValueType();
5674 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5675 "unexpected type for custom-lowering ISD::SDIV");
5678 SDValue N0 = Op.getOperand(0);
5679 SDValue N1 = Op.getOperand(1);
5682 if (VT == MVT::v8i8) {
5683 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5684 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
5686 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5687 DAG.getIntPtrConstant(4));
5688 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5689 DAG.getIntPtrConstant(4));
5690 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5691 DAG.getIntPtrConstant(0));
5692 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5693 DAG.getIntPtrConstant(0));
5695 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5696 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5698 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5699 N0 = LowerCONCAT_VECTORS(N0, DAG);
5701 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5704 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5707 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5708 EVT VT = Op.getValueType();
5709 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5710 "unexpected type for custom-lowering ISD::UDIV");
5713 SDValue N0 = Op.getOperand(0);
5714 SDValue N1 = Op.getOperand(1);
5717 if (VT == MVT::v8i8) {
5718 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5719 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
5721 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5722 DAG.getIntPtrConstant(4));
5723 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5724 DAG.getIntPtrConstant(4));
5725 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5726 DAG.getIntPtrConstant(0));
5727 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5728 DAG.getIntPtrConstant(0));
5730 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5731 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
5733 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5734 N0 = LowerCONCAT_VECTORS(N0, DAG);
5736 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
5737 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5742 // v4i16 sdiv ... Convert to float.
5743 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5744 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5745 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5746 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5747 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5748 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
5750 // Use reciprocal estimate and two refinement steps.
5751 // float4 recip = vrecpeq_f32(yf);
5752 // recip *= vrecpsq_f32(yf, recip);
5753 // recip *= vrecpsq_f32(yf, recip);
5754 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
5755 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
5756 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
5757 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5759 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5760 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
5761 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5763 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5764 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5765 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
5766 // and that it will never cause us to return an answer too large).
5767 // float4 result = as_float4(as_int4(xf*recip) + 2);
5768 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5769 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
5770 N1 = DAG.getConstant(2, MVT::i32);
5771 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5772 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5773 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5774 // Convert back to integer and return.
5775 // return vmovn_u32(vcvt_s32_f32(result));
5776 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5777 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5781 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
5782 EVT VT = Op.getNode()->getValueType(0);
5783 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
5786 bool ExtraOp = false;
5787 switch (Op.getOpcode()) {
5788 default: llvm_unreachable("Invalid code");
5789 case ISD::ADDC: Opc = ARMISD::ADDC; break;
5790 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
5791 case ISD::SUBC: Opc = ARMISD::SUBC; break;
5792 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
5796 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
5798 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
5799 Op.getOperand(1), Op.getOperand(2));
5802 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
5803 // Monotonic load/store is legal for all targets
5804 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
5807 // Aquire/Release load/store is not legal for targets without a
5808 // dmb or equivalent available.
5813 ReplaceATOMIC_OP_64(SDNode *Node, SmallVectorImpl<SDValue>& Results,
5814 SelectionDAG &DAG, unsigned NewOp) {
5816 assert (Node->getValueType(0) == MVT::i64 &&
5817 "Only know how to expand i64 atomics");
5819 SmallVector<SDValue, 6> Ops;
5820 Ops.push_back(Node->getOperand(0)); // Chain
5821 Ops.push_back(Node->getOperand(1)); // Ptr
5823 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5824 Node->getOperand(2), DAG.getIntPtrConstant(0)));
5825 // High part of Val1
5826 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5827 Node->getOperand(2), DAG.getIntPtrConstant(1)));
5828 if (NewOp == ARMISD::ATOMCMPXCHG64_DAG) {
5829 // High part of Val1
5830 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5831 Node->getOperand(3), DAG.getIntPtrConstant(0)));
5832 // High part of Val2
5833 Ops.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
5834 Node->getOperand(3), DAG.getIntPtrConstant(1)));
5836 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
5838 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops.data(), Ops.size(), MVT::i64,
5839 cast<MemSDNode>(Node)->getMemOperand());
5840 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1) };
5841 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
5842 Results.push_back(Result.getValue(2));
5845 static void ReplaceREADCYCLECOUNTER(SDNode *N,
5846 SmallVectorImpl<SDValue> &Results,
5848 const ARMSubtarget *Subtarget) {
5850 SDValue Cycles32, OutChain;
5852 if (Subtarget->hasPerfMon()) {
5853 // Under Power Management extensions, the cycle-count is:
5854 // mrc p15, #0, <Rt>, c9, c13, #0
5855 SDValue Ops[] = { N->getOperand(0), // Chain
5856 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
5857 DAG.getConstant(15, MVT::i32),
5858 DAG.getConstant(0, MVT::i32),
5859 DAG.getConstant(9, MVT::i32),
5860 DAG.getConstant(13, MVT::i32),
5861 DAG.getConstant(0, MVT::i32)
5864 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
5865 DAG.getVTList(MVT::i32, MVT::Other), &Ops[0],
5866 array_lengthof(Ops));
5867 OutChain = Cycles32.getValue(1);
5869 // Intrinsic is defined to return 0 on unsupported platforms. Technically
5870 // there are older ARM CPUs that have implementation-specific ways of
5871 // obtaining this information (FIXME!).
5872 Cycles32 = DAG.getConstant(0, MVT::i32);
5873 OutChain = DAG.getEntryNode();
5877 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
5878 Cycles32, DAG.getConstant(0, MVT::i32));
5879 Results.push_back(Cycles64);
5880 Results.push_back(OutChain);
5883 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
5884 switch (Op.getOpcode()) {
5885 default: llvm_unreachable("Don't know how to custom lower this!");
5886 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5887 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
5888 case ISD::GlobalAddress:
5889 return Subtarget->isTargetDarwin() ? LowerGlobalAddressDarwin(Op, DAG) :
5890 LowerGlobalAddressELF(Op, DAG);
5891 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5892 case ISD::SELECT: return LowerSELECT(Op, DAG);
5893 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
5894 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
5895 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
5896 case ISD::VASTART: return LowerVASTART(Op, DAG);
5897 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
5898 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
5899 case ISD::SINT_TO_FP:
5900 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
5901 case ISD::FP_TO_SINT:
5902 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
5903 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
5904 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5905 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5906 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
5907 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
5908 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
5909 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
5911 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
5914 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
5915 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
5916 case ISD::SRL_PARTS:
5917 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
5918 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
5919 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
5920 case ISD::SETCC: return LowerVSETCC(Op, DAG);
5921 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
5922 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
5923 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5924 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5925 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5926 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
5927 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
5928 case ISD::MUL: return LowerMUL(Op, DAG);
5929 case ISD::SDIV: return LowerSDIV(Op, DAG);
5930 case ISD::UDIV: return LowerUDIV(Op, DAG);
5934 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
5935 case ISD::ATOMIC_LOAD:
5936 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
5938 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
5942 /// ReplaceNodeResults - Replace the results of node with an illegal result
5943 /// type with new values built out of custom code.
5944 void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
5945 SmallVectorImpl<SDValue>&Results,
5946 SelectionDAG &DAG) const {
5948 switch (N->getOpcode()) {
5950 llvm_unreachable("Don't know how to custom expand this!");
5952 Res = ExpandBITCAST(N, DAG);
5954 case ISD::SIGN_EXTEND:
5955 case ISD::ZERO_EXTEND:
5956 Res = ExpandVectorExtension(N, DAG);
5960 Res = Expand64BitShift(N, DAG, Subtarget);
5962 case ISD::READCYCLECOUNTER:
5963 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
5965 case ISD::ATOMIC_LOAD_ADD:
5966 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMADD64_DAG);
5968 case ISD::ATOMIC_LOAD_AND:
5969 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMAND64_DAG);
5971 case ISD::ATOMIC_LOAD_NAND:
5972 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMNAND64_DAG);
5974 case ISD::ATOMIC_LOAD_OR:
5975 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMOR64_DAG);
5977 case ISD::ATOMIC_LOAD_SUB:
5978 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSUB64_DAG);
5980 case ISD::ATOMIC_LOAD_XOR:
5981 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMXOR64_DAG);
5983 case ISD::ATOMIC_SWAP:
5984 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMSWAP64_DAG);
5986 case ISD::ATOMIC_CMP_SWAP:
5987 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMCMPXCHG64_DAG);
5989 case ISD::ATOMIC_LOAD_MIN:
5990 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMIN64_DAG);
5992 case ISD::ATOMIC_LOAD_UMIN:
5993 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMIN64_DAG);
5995 case ISD::ATOMIC_LOAD_MAX:
5996 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMMAX64_DAG);
5998 case ISD::ATOMIC_LOAD_UMAX:
5999 ReplaceATOMIC_OP_64(N, Results, DAG, ARMISD::ATOMUMAX64_DAG);
6003 Results.push_back(Res);
6006 //===----------------------------------------------------------------------===//
6007 // ARM Scheduler Hooks
6008 //===----------------------------------------------------------------------===//
6011 ARMTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
6012 MachineBasicBlock *BB,
6013 unsigned Size) const {
6014 unsigned dest = MI->getOperand(0).getReg();
6015 unsigned ptr = MI->getOperand(1).getReg();
6016 unsigned oldval = MI->getOperand(2).getReg();
6017 unsigned newval = MI->getOperand(3).getReg();
6018 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6019 DebugLoc dl = MI->getDebugLoc();
6020 bool isThumb2 = Subtarget->isThumb2();
6022 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
6023 unsigned scratch = MRI.createVirtualRegister(isThumb2 ?
6024 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6025 (const TargetRegisterClass*)&ARM::GPRRegClass);
6028 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
6029 MRI.constrainRegClass(oldval, &ARM::rGPRRegClass);
6030 MRI.constrainRegClass(newval, &ARM::rGPRRegClass);
6033 unsigned ldrOpc, strOpc;
6035 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
6037 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
6038 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
6041 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
6042 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
6045 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
6046 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
6050 MachineFunction *MF = BB->getParent();
6051 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6052 MachineFunction::iterator It = BB;
6053 ++It; // insert the new blocks after the current block
6055 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
6056 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
6057 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6058 MF->insert(It, loop1MBB);
6059 MF->insert(It, loop2MBB);
6060 MF->insert(It, exitMBB);
6062 // Transfer the remainder of BB and its successor edges to exitMBB.
6063 exitMBB->splice(exitMBB->begin(), BB,
6064 llvm::next(MachineBasicBlock::iterator(MI)),
6066 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6070 // fallthrough --> loop1MBB
6071 BB->addSuccessor(loop1MBB);
6074 // ldrex dest, [ptr]
6078 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
6079 if (ldrOpc == ARM::t2LDREX)
6081 AddDefaultPred(MIB);
6082 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6083 .addReg(dest).addReg(oldval));
6084 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6085 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6086 BB->addSuccessor(loop2MBB);
6087 BB->addSuccessor(exitMBB);
6090 // strex scratch, newval, [ptr]
6094 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(newval).addReg(ptr);
6095 if (strOpc == ARM::t2STREX)
6097 AddDefaultPred(MIB);
6098 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6099 .addReg(scratch).addImm(0));
6100 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6101 .addMBB(loop1MBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6102 BB->addSuccessor(loop1MBB);
6103 BB->addSuccessor(exitMBB);
6109 MI->eraseFromParent(); // The instruction is gone now.
6115 ARMTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
6116 unsigned Size, unsigned BinOpcode) const {
6117 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
6118 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6120 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6121 MachineFunction *MF = BB->getParent();
6122 MachineFunction::iterator It = BB;
6125 unsigned dest = MI->getOperand(0).getReg();
6126 unsigned ptr = MI->getOperand(1).getReg();
6127 unsigned incr = MI->getOperand(2).getReg();
6128 DebugLoc dl = MI->getDebugLoc();
6129 bool isThumb2 = Subtarget->isThumb2();
6131 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
6133 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
6134 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
6137 unsigned ldrOpc, strOpc;
6139 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
6141 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
6142 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
6145 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
6146 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
6149 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
6150 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
6154 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6155 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6156 MF->insert(It, loopMBB);
6157 MF->insert(It, exitMBB);
6159 // Transfer the remainder of BB and its successor edges to exitMBB.
6160 exitMBB->splice(exitMBB->begin(), BB,
6161 llvm::next(MachineBasicBlock::iterator(MI)),
6163 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6165 const TargetRegisterClass *TRC = isThumb2 ?
6166 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6167 (const TargetRegisterClass*)&ARM::GPRRegClass;
6168 unsigned scratch = MRI.createVirtualRegister(TRC);
6169 unsigned scratch2 = (!BinOpcode) ? incr : MRI.createVirtualRegister(TRC);
6173 // fallthrough --> loopMBB
6174 BB->addSuccessor(loopMBB);
6178 // <binop> scratch2, dest, incr
6179 // strex scratch, scratch2, ptr
6182 // fallthrough --> exitMBB
6184 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
6185 if (ldrOpc == ARM::t2LDREX)
6187 AddDefaultPred(MIB);
6189 // operand order needs to go the other way for NAND
6190 if (BinOpcode == ARM::BICrr || BinOpcode == ARM::t2BICrr)
6191 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
6192 addReg(incr).addReg(dest)).addReg(0);
6194 AddDefaultPred(BuildMI(BB, dl, TII->get(BinOpcode), scratch2).
6195 addReg(dest).addReg(incr)).addReg(0);
6198 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
6199 if (strOpc == ARM::t2STREX)
6201 AddDefaultPred(MIB);
6202 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6203 .addReg(scratch).addImm(0));
6204 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6205 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6207 BB->addSuccessor(loopMBB);
6208 BB->addSuccessor(exitMBB);
6214 MI->eraseFromParent(); // The instruction is gone now.
6220 ARMTargetLowering::EmitAtomicBinaryMinMax(MachineInstr *MI,
6221 MachineBasicBlock *BB,
6224 ARMCC::CondCodes Cond) const {
6225 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6227 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6228 MachineFunction *MF = BB->getParent();
6229 MachineFunction::iterator It = BB;
6232 unsigned dest = MI->getOperand(0).getReg();
6233 unsigned ptr = MI->getOperand(1).getReg();
6234 unsigned incr = MI->getOperand(2).getReg();
6235 unsigned oldval = dest;
6236 DebugLoc dl = MI->getDebugLoc();
6237 bool isThumb2 = Subtarget->isThumb2();
6239 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
6241 MRI.constrainRegClass(dest, &ARM::rGPRRegClass);
6242 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
6245 unsigned ldrOpc, strOpc, extendOpc;
6247 default: llvm_unreachable("unsupported size for AtomicCmpSwap!");
6249 ldrOpc = isThumb2 ? ARM::t2LDREXB : ARM::LDREXB;
6250 strOpc = isThumb2 ? ARM::t2STREXB : ARM::STREXB;
6251 extendOpc = isThumb2 ? ARM::t2SXTB : ARM::SXTB;
6254 ldrOpc = isThumb2 ? ARM::t2LDREXH : ARM::LDREXH;
6255 strOpc = isThumb2 ? ARM::t2STREXH : ARM::STREXH;
6256 extendOpc = isThumb2 ? ARM::t2SXTH : ARM::SXTH;
6259 ldrOpc = isThumb2 ? ARM::t2LDREX : ARM::LDREX;
6260 strOpc = isThumb2 ? ARM::t2STREX : ARM::STREX;
6265 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6266 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6267 MF->insert(It, loopMBB);
6268 MF->insert(It, exitMBB);
6270 // Transfer the remainder of BB and its successor edges to exitMBB.
6271 exitMBB->splice(exitMBB->begin(), BB,
6272 llvm::next(MachineBasicBlock::iterator(MI)),
6274 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6276 const TargetRegisterClass *TRC = isThumb2 ?
6277 (const TargetRegisterClass*)&ARM::rGPRRegClass :
6278 (const TargetRegisterClass*)&ARM::GPRRegClass;
6279 unsigned scratch = MRI.createVirtualRegister(TRC);
6280 unsigned scratch2 = MRI.createVirtualRegister(TRC);
6284 // fallthrough --> loopMBB
6285 BB->addSuccessor(loopMBB);
6289 // (sign extend dest, if required)
6291 // cmov.cond scratch2, incr, dest
6292 // strex scratch, scratch2, ptr
6295 // fallthrough --> exitMBB
6297 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(ldrOpc), dest).addReg(ptr);
6298 if (ldrOpc == ARM::t2LDREX)
6300 AddDefaultPred(MIB);
6302 // Sign extend the value, if necessary.
6303 if (signExtend && extendOpc) {
6304 oldval = MRI.createVirtualRegister(&ARM::GPRRegClass);
6305 AddDefaultPred(BuildMI(BB, dl, TII->get(extendOpc), oldval)
6310 // Build compare and cmov instructions.
6311 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
6312 .addReg(oldval).addReg(incr));
6313 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2MOVCCr : ARM::MOVCCr), scratch2)
6314 .addReg(incr).addReg(oldval).addImm(Cond).addReg(ARM::CPSR);
6316 MIB = BuildMI(BB, dl, TII->get(strOpc), scratch).addReg(scratch2).addReg(ptr);
6317 if (strOpc == ARM::t2STREX)
6319 AddDefaultPred(MIB);
6320 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6321 .addReg(scratch).addImm(0));
6322 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6323 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6325 BB->addSuccessor(loopMBB);
6326 BB->addSuccessor(exitMBB);
6332 MI->eraseFromParent(); // The instruction is gone now.
6338 ARMTargetLowering::EmitAtomicBinary64(MachineInstr *MI, MachineBasicBlock *BB,
6339 unsigned Op1, unsigned Op2,
6340 bool NeedsCarry, bool IsCmpxchg,
6341 bool IsMinMax, ARMCC::CondCodes CC) const {
6342 // This also handles ATOMIC_SWAP, indicated by Op1==0.
6343 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6345 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6346 MachineFunction *MF = BB->getParent();
6347 MachineFunction::iterator It = BB;
6350 unsigned destlo = MI->getOperand(0).getReg();
6351 unsigned desthi = MI->getOperand(1).getReg();
6352 unsigned ptr = MI->getOperand(2).getReg();
6353 unsigned vallo = MI->getOperand(3).getReg();
6354 unsigned valhi = MI->getOperand(4).getReg();
6355 DebugLoc dl = MI->getDebugLoc();
6356 bool isThumb2 = Subtarget->isThumb2();
6358 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
6360 MRI.constrainRegClass(destlo, &ARM::rGPRRegClass);
6361 MRI.constrainRegClass(desthi, &ARM::rGPRRegClass);
6362 MRI.constrainRegClass(ptr, &ARM::rGPRRegClass);
6363 MRI.constrainRegClass(vallo, &ARM::rGPRRegClass);
6364 MRI.constrainRegClass(valhi, &ARM::rGPRRegClass);
6367 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6368 MachineBasicBlock *contBB = 0, *cont2BB = 0;
6369 if (IsCmpxchg || IsMinMax)
6370 contBB = MF->CreateMachineBasicBlock(LLVM_BB);
6372 cont2BB = MF->CreateMachineBasicBlock(LLVM_BB);
6373 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6375 MF->insert(It, loopMBB);
6376 if (IsCmpxchg || IsMinMax) MF->insert(It, contBB);
6377 if (IsCmpxchg) MF->insert(It, cont2BB);
6378 MF->insert(It, exitMBB);
6380 // Transfer the remainder of BB and its successor edges to exitMBB.
6381 exitMBB->splice(exitMBB->begin(), BB,
6382 llvm::next(MachineBasicBlock::iterator(MI)),
6384 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6386 const TargetRegisterClass *TRC = isThumb2 ?
6387 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6388 (const TargetRegisterClass*)&ARM::GPRRegClass;
6389 unsigned storesuccess = MRI.createVirtualRegister(TRC);
6393 // fallthrough --> loopMBB
6394 BB->addSuccessor(loopMBB);
6397 // ldrexd r2, r3, ptr
6398 // <binopa> r0, r2, incr
6399 // <binopb> r1, r3, incr
6400 // strexd storesuccess, r0, r1, ptr
6401 // cmp storesuccess, #0
6403 // fallthrough --> exitMBB
6408 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2LDREXD))
6409 .addReg(destlo, RegState::Define)
6410 .addReg(desthi, RegState::Define)
6413 unsigned GPRPair0 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6414 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDREXD))
6415 .addReg(GPRPair0, RegState::Define).addReg(ptr));
6416 // Copy r2/r3 into dest. (This copy will normally be coalesced.)
6417 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), destlo)
6418 .addReg(GPRPair0, 0, ARM::gsub_0);
6419 BuildMI(BB, dl, TII->get(TargetOpcode::COPY), desthi)
6420 .addReg(GPRPair0, 0, ARM::gsub_1);
6423 unsigned StoreLo, StoreHi;
6426 for (unsigned i = 0; i < 2; i++) {
6427 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr :
6429 .addReg(i == 0 ? destlo : desthi)
6430 .addReg(i == 0 ? vallo : valhi));
6431 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6432 .addMBB(exitMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6433 BB->addSuccessor(exitMBB);
6434 BB->addSuccessor(i == 0 ? contBB : cont2BB);
6435 BB = (i == 0 ? contBB : cont2BB);
6438 // Copy to physregs for strexd
6439 StoreLo = MI->getOperand(5).getReg();
6440 StoreHi = MI->getOperand(6).getReg();
6442 // Perform binary operation
6443 unsigned tmpRegLo = MRI.createVirtualRegister(TRC);
6444 AddDefaultPred(BuildMI(BB, dl, TII->get(Op1), tmpRegLo)
6445 .addReg(destlo).addReg(vallo))
6446 .addReg(NeedsCarry ? ARM::CPSR : 0, getDefRegState(NeedsCarry));
6447 unsigned tmpRegHi = MRI.createVirtualRegister(TRC);
6448 AddDefaultPred(BuildMI(BB, dl, TII->get(Op2), tmpRegHi)
6449 .addReg(desthi).addReg(valhi))
6450 .addReg(IsMinMax ? ARM::CPSR : 0, getDefRegState(IsMinMax));
6455 // Copy to physregs for strexd
6460 // Compare and branch to exit block.
6461 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6462 .addMBB(exitMBB).addImm(CC).addReg(ARM::CPSR);
6463 BB->addSuccessor(exitMBB);
6464 BB->addSuccessor(contBB);
6472 MRI.constrainRegClass(StoreLo, &ARM::rGPRRegClass);
6473 MRI.constrainRegClass(StoreHi, &ARM::rGPRRegClass);
6474 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2STREXD), storesuccess)
6475 .addReg(StoreLo).addReg(StoreHi).addReg(ptr));
6477 // Marshal a pair...
6478 unsigned StorePair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6479 unsigned UndefPair = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6480 unsigned r1 = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
6481 BuildMI(BB, dl, TII->get(TargetOpcode::IMPLICIT_DEF), UndefPair);
6482 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), r1)
6485 .addImm(ARM::gsub_0);
6486 BuildMI(BB, dl, TII->get(TargetOpcode::INSERT_SUBREG), StorePair)
6489 .addImm(ARM::gsub_1);
6492 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::STREXD), storesuccess)
6493 .addReg(StorePair).addReg(ptr));
6496 AddDefaultPred(BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
6497 .addReg(storesuccess).addImm(0));
6498 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
6499 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
6501 BB->addSuccessor(loopMBB);
6502 BB->addSuccessor(exitMBB);
6508 MI->eraseFromParent(); // The instruction is gone now.
6513 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6514 /// registers the function context.
6515 void ARMTargetLowering::
6516 SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6517 MachineBasicBlock *DispatchBB, int FI) const {
6518 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6519 DebugLoc dl = MI->getDebugLoc();
6520 MachineFunction *MF = MBB->getParent();
6521 MachineRegisterInfo *MRI = &MF->getRegInfo();
6522 MachineConstantPool *MCP = MF->getConstantPool();
6523 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6524 const Function *F = MF->getFunction();
6526 bool isThumb = Subtarget->isThumb();
6527 bool isThumb2 = Subtarget->isThumb2();
6529 unsigned PCLabelId = AFI->createPICLabelUId();
6530 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
6531 ARMConstantPoolValue *CPV =
6532 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6533 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6535 const TargetRegisterClass *TRC = isThumb ?
6536 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6537 (const TargetRegisterClass*)&ARM::GPRRegClass;
6539 // Grab constant pool and fixed stack memory operands.
6540 MachineMemOperand *CPMMO =
6541 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6542 MachineMemOperand::MOLoad, 4, 4);
6544 MachineMemOperand *FIMMOSt =
6545 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6546 MachineMemOperand::MOStore, 4, 4);
6548 // Load the address of the dispatch MBB into the jump buffer.
6550 // Incoming value: jbuf
6551 // ldr.n r5, LCPI1_1
6554 // str r5, [$jbuf, #+4] ; &jbuf[1]
6555 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6556 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6557 .addConstantPoolIndex(CPI)
6558 .addMemOperand(CPMMO));
6559 // Set the low bit because of thumb mode.
6560 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6562 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6563 .addReg(NewVReg1, RegState::Kill)
6565 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6566 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6567 .addReg(NewVReg2, RegState::Kill)
6569 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6570 .addReg(NewVReg3, RegState::Kill)
6572 .addImm(36) // &jbuf[1] :: pc
6573 .addMemOperand(FIMMOSt));
6574 } else if (isThumb) {
6575 // Incoming value: jbuf
6576 // ldr.n r1, LCPI1_4
6580 // add r2, $jbuf, #+4 ; &jbuf[1]
6582 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6583 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6584 .addConstantPoolIndex(CPI)
6585 .addMemOperand(CPMMO));
6586 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6587 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6588 .addReg(NewVReg1, RegState::Kill)
6590 // Set the low bit because of thumb mode.
6591 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6592 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6593 .addReg(ARM::CPSR, RegState::Define)
6595 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6596 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6597 .addReg(ARM::CPSR, RegState::Define)
6598 .addReg(NewVReg2, RegState::Kill)
6599 .addReg(NewVReg3, RegState::Kill));
6600 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6601 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6603 .addImm(36)); // &jbuf[1] :: pc
6604 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6605 .addReg(NewVReg4, RegState::Kill)
6606 .addReg(NewVReg5, RegState::Kill)
6608 .addMemOperand(FIMMOSt));
6610 // Incoming value: jbuf
6613 // str r1, [$jbuf, #+4] ; &jbuf[1]
6614 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6615 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6616 .addConstantPoolIndex(CPI)
6618 .addMemOperand(CPMMO));
6619 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6620 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6621 .addReg(NewVReg1, RegState::Kill)
6622 .addImm(PCLabelId));
6623 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6624 .addReg(NewVReg2, RegState::Kill)
6626 .addImm(36) // &jbuf[1] :: pc
6627 .addMemOperand(FIMMOSt));
6631 MachineBasicBlock *ARMTargetLowering::
6632 EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6633 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6634 DebugLoc dl = MI->getDebugLoc();
6635 MachineFunction *MF = MBB->getParent();
6636 MachineRegisterInfo *MRI = &MF->getRegInfo();
6637 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6638 MachineFrameInfo *MFI = MF->getFrameInfo();
6639 int FI = MFI->getFunctionContextIndex();
6641 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6642 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6643 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
6645 // Get a mapping of the call site numbers to all of the landing pads they're
6647 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6648 unsigned MaxCSNum = 0;
6649 MachineModuleInfo &MMI = MF->getMMI();
6650 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6652 if (!BB->isLandingPad()) continue;
6654 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6656 for (MachineBasicBlock::iterator
6657 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6658 if (!II->isEHLabel()) continue;
6660 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
6661 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
6663 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6664 for (SmallVectorImpl<unsigned>::iterator
6665 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6666 CSI != CSE; ++CSI) {
6667 CallSiteNumToLPad[*CSI].push_back(BB);
6668 MaxCSNum = std::max(MaxCSNum, *CSI);
6674 // Get an ordered list of the machine basic blocks for the jump table.
6675 std::vector<MachineBasicBlock*> LPadList;
6676 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
6677 LPadList.reserve(CallSiteNumToLPad.size());
6678 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6679 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6680 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6681 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
6682 LPadList.push_back(*II);
6683 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6687 assert(!LPadList.empty() &&
6688 "No landing pad destinations for the dispatch jump table!");
6690 // Create the jump table and associated information.
6691 MachineJumpTableInfo *JTI =
6692 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6693 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6694 unsigned UId = AFI->createJumpTableUId();
6695 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
6697 // Create the MBBs for the dispatch code.
6699 // Shove the dispatch's address into the return slot in the function context.
6700 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6701 DispatchBB->setIsLandingPad();
6703 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
6704 unsigned trap_opcode;
6705 if (Subtarget->isThumb())
6706 trap_opcode = ARM::tTRAP;
6708 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6710 BuildMI(TrapBB, dl, TII->get(trap_opcode));
6711 DispatchBB->addSuccessor(TrapBB);
6713 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6714 DispatchBB->addSuccessor(DispContBB);
6717 MF->insert(MF->end(), DispatchBB);
6718 MF->insert(MF->end(), DispContBB);
6719 MF->insert(MF->end(), TrapBB);
6721 // Insert code into the entry block that creates and registers the function
6723 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6725 MachineMemOperand *FIMMOLd =
6726 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6727 MachineMemOperand::MOLoad |
6728 MachineMemOperand::MOVolatile, 4, 4);
6730 MachineInstrBuilder MIB;
6731 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6733 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6734 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6736 // Add a register mask with no preserved registers. This results in all
6737 // registers being marked as clobbered.
6738 MIB.addRegMask(RI.getNoPreservedMask());
6740 unsigned NumLPads = LPadList.size();
6741 if (Subtarget->isThumb2()) {
6742 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6743 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6746 .addMemOperand(FIMMOLd));
6748 if (NumLPads < 256) {
6749 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6751 .addImm(LPadList.size()));
6753 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6754 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
6755 .addImm(NumLPads & 0xFFFF));
6757 unsigned VReg2 = VReg1;
6758 if ((NumLPads & 0xFFFF0000) != 0) {
6759 VReg2 = MRI->createVirtualRegister(TRC);
6760 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6762 .addImm(NumLPads >> 16));
6765 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6770 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6775 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6776 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
6777 .addJumpTableIndex(MJTI)
6780 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6783 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6784 .addReg(NewVReg3, RegState::Kill)
6786 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6788 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
6789 .addReg(NewVReg4, RegState::Kill)
6791 .addJumpTableIndex(MJTI)
6793 } else if (Subtarget->isThumb()) {
6794 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6795 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6798 .addMemOperand(FIMMOLd));
6800 if (NumLPads < 256) {
6801 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6805 MachineConstantPool *ConstantPool = MF->getConstantPool();
6806 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6807 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6809 // MachineConstantPool wants an explicit alignment.
6810 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
6812 Align = getDataLayout()->getTypeAllocSize(C->getType());
6813 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6815 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6816 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6817 .addReg(VReg1, RegState::Define)
6818 .addConstantPoolIndex(Idx));
6819 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6824 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6829 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6830 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6831 .addReg(ARM::CPSR, RegState::Define)
6835 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6836 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
6837 .addJumpTableIndex(MJTI)
6840 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6841 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6842 .addReg(ARM::CPSR, RegState::Define)
6843 .addReg(NewVReg2, RegState::Kill)
6846 MachineMemOperand *JTMMOLd =
6847 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6848 MachineMemOperand::MOLoad, 4, 4);
6850 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6851 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6852 .addReg(NewVReg4, RegState::Kill)
6854 .addMemOperand(JTMMOLd));
6856 unsigned NewVReg6 = NewVReg5;
6857 if (RelocM == Reloc::PIC_) {
6858 NewVReg6 = MRI->createVirtualRegister(TRC);
6859 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6860 .addReg(ARM::CPSR, RegState::Define)
6861 .addReg(NewVReg5, RegState::Kill)
6865 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6866 .addReg(NewVReg6, RegState::Kill)
6867 .addJumpTableIndex(MJTI)
6870 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6871 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6874 .addMemOperand(FIMMOLd));
6876 if (NumLPads < 256) {
6877 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6880 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
6881 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6882 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
6883 .addImm(NumLPads & 0xFFFF));
6885 unsigned VReg2 = VReg1;
6886 if ((NumLPads & 0xFFFF0000) != 0) {
6887 VReg2 = MRI->createVirtualRegister(TRC);
6888 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6890 .addImm(NumLPads >> 16));
6893 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6897 MachineConstantPool *ConstantPool = MF->getConstantPool();
6898 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6899 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6901 // MachineConstantPool wants an explicit alignment.
6902 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
6904 Align = getDataLayout()->getTypeAllocSize(C->getType());
6905 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6907 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6908 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6909 .addReg(VReg1, RegState::Define)
6910 .addConstantPoolIndex(Idx)
6912 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6914 .addReg(VReg1, RegState::Kill));
6917 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6922 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6924 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
6926 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6927 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6928 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
6929 .addJumpTableIndex(MJTI)
6932 MachineMemOperand *JTMMOLd =
6933 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6934 MachineMemOperand::MOLoad, 4, 4);
6935 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6937 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6938 .addReg(NewVReg3, RegState::Kill)
6941 .addMemOperand(JTMMOLd));
6943 if (RelocM == Reloc::PIC_) {
6944 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6945 .addReg(NewVReg5, RegState::Kill)
6947 .addJumpTableIndex(MJTI)
6950 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6951 .addReg(NewVReg5, RegState::Kill)
6952 .addJumpTableIndex(MJTI)
6957 // Add the jump table entries as successors to the MBB.
6958 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
6959 for (std::vector<MachineBasicBlock*>::iterator
6960 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6961 MachineBasicBlock *CurMBB = *I;
6962 if (SeenMBBs.insert(CurMBB))
6963 DispContBB->addSuccessor(CurMBB);
6966 // N.B. the order the invoke BBs are processed in doesn't matter here.
6967 const uint16_t *SavedRegs = RI.getCalleeSavedRegs(MF);
6968 SmallVector<MachineBasicBlock*, 64> MBBLPads;
6969 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6970 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6971 MachineBasicBlock *BB = *I;
6973 // Remove the landing pad successor from the invoke block and replace it
6974 // with the new dispatch block.
6975 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6977 while (!Successors.empty()) {
6978 MachineBasicBlock *SMBB = Successors.pop_back_val();
6979 if (SMBB->isLandingPad()) {
6980 BB->removeSuccessor(SMBB);
6981 MBBLPads.push_back(SMBB);
6985 BB->addSuccessor(DispatchBB);
6987 // Find the invoke call and mark all of the callee-saved registers as
6988 // 'implicit defined' so that they're spilled. This prevents code from
6989 // moving instructions to before the EH block, where they will never be
6991 for (MachineBasicBlock::reverse_iterator
6992 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
6993 if (!II->isCall()) continue;
6995 DenseMap<unsigned, bool> DefRegs;
6996 for (MachineInstr::mop_iterator
6997 OI = II->operands_begin(), OE = II->operands_end();
6999 if (!OI->isReg()) continue;
7000 DefRegs[OI->getReg()] = true;
7003 MachineInstrBuilder MIB(*MF, &*II);
7005 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
7006 unsigned Reg = SavedRegs[i];
7007 if (Subtarget->isThumb2() &&
7008 !ARM::tGPRRegClass.contains(Reg) &&
7009 !ARM::hGPRRegClass.contains(Reg))
7011 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
7013 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
7016 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
7023 // Mark all former landing pads as non-landing pads. The dispatch is the only
7025 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7026 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
7027 (*I)->setIsLandingPad(false);
7029 // The instruction is gone now.
7030 MI->eraseFromParent();
7036 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7037 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7038 E = MBB->succ_end(); I != E; ++I)
7041 llvm_unreachable("Expecting a BB with two successors!");
7044 MachineBasicBlock *ARMTargetLowering::
7045 EmitStructByval(MachineInstr *MI, MachineBasicBlock *BB) const {
7046 // This pseudo instruction has 3 operands: dst, src, size
7047 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7048 // Otherwise, we will generate unrolled scalar copies.
7049 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7050 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7051 MachineFunction::iterator It = BB;
7054 unsigned dest = MI->getOperand(0).getReg();
7055 unsigned src = MI->getOperand(1).getReg();
7056 unsigned SizeVal = MI->getOperand(2).getImm();
7057 unsigned Align = MI->getOperand(3).getImm();
7058 DebugLoc dl = MI->getDebugLoc();
7060 bool isThumb2 = Subtarget->isThumb2();
7061 MachineFunction *MF = BB->getParent();
7062 MachineRegisterInfo &MRI = MF->getRegInfo();
7063 unsigned ldrOpc, strOpc, UnitSize = 0;
7065 const TargetRegisterClass *TRC = isThumb2 ?
7066 (const TargetRegisterClass*)&ARM::tGPRRegClass :
7067 (const TargetRegisterClass*)&ARM::GPRRegClass;
7068 const TargetRegisterClass *TRC_Vec = 0;
7071 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
7072 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
7074 } else if (Align & 2) {
7075 ldrOpc = isThumb2 ? ARM::t2LDRH_POST : ARM::LDRH_POST;
7076 strOpc = isThumb2 ? ARM::t2STRH_POST : ARM::STRH_POST;
7079 // Check whether we can use NEON instructions.
7080 if (!MF->getFunction()->getAttributes().
7081 hasAttribute(AttributeSet::FunctionIndex,
7082 Attribute::NoImplicitFloat) &&
7083 Subtarget->hasNEON()) {
7084 if ((Align % 16 == 0) && SizeVal >= 16) {
7085 ldrOpc = ARM::VLD1q32wb_fixed;
7086 strOpc = ARM::VST1q32wb_fixed;
7088 TRC_Vec = (const TargetRegisterClass*)&ARM::DPairRegClass;
7090 else if ((Align % 8 == 0) && SizeVal >= 8) {
7091 ldrOpc = ARM::VLD1d32wb_fixed;
7092 strOpc = ARM::VST1d32wb_fixed;
7094 TRC_Vec = (const TargetRegisterClass*)&ARM::DPRRegClass;
7097 // Can't use NEON instructions.
7098 if (UnitSize == 0) {
7099 ldrOpc = isThumb2 ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
7100 strOpc = isThumb2 ? ARM::t2STR_POST : ARM::STR_POST_IMM;
7105 unsigned BytesLeft = SizeVal % UnitSize;
7106 unsigned LoopSize = SizeVal - BytesLeft;
7108 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7109 // Use LDR and STR to copy.
7110 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7111 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7112 unsigned srcIn = src;
7113 unsigned destIn = dest;
7114 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
7115 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
7116 unsigned srcOut = MRI.createVirtualRegister(TRC);
7117 unsigned destOut = MRI.createVirtualRegister(TRC);
7118 if (UnitSize >= 8) {
7119 AddDefaultPred(BuildMI(*BB, MI, dl,
7120 TII->get(ldrOpc), scratch)
7121 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(0));
7123 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
7124 .addReg(destIn).addImm(0).addReg(scratch));
7125 } else if (isThumb2) {
7126 AddDefaultPred(BuildMI(*BB, MI, dl,
7127 TII->get(ldrOpc), scratch)
7128 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(UnitSize));
7130 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
7131 .addReg(scratch).addReg(destIn)
7134 AddDefaultPred(BuildMI(*BB, MI, dl,
7135 TII->get(ldrOpc), scratch)
7136 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0)
7139 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
7140 .addReg(scratch).addReg(destIn)
7141 .addReg(0).addImm(UnitSize));
7147 // Handle the leftover bytes with LDRB and STRB.
7148 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7149 // [destOut] = STRB_POST(scratch, destIn, 1)
7150 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
7151 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
7152 for (unsigned i = 0; i < BytesLeft; i++) {
7153 unsigned scratch = MRI.createVirtualRegister(TRC);
7154 unsigned srcOut = MRI.createVirtualRegister(TRC);
7155 unsigned destOut = MRI.createVirtualRegister(TRC);
7157 AddDefaultPred(BuildMI(*BB, MI, dl,
7158 TII->get(ldrOpc),scratch)
7159 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
7161 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
7162 .addReg(scratch).addReg(destIn)
7163 .addReg(0).addImm(1));
7165 AddDefaultPred(BuildMI(*BB, MI, dl,
7166 TII->get(ldrOpc),scratch)
7167 .addReg(srcOut, RegState::Define).addReg(srcIn)
7168 .addReg(0).addImm(1));
7170 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(strOpc), destOut)
7171 .addReg(scratch).addReg(destIn)
7172 .addReg(0).addImm(1));
7177 MI->eraseFromParent(); // The instruction is gone now.
7181 // Expand the pseudo op to a loop.
7184 // movw varEnd, # --> with thumb2
7186 // ldrcp varEnd, idx --> without thumb2
7187 // fallthrough --> loopMBB
7189 // PHI varPhi, varEnd, varLoop
7190 // PHI srcPhi, src, srcLoop
7191 // PHI destPhi, dst, destLoop
7192 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7193 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7194 // subs varLoop, varPhi, #UnitSize
7196 // fallthrough --> exitMBB
7198 // epilogue to handle left-over bytes
7199 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7200 // [destOut] = STRB_POST(scratch, destLoop, 1)
7201 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7202 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7203 MF->insert(It, loopMBB);
7204 MF->insert(It, exitMBB);
7206 // Transfer the remainder of BB and its successor edges to exitMBB.
7207 exitMBB->splice(exitMBB->begin(), BB,
7208 llvm::next(MachineBasicBlock::iterator(MI)),
7210 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7212 // Load an immediate to varEnd.
7213 unsigned varEnd = MRI.createVirtualRegister(TRC);
7215 unsigned VReg1 = varEnd;
7216 if ((LoopSize & 0xFFFF0000) != 0)
7217 VReg1 = MRI.createVirtualRegister(TRC);
7218 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), VReg1)
7219 .addImm(LoopSize & 0xFFFF));
7221 if ((LoopSize & 0xFFFF0000) != 0)
7222 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7224 .addImm(LoopSize >> 16));
7226 MachineConstantPool *ConstantPool = MF->getConstantPool();
7227 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7228 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7230 // MachineConstantPool wants an explicit alignment.
7231 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7233 Align = getDataLayout()->getTypeAllocSize(C->getType());
7234 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7236 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::LDRcp))
7237 .addReg(varEnd, RegState::Define)
7238 .addConstantPoolIndex(Idx)
7241 BB->addSuccessor(loopMBB);
7243 // Generate the loop body:
7244 // varPhi = PHI(varLoop, varEnd)
7245 // srcPhi = PHI(srcLoop, src)
7246 // destPhi = PHI(destLoop, dst)
7247 MachineBasicBlock *entryBB = BB;
7249 unsigned varLoop = MRI.createVirtualRegister(TRC);
7250 unsigned varPhi = MRI.createVirtualRegister(TRC);
7251 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7252 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7253 unsigned destLoop = MRI.createVirtualRegister(TRC);
7254 unsigned destPhi = MRI.createVirtualRegister(TRC);
7256 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7257 .addReg(varLoop).addMBB(loopMBB)
7258 .addReg(varEnd).addMBB(entryBB);
7259 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7260 .addReg(srcLoop).addMBB(loopMBB)
7261 .addReg(src).addMBB(entryBB);
7262 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7263 .addReg(destLoop).addMBB(loopMBB)
7264 .addReg(dest).addMBB(entryBB);
7266 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7267 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
7268 unsigned scratch = MRI.createVirtualRegister(UnitSize >= 8 ? TRC_Vec:TRC);
7269 if (UnitSize >= 8) {
7270 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
7271 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(0));
7273 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
7274 .addReg(destPhi).addImm(0).addReg(scratch));
7275 } else if (isThumb2) {
7276 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
7277 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addImm(UnitSize));
7279 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
7280 .addReg(scratch).addReg(destPhi)
7283 AddDefaultPred(BuildMI(BB, dl, TII->get(ldrOpc), scratch)
7284 .addReg(srcLoop, RegState::Define).addReg(srcPhi).addReg(0)
7287 AddDefaultPred(BuildMI(BB, dl, TII->get(strOpc), destLoop)
7288 .addReg(scratch).addReg(destPhi)
7289 .addReg(0).addImm(UnitSize));
7292 // Decrement loop variable by UnitSize.
7293 MachineInstrBuilder MIB = BuildMI(BB, dl,
7294 TII->get(isThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7295 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7296 MIB->getOperand(5).setReg(ARM::CPSR);
7297 MIB->getOperand(5).setIsDef(true);
7299 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7300 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
7302 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7303 BB->addSuccessor(loopMBB);
7304 BB->addSuccessor(exitMBB);
7306 // Add epilogue to handle BytesLeft.
7308 MachineInstr *StartOfExit = exitMBB->begin();
7309 ldrOpc = isThumb2 ? ARM::t2LDRB_POST : ARM::LDRB_POST_IMM;
7310 strOpc = isThumb2 ? ARM::t2STRB_POST : ARM::STRB_POST_IMM;
7312 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7313 // [destOut] = STRB_POST(scratch, destLoop, 1)
7314 unsigned srcIn = srcLoop;
7315 unsigned destIn = destLoop;
7316 for (unsigned i = 0; i < BytesLeft; i++) {
7317 unsigned scratch = MRI.createVirtualRegister(TRC);
7318 unsigned srcOut = MRI.createVirtualRegister(TRC);
7319 unsigned destOut = MRI.createVirtualRegister(TRC);
7321 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
7322 TII->get(ldrOpc),scratch)
7323 .addReg(srcOut, RegState::Define).addReg(srcIn).addImm(1));
7325 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
7326 .addReg(scratch).addReg(destIn)
7329 AddDefaultPred(BuildMI(*BB, StartOfExit, dl,
7330 TII->get(ldrOpc),scratch)
7331 .addReg(srcOut, RegState::Define).addReg(srcIn).addReg(0).addImm(1));
7333 AddDefaultPred(BuildMI(*BB, StartOfExit, dl, TII->get(strOpc), destOut)
7334 .addReg(scratch).addReg(destIn)
7335 .addReg(0).addImm(1));
7341 MI->eraseFromParent(); // The instruction is gone now.
7346 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7347 MachineBasicBlock *BB) const {
7348 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7349 DebugLoc dl = MI->getDebugLoc();
7350 bool isThumb2 = Subtarget->isThumb2();
7351 switch (MI->getOpcode()) {
7354 llvm_unreachable("Unexpected instr type to insert");
7356 // The Thumb2 pre-indexed stores have the same MI operands, they just
7357 // define them differently in the .td files from the isel patterns, so
7358 // they need pseudos.
7359 case ARM::t2STR_preidx:
7360 MI->setDesc(TII->get(ARM::t2STR_PRE));
7362 case ARM::t2STRB_preidx:
7363 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7365 case ARM::t2STRH_preidx:
7366 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7369 case ARM::STRi_preidx:
7370 case ARM::STRBi_preidx: {
7371 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
7372 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7373 // Decode the offset.
7374 unsigned Offset = MI->getOperand(4).getImm();
7375 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7376 Offset = ARM_AM::getAM2Offset(Offset);
7380 MachineMemOperand *MMO = *MI->memoperands_begin();
7381 BuildMI(*BB, MI, dl, TII->get(NewOpc))
7382 .addOperand(MI->getOperand(0)) // Rn_wb
7383 .addOperand(MI->getOperand(1)) // Rt
7384 .addOperand(MI->getOperand(2)) // Rn
7385 .addImm(Offset) // offset (skip GPR==zero_reg)
7386 .addOperand(MI->getOperand(5)) // pred
7387 .addOperand(MI->getOperand(6))
7388 .addMemOperand(MMO);
7389 MI->eraseFromParent();
7392 case ARM::STRr_preidx:
7393 case ARM::STRBr_preidx:
7394 case ARM::STRH_preidx: {
7396 switch (MI->getOpcode()) {
7397 default: llvm_unreachable("unexpected opcode!");
7398 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7399 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7400 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7402 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7403 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7404 MIB.addOperand(MI->getOperand(i));
7405 MI->eraseFromParent();
7408 case ARM::ATOMIC_LOAD_ADD_I8:
7409 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7410 case ARM::ATOMIC_LOAD_ADD_I16:
7411 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7412 case ARM::ATOMIC_LOAD_ADD_I32:
7413 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr);
7415 case ARM::ATOMIC_LOAD_AND_I8:
7416 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7417 case ARM::ATOMIC_LOAD_AND_I16:
7418 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7419 case ARM::ATOMIC_LOAD_AND_I32:
7420 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7422 case ARM::ATOMIC_LOAD_OR_I8:
7423 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7424 case ARM::ATOMIC_LOAD_OR_I16:
7425 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7426 case ARM::ATOMIC_LOAD_OR_I32:
7427 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7429 case ARM::ATOMIC_LOAD_XOR_I8:
7430 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7431 case ARM::ATOMIC_LOAD_XOR_I16:
7432 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7433 case ARM::ATOMIC_LOAD_XOR_I32:
7434 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7436 case ARM::ATOMIC_LOAD_NAND_I8:
7437 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7438 case ARM::ATOMIC_LOAD_NAND_I16:
7439 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7440 case ARM::ATOMIC_LOAD_NAND_I32:
7441 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2BICrr : ARM::BICrr);
7443 case ARM::ATOMIC_LOAD_SUB_I8:
7444 return EmitAtomicBinary(MI, BB, 1, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7445 case ARM::ATOMIC_LOAD_SUB_I16:
7446 return EmitAtomicBinary(MI, BB, 2, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7447 case ARM::ATOMIC_LOAD_SUB_I32:
7448 return EmitAtomicBinary(MI, BB, 4, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr);
7450 case ARM::ATOMIC_LOAD_MIN_I8:
7451 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::LT);
7452 case ARM::ATOMIC_LOAD_MIN_I16:
7453 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::LT);
7454 case ARM::ATOMIC_LOAD_MIN_I32:
7455 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::LT);
7457 case ARM::ATOMIC_LOAD_MAX_I8:
7458 return EmitAtomicBinaryMinMax(MI, BB, 1, true, ARMCC::GT);
7459 case ARM::ATOMIC_LOAD_MAX_I16:
7460 return EmitAtomicBinaryMinMax(MI, BB, 2, true, ARMCC::GT);
7461 case ARM::ATOMIC_LOAD_MAX_I32:
7462 return EmitAtomicBinaryMinMax(MI, BB, 4, true, ARMCC::GT);
7464 case ARM::ATOMIC_LOAD_UMIN_I8:
7465 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::LO);
7466 case ARM::ATOMIC_LOAD_UMIN_I16:
7467 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::LO);
7468 case ARM::ATOMIC_LOAD_UMIN_I32:
7469 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::LO);
7471 case ARM::ATOMIC_LOAD_UMAX_I8:
7472 return EmitAtomicBinaryMinMax(MI, BB, 1, false, ARMCC::HI);
7473 case ARM::ATOMIC_LOAD_UMAX_I16:
7474 return EmitAtomicBinaryMinMax(MI, BB, 2, false, ARMCC::HI);
7475 case ARM::ATOMIC_LOAD_UMAX_I32:
7476 return EmitAtomicBinaryMinMax(MI, BB, 4, false, ARMCC::HI);
7478 case ARM::ATOMIC_SWAP_I8: return EmitAtomicBinary(MI, BB, 1, 0);
7479 case ARM::ATOMIC_SWAP_I16: return EmitAtomicBinary(MI, BB, 2, 0);
7480 case ARM::ATOMIC_SWAP_I32: return EmitAtomicBinary(MI, BB, 4, 0);
7482 case ARM::ATOMIC_CMP_SWAP_I8: return EmitAtomicCmpSwap(MI, BB, 1);
7483 case ARM::ATOMIC_CMP_SWAP_I16: return EmitAtomicCmpSwap(MI, BB, 2);
7484 case ARM::ATOMIC_CMP_SWAP_I32: return EmitAtomicCmpSwap(MI, BB, 4);
7487 case ARM::ATOMADD6432:
7488 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ADDrr : ARM::ADDrr,
7489 isThumb2 ? ARM::t2ADCrr : ARM::ADCrr,
7490 /*NeedsCarry*/ true);
7491 case ARM::ATOMSUB6432:
7492 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7493 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7494 /*NeedsCarry*/ true);
7495 case ARM::ATOMOR6432:
7496 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ORRrr : ARM::ORRrr,
7497 isThumb2 ? ARM::t2ORRrr : ARM::ORRrr);
7498 case ARM::ATOMXOR6432:
7499 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2EORrr : ARM::EORrr,
7500 isThumb2 ? ARM::t2EORrr : ARM::EORrr);
7501 case ARM::ATOMAND6432:
7502 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2ANDrr : ARM::ANDrr,
7503 isThumb2 ? ARM::t2ANDrr : ARM::ANDrr);
7504 case ARM::ATOMSWAP6432:
7505 return EmitAtomicBinary64(MI, BB, 0, 0, false);
7506 case ARM::ATOMCMPXCHG6432:
7507 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7508 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7509 /*NeedsCarry*/ false, /*IsCmpxchg*/true);
7510 case ARM::ATOMMIN6432:
7511 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7512 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7513 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7514 /*IsMinMax*/ true, ARMCC::LT);
7515 case ARM::ATOMMAX6432:
7516 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7517 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7518 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7519 /*IsMinMax*/ true, ARMCC::GE);
7520 case ARM::ATOMUMIN6432:
7521 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7522 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7523 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7524 /*IsMinMax*/ true, ARMCC::LO);
7525 case ARM::ATOMUMAX6432:
7526 return EmitAtomicBinary64(MI, BB, isThumb2 ? ARM::t2SUBrr : ARM::SUBrr,
7527 isThumb2 ? ARM::t2SBCrr : ARM::SBCrr,
7528 /*NeedsCarry*/ true, /*IsCmpxchg*/false,
7529 /*IsMinMax*/ true, ARMCC::HS);
7531 case ARM::tMOVCCr_pseudo: {
7532 // To "insert" a SELECT_CC instruction, we actually have to insert the
7533 // diamond control-flow pattern. The incoming instruction knows the
7534 // destination vreg to set, the condition code register to branch on, the
7535 // true/false values to select between, and a branch opcode to use.
7536 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7537 MachineFunction::iterator It = BB;
7543 // cmpTY ccX, r1, r2
7545 // fallthrough --> copy0MBB
7546 MachineBasicBlock *thisMBB = BB;
7547 MachineFunction *F = BB->getParent();
7548 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7549 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
7550 F->insert(It, copy0MBB);
7551 F->insert(It, sinkMBB);
7553 // Transfer the remainder of BB and its successor edges to sinkMBB.
7554 sinkMBB->splice(sinkMBB->begin(), BB,
7555 llvm::next(MachineBasicBlock::iterator(MI)),
7557 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7559 BB->addSuccessor(copy0MBB);
7560 BB->addSuccessor(sinkMBB);
7562 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7563 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7566 // %FalseValue = ...
7567 // # fallthrough to sinkMBB
7570 // Update machine-CFG edges
7571 BB->addSuccessor(sinkMBB);
7574 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7577 BuildMI(*BB, BB->begin(), dl,
7578 TII->get(ARM::PHI), MI->getOperand(0).getReg())
7579 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7580 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7582 MI->eraseFromParent(); // The pseudo instruction is gone now.
7587 case ARM::BCCZi64: {
7588 // If there is an unconditional branch to the other successor, remove it.
7589 BB->erase(llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
7591 // Compare both parts that make up the double comparison separately for
7593 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7595 unsigned LHS1 = MI->getOperand(1).getReg();
7596 unsigned LHS2 = MI->getOperand(2).getReg();
7598 AddDefaultPred(BuildMI(BB, dl,
7599 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7600 .addReg(LHS1).addImm(0));
7601 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7602 .addReg(LHS2).addImm(0)
7603 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7605 unsigned RHS1 = MI->getOperand(3).getReg();
7606 unsigned RHS2 = MI->getOperand(4).getReg();
7607 AddDefaultPred(BuildMI(BB, dl,
7608 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7609 .addReg(LHS1).addReg(RHS1));
7610 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7611 .addReg(LHS2).addReg(RHS2)
7612 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7615 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7616 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7617 if (MI->getOperand(0).getImm() == ARMCC::NE)
7618 std::swap(destMBB, exitMBB);
7620 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7621 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
7623 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7625 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
7627 MI->eraseFromParent(); // The pseudo instruction is gone now.
7631 case ARM::Int_eh_sjlj_setjmp:
7632 case ARM::Int_eh_sjlj_setjmp_nofp:
7633 case ARM::tInt_eh_sjlj_setjmp:
7634 case ARM::t2Int_eh_sjlj_setjmp:
7635 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7636 EmitSjLjDispatchBlock(MI, BB);
7641 // To insert an ABS instruction, we have to insert the
7642 // diamond control-flow pattern. The incoming instruction knows the
7643 // source vreg to test against 0, the destination vreg to set,
7644 // the condition code register to branch on, the
7645 // true/false values to select between, and a branch opcode to use.
7650 // BCC (branch to SinkBB if V0 >= 0)
7651 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
7652 // SinkBB: V1 = PHI(V2, V3)
7653 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7654 MachineFunction::iterator BBI = BB;
7656 MachineFunction *Fn = BB->getParent();
7657 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7658 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7659 Fn->insert(BBI, RSBBB);
7660 Fn->insert(BBI, SinkBB);
7662 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7663 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7664 bool isThumb2 = Subtarget->isThumb2();
7665 MachineRegisterInfo &MRI = Fn->getRegInfo();
7666 // In Thumb mode S must not be specified if source register is the SP or
7667 // PC and if destination register is the SP, so restrict register class
7668 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7669 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7670 (const TargetRegisterClass*)&ARM::GPRRegClass);
7672 // Transfer the remainder of BB and its successor edges to sinkMBB.
7673 SinkBB->splice(SinkBB->begin(), BB,
7674 llvm::next(MachineBasicBlock::iterator(MI)),
7676 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7678 BB->addSuccessor(RSBBB);
7679 BB->addSuccessor(SinkBB);
7681 // fall through to SinkMBB
7682 RSBBB->addSuccessor(SinkBB);
7684 // insert a cmp at the end of BB
7685 AddDefaultPred(BuildMI(BB, dl,
7686 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7687 .addReg(ABSSrcReg).addImm(0));
7689 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
7691 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7692 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7694 // insert rsbri in RSBBB
7695 // Note: BCC and rsbri will be converted into predicated rsbmi
7696 // by if-conversion pass
7697 BuildMI(*RSBBB, RSBBB->begin(), dl,
7698 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
7699 .addReg(ABSSrcReg, RegState::Kill)
7700 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7702 // insert PHI in SinkBB,
7703 // reuse ABSDstReg to not change uses of ABS instruction
7704 BuildMI(*SinkBB, SinkBB->begin(), dl,
7705 TII->get(ARM::PHI), ABSDstReg)
7706 .addReg(NewRsbDstReg).addMBB(RSBBB)
7707 .addReg(ABSSrcReg).addMBB(BB);
7709 // remove ABS instruction
7710 MI->eraseFromParent();
7712 // return last added BB
7715 case ARM::COPY_STRUCT_BYVAL_I32:
7717 return EmitStructByval(MI, BB);
7721 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7722 SDNode *Node) const {
7723 if (!MI->hasPostISelHook()) {
7724 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7725 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7729 const MCInstrDesc *MCID = &MI->getDesc();
7730 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7731 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7732 // operand is still set to noreg. If needed, set the optional operand's
7733 // register to CPSR, and remove the redundant implicit def.
7735 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
7737 // Rename pseudo opcodes.
7738 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7740 const ARMBaseInstrInfo *TII =
7741 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
7742 MCID = &TII->get(NewOpc);
7744 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7745 "converted opcode should be the same except for cc_out");
7749 // Add the optional cc_out operand
7750 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
7752 unsigned ccOutIdx = MCID->getNumOperands() - 1;
7754 // Any ARM instruction that sets the 's' bit should specify an optional
7755 // "cc_out" operand in the last operand position.
7756 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
7757 assert(!NewOpc && "Optional cc_out operand required");
7760 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7761 // since we already have an optional CPSR def.
7762 bool definesCPSR = false;
7763 bool deadCPSR = false;
7764 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
7766 const MachineOperand &MO = MI->getOperand(i);
7767 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7771 MI->RemoveOperand(i);
7776 assert(!NewOpc && "Optional cc_out operand required");
7779 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
7781 assert(!MI->getOperand(ccOutIdx).getReg() &&
7782 "expect uninitialized optional cc_out operand");
7786 // If this instruction was defined with an optional CPSR def and its dag node
7787 // had a live implicit CPSR def, then activate the optional CPSR def.
7788 MachineOperand &MO = MI->getOperand(ccOutIdx);
7789 MO.setReg(ARM::CPSR);
7793 //===----------------------------------------------------------------------===//
7794 // ARM Optimization Hooks
7795 //===----------------------------------------------------------------------===//
7797 // Helper function that checks if N is a null or all ones constant.
7798 static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7799 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7802 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7805 // Return true if N is conditionally 0 or all ones.
7806 // Detects these expressions where cc is an i1 value:
7808 // (select cc 0, y) [AllOnes=0]
7809 // (select cc y, 0) [AllOnes=0]
7810 // (zext cc) [AllOnes=0]
7811 // (sext cc) [AllOnes=0/1]
7812 // (select cc -1, y) [AllOnes=1]
7813 // (select cc y, -1) [AllOnes=1]
7815 // Invert is set when N is the null/all ones constant when CC is false.
7816 // OtherOp is set to the alternative value of N.
7817 static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7818 SDValue &CC, bool &Invert,
7820 SelectionDAG &DAG) {
7821 switch (N->getOpcode()) {
7822 default: return false;
7824 CC = N->getOperand(0);
7825 SDValue N1 = N->getOperand(1);
7826 SDValue N2 = N->getOperand(2);
7827 if (isZeroOrAllOnes(N1, AllOnes)) {
7832 if (isZeroOrAllOnes(N2, AllOnes)) {
7839 case ISD::ZERO_EXTEND:
7840 // (zext cc) can never be the all ones value.
7844 case ISD::SIGN_EXTEND: {
7845 EVT VT = N->getValueType(0);
7846 CC = N->getOperand(0);
7847 if (CC.getValueType() != MVT::i1)
7851 // When looking for an AllOnes constant, N is an sext, and the 'other'
7853 OtherOp = DAG.getConstant(0, VT);
7854 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7855 // When looking for a 0 constant, N can be zext or sext.
7856 OtherOp = DAG.getConstant(1, VT);
7858 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7864 // Combine a constant select operand into its use:
7866 // (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7867 // (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7868 // (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7869 // (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7870 // (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
7872 // The transform is rejected if the select doesn't have a constant operand that
7873 // is null, or all ones when AllOnes is set.
7875 // Also recognize sext/zext from i1:
7877 // (add (zext cc), x) -> (select cc (add x, 1), x)
7878 // (add (sext cc), x) -> (select cc (add x, -1), x)
7880 // These transformations eventually create predicated instructions.
7882 // @param N The node to transform.
7883 // @param Slct The N operand that is a select.
7884 // @param OtherOp The other N operand (x above).
7885 // @param DCI Context.
7886 // @param AllOnes Require the select constant to be all ones instead of null.
7887 // @returns The new node, or SDValue() on failure.
7889 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
7890 TargetLowering::DAGCombinerInfo &DCI,
7891 bool AllOnes = false) {
7892 SelectionDAG &DAG = DCI.DAG;
7893 EVT VT = N->getValueType(0);
7894 SDValue NonConstantVal;
7897 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7898 NonConstantVal, DAG))
7901 // Slct is now know to be the desired identity constant when CC is true.
7902 SDValue TrueVal = OtherOp;
7903 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
7904 OtherOp, NonConstantVal);
7905 // Unless SwapSelectOps says CC should be false.
7907 std::swap(TrueVal, FalseVal);
7909 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
7910 CCOp, TrueVal, FalseVal);
7913 // Attempt combineSelectAndUse on each operand of a commutative operator N.
7915 SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7916 TargetLowering::DAGCombinerInfo &DCI) {
7917 SDValue N0 = N->getOperand(0);
7918 SDValue N1 = N->getOperand(1);
7919 if (N0.getNode()->hasOneUse()) {
7920 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7921 if (Result.getNode())
7924 if (N1.getNode()->hasOneUse()) {
7925 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7926 if (Result.getNode())
7932 // AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
7933 // (only after legalization).
7934 static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7935 TargetLowering::DAGCombinerInfo &DCI,
7936 const ARMSubtarget *Subtarget) {
7938 // Only perform optimization if after legalize, and if NEON is available. We
7939 // also expected both operands to be BUILD_VECTORs.
7940 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7941 || N0.getOpcode() != ISD::BUILD_VECTOR
7942 || N1.getOpcode() != ISD::BUILD_VECTOR)
7945 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7946 EVT VT = N->getValueType(0);
7947 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7950 // Check that the vector operands are of the right form.
7951 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7952 // operands, where N is the size of the formed vector.
7953 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7954 // index such that we have a pair wise add pattern.
7956 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
7957 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
7959 SDValue Vec = N0->getOperand(0)->getOperand(0);
7960 SDNode *V = Vec.getNode();
7961 unsigned nextIndex = 0;
7963 // For each operands to the ADD which are BUILD_VECTORs,
7964 // check to see if each of their operands are an EXTRACT_VECTOR with
7965 // the same vector and appropriate index.
7966 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7967 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7968 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
7970 SDValue ExtVec0 = N0->getOperand(i);
7971 SDValue ExtVec1 = N1->getOperand(i);
7973 // First operand is the vector, verify its the same.
7974 if (V != ExtVec0->getOperand(0).getNode() ||
7975 V != ExtVec1->getOperand(0).getNode())
7978 // Second is the constant, verify its correct.
7979 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7980 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
7982 // For the constant, we want to see all the even or all the odd.
7983 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7984 || C1->getZExtValue() != nextIndex+1)
7993 // Create VPADDL node.
7994 SelectionDAG &DAG = DCI.DAG;
7995 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7997 // Build operand list.
7998 SmallVector<SDValue, 8> Ops;
7999 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
8000 TLI.getPointerTy()));
8002 // Input is the vector.
8005 // Get widened type and narrowed type.
8007 unsigned numElem = VT.getVectorNumElements();
8008 switch (VT.getVectorElementType().getSimpleVT().SimpleTy) {
8009 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8010 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8011 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8013 llvm_unreachable("Invalid vector element type for padd optimization.");
8016 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
8017 widenType, &Ops[0], Ops.size());
8018 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, tmp);
8021 static SDValue findMUL_LOHI(SDValue V) {
8022 if (V->getOpcode() == ISD::UMUL_LOHI ||
8023 V->getOpcode() == ISD::SMUL_LOHI)
8028 static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8029 TargetLowering::DAGCombinerInfo &DCI,
8030 const ARMSubtarget *Subtarget) {
8032 if (Subtarget->isThumb1Only()) return SDValue();
8034 // Only perform the checks after legalize when the pattern is available.
8035 if (DCI.isBeforeLegalize()) return SDValue();
8037 // Look for multiply add opportunities.
8038 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8039 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8040 // a glue link from the first add to the second add.
8041 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8042 // a S/UMLAL instruction.
8045 // \ / \ [no multiline comment]
8051 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8052 SDValue AddcOp0 = AddcNode->getOperand(0);
8053 SDValue AddcOp1 = AddcNode->getOperand(1);
8055 // Check if the two operands are from the same mul_lohi node.
8056 if (AddcOp0.getNode() == AddcOp1.getNode())
8059 assert(AddcNode->getNumValues() == 2 &&
8060 AddcNode->getValueType(0) == MVT::i32 &&
8061 "Expect ADDC with two result values. First: i32");
8063 // Check that we have a glued ADDC node.
8064 if (AddcNode->getValueType(1) != MVT::Glue)
8067 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8068 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8069 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8070 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8071 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8074 // Look for the glued ADDE.
8075 SDNode* AddeNode = AddcNode->getGluedUser();
8076 if (AddeNode == NULL)
8079 // Make sure it is really an ADDE.
8080 if (AddeNode->getOpcode() != ISD::ADDE)
8083 assert(AddeNode->getNumOperands() == 3 &&
8084 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8085 "ADDE node has the wrong inputs");
8087 // Check for the triangle shape.
8088 SDValue AddeOp0 = AddeNode->getOperand(0);
8089 SDValue AddeOp1 = AddeNode->getOperand(1);
8091 // Make sure that the ADDE operands are not coming from the same node.
8092 if (AddeOp0.getNode() == AddeOp1.getNode())
8095 // Find the MUL_LOHI node walking up ADDE's operands.
8096 bool IsLeftOperandMUL = false;
8097 SDValue MULOp = findMUL_LOHI(AddeOp0);
8098 if (MULOp == SDValue())
8099 MULOp = findMUL_LOHI(AddeOp1);
8101 IsLeftOperandMUL = true;
8102 if (MULOp == SDValue())
8105 // Figure out the right opcode.
8106 unsigned Opc = MULOp->getOpcode();
8107 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8109 // Figure out the high and low input values to the MLAL node.
8110 SDValue* HiMul = &MULOp;
8111 SDValue* HiAdd = NULL;
8112 SDValue* LoMul = NULL;
8113 SDValue* LowAdd = NULL;
8115 if (IsLeftOperandMUL)
8121 if (AddcOp0->getOpcode() == Opc) {
8125 if (AddcOp1->getOpcode() == Opc) {
8133 if (LoMul->getNode() != HiMul->getNode())
8136 // Create the merged node.
8137 SelectionDAG &DAG = DCI.DAG;
8139 // Build operand list.
8140 SmallVector<SDValue, 8> Ops;
8141 Ops.push_back(LoMul->getOperand(0));
8142 Ops.push_back(LoMul->getOperand(1));
8143 Ops.push_back(*LowAdd);
8144 Ops.push_back(*HiAdd);
8146 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
8147 DAG.getVTList(MVT::i32, MVT::i32),
8148 &Ops[0], Ops.size());
8150 // Replace the ADDs' nodes uses by the MLA node's values.
8151 SDValue HiMLALResult(MLALNode.getNode(), 1);
8152 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8154 SDValue LoMLALResult(MLALNode.getNode(), 0);
8155 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8157 // Return original node to notify the driver to stop replacing.
8158 SDValue resNode(AddcNode, 0);
8162 /// PerformADDCCombine - Target-specific dag combine transform from
8163 /// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8164 static SDValue PerformADDCCombine(SDNode *N,
8165 TargetLowering::DAGCombinerInfo &DCI,
8166 const ARMSubtarget *Subtarget) {
8168 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8172 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8173 /// operands N0 and N1. This is a helper for PerformADDCombine that is
8174 /// called with the default operands, and if that fails, with commuted
8176 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
8177 TargetLowering::DAGCombinerInfo &DCI,
8178 const ARMSubtarget *Subtarget){
8180 // Attempt to create vpaddl for this add.
8181 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8182 if (Result.getNode())
8185 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8186 if (N0.getNode()->hasOneUse()) {
8187 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8188 if (Result.getNode()) return Result;
8193 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8195 static SDValue PerformADDCombine(SDNode *N,
8196 TargetLowering::DAGCombinerInfo &DCI,
8197 const ARMSubtarget *Subtarget) {
8198 SDValue N0 = N->getOperand(0);
8199 SDValue N1 = N->getOperand(1);
8201 // First try with the default operand order.
8202 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
8203 if (Result.getNode())
8206 // If that didn't work, try again with the operands commuted.
8207 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
8210 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
8212 static SDValue PerformSUBCombine(SDNode *N,
8213 TargetLowering::DAGCombinerInfo &DCI) {
8214 SDValue N0 = N->getOperand(0);
8215 SDValue N1 = N->getOperand(1);
8217 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8218 if (N1.getNode()->hasOneUse()) {
8219 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8220 if (Result.getNode()) return Result;
8226 /// PerformVMULCombine
8227 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8228 /// special multiplier accumulator forwarding.
8234 static SDValue PerformVMULCombine(SDNode *N,
8235 TargetLowering::DAGCombinerInfo &DCI,
8236 const ARMSubtarget *Subtarget) {
8237 if (!Subtarget->hasVMLxForwarding())
8240 SelectionDAG &DAG = DCI.DAG;
8241 SDValue N0 = N->getOperand(0);
8242 SDValue N1 = N->getOperand(1);
8243 unsigned Opcode = N0.getOpcode();
8244 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8245 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
8246 Opcode = N1.getOpcode();
8247 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8248 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8253 EVT VT = N->getValueType(0);
8255 SDValue N00 = N0->getOperand(0);
8256 SDValue N01 = N0->getOperand(1);
8257 return DAG.getNode(Opcode, DL, VT,
8258 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8259 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8262 static SDValue PerformMULCombine(SDNode *N,
8263 TargetLowering::DAGCombinerInfo &DCI,
8264 const ARMSubtarget *Subtarget) {
8265 SelectionDAG &DAG = DCI.DAG;
8267 if (Subtarget->isThumb1Only())
8270 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8273 EVT VT = N->getValueType(0);
8274 if (VT.is64BitVector() || VT.is128BitVector())
8275 return PerformVMULCombine(N, DCI, Subtarget);
8279 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8283 int64_t MulAmt = C->getSExtValue();
8284 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
8286 ShiftAmt = ShiftAmt & (32 - 1);
8287 SDValue V = N->getOperand(0);
8291 MulAmt >>= ShiftAmt;
8294 if (isPowerOf2_32(MulAmt - 1)) {
8295 // (mul x, 2^N + 1) => (add (shl x, N), x)
8296 Res = DAG.getNode(ISD::ADD, DL, VT,
8298 DAG.getNode(ISD::SHL, DL, VT,
8300 DAG.getConstant(Log2_32(MulAmt - 1),
8302 } else if (isPowerOf2_32(MulAmt + 1)) {
8303 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8304 Res = DAG.getNode(ISD::SUB, DL, VT,
8305 DAG.getNode(ISD::SHL, DL, VT,
8307 DAG.getConstant(Log2_32(MulAmt + 1),
8313 uint64_t MulAmtAbs = -MulAmt;
8314 if (isPowerOf2_32(MulAmtAbs + 1)) {
8315 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8316 Res = DAG.getNode(ISD::SUB, DL, VT,
8318 DAG.getNode(ISD::SHL, DL, VT,
8320 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8322 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8323 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8324 Res = DAG.getNode(ISD::ADD, DL, VT,
8326 DAG.getNode(ISD::SHL, DL, VT,
8328 DAG.getConstant(Log2_32(MulAmtAbs-1),
8330 Res = DAG.getNode(ISD::SUB, DL, VT,
8331 DAG.getConstant(0, MVT::i32),Res);
8338 Res = DAG.getNode(ISD::SHL, DL, VT,
8339 Res, DAG.getConstant(ShiftAmt, MVT::i32));
8341 // Do not add new nodes to DAG combiner worklist.
8342 DCI.CombineTo(N, Res, false);
8346 static SDValue PerformANDCombine(SDNode *N,
8347 TargetLowering::DAGCombinerInfo &DCI,
8348 const ARMSubtarget *Subtarget) {
8350 // Attempt to use immediate-form VBIC
8351 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
8353 EVT VT = N->getValueType(0);
8354 SelectionDAG &DAG = DCI.DAG;
8356 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8359 APInt SplatBits, SplatUndef;
8360 unsigned SplatBitSize;
8363 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8364 if (SplatBitSize <= 64) {
8366 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8367 SplatUndef.getZExtValue(), SplatBitSize,
8368 DAG, VbicVT, VT.is128BitVector(),
8370 if (Val.getNode()) {
8372 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
8373 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
8374 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
8379 if (!Subtarget->isThumb1Only()) {
8380 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8381 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8382 if (Result.getNode())
8389 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8390 static SDValue PerformORCombine(SDNode *N,
8391 TargetLowering::DAGCombinerInfo &DCI,
8392 const ARMSubtarget *Subtarget) {
8393 // Attempt to use immediate-form VORR
8394 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
8396 EVT VT = N->getValueType(0);
8397 SelectionDAG &DAG = DCI.DAG;
8399 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8402 APInt SplatBits, SplatUndef;
8403 unsigned SplatBitSize;
8405 if (BVN && Subtarget->hasNEON() &&
8406 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8407 if (SplatBitSize <= 64) {
8409 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8410 SplatUndef.getZExtValue(), SplatBitSize,
8411 DAG, VorrVT, VT.is128BitVector(),
8413 if (Val.getNode()) {
8415 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
8416 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
8417 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
8422 if (!Subtarget->isThumb1Only()) {
8423 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8424 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8425 if (Result.getNode())
8429 // The code below optimizes (or (and X, Y), Z).
8430 // The AND operand needs to have a single user to make these optimizations
8432 SDValue N0 = N->getOperand(0);
8433 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
8435 SDValue N1 = N->getOperand(1);
8437 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8438 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8439 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8441 unsigned SplatBitSize;
8444 APInt SplatBits0, SplatBits1;
8445 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
8446 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8447 // Ensure that the second operand of both ands are constants
8448 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
8449 HasAnyUndefs) && !HasAnyUndefs) {
8450 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8451 HasAnyUndefs) && !HasAnyUndefs) {
8452 // Ensure that the bit width of the constants are the same and that
8453 // the splat arguments are logical inverses as per the pattern we
8454 // are trying to simplify.
8455 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8456 SplatBits0 == ~SplatBits1) {
8457 // Canonicalize the vector type to make instruction selection
8459 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8460 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8464 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8470 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8473 // BFI is only available on V6T2+
8474 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8478 // 1) or (and A, mask), val => ARMbfi A, val, mask
8479 // iff (val & mask) == val
8481 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
8482 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
8483 // && mask == ~mask2
8484 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
8485 // && ~mask == mask2
8486 // (i.e., copy a bitfield value into another bitfield of the same width)
8491 SDValue N00 = N0.getOperand(0);
8493 // The value and the mask need to be constants so we can verify this is
8494 // actually a bitfield set. If the mask is 0xffff, we can do better
8495 // via a movt instruction, so don't use BFI in that case.
8496 SDValue MaskOp = N0.getOperand(1);
8497 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8500 unsigned Mask = MaskC->getZExtValue();
8504 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
8505 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8507 unsigned Val = N1C->getZExtValue();
8508 if ((Val & ~Mask) != Val)
8511 if (ARM::isBitFieldInvertedMask(Mask)) {
8512 Val >>= countTrailingZeros(~Mask);
8514 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
8515 DAG.getConstant(Val, MVT::i32),
8516 DAG.getConstant(Mask, MVT::i32));
8518 // Do not add new nodes to DAG combiner worklist.
8519 DCI.CombineTo(N, Res, false);
8522 } else if (N1.getOpcode() == ISD::AND) {
8523 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
8524 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8527 unsigned Mask2 = N11C->getZExtValue();
8529 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8531 if (ARM::isBitFieldInvertedMask(Mask) &&
8533 // The pack halfword instruction works better for masks that fit it,
8534 // so use that when it's available.
8535 if (Subtarget->hasT2ExtractPack() &&
8536 (Mask == 0xffff || Mask == 0xffff0000))
8539 unsigned amt = countTrailingZeros(Mask2);
8540 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
8541 DAG.getConstant(amt, MVT::i32));
8542 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
8543 DAG.getConstant(Mask, MVT::i32));
8544 // Do not add new nodes to DAG combiner worklist.
8545 DCI.CombineTo(N, Res, false);
8547 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
8549 // The pack halfword instruction works better for masks that fit it,
8550 // so use that when it's available.
8551 if (Subtarget->hasT2ExtractPack() &&
8552 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8555 unsigned lsb = countTrailingZeros(Mask);
8556 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
8557 DAG.getConstant(lsb, MVT::i32));
8558 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
8559 DAG.getConstant(Mask2, MVT::i32));
8560 // Do not add new nodes to DAG combiner worklist.
8561 DCI.CombineTo(N, Res, false);
8566 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8567 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8568 ARM::isBitFieldInvertedMask(~Mask)) {
8569 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8570 // where lsb(mask) == #shamt and masked bits of B are known zero.
8571 SDValue ShAmt = N00.getOperand(1);
8572 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
8573 unsigned LSB = countTrailingZeros(Mask);
8577 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8578 DAG.getConstant(~Mask, MVT::i32));
8580 // Do not add new nodes to DAG combiner worklist.
8581 DCI.CombineTo(N, Res, false);
8587 static SDValue PerformXORCombine(SDNode *N,
8588 TargetLowering::DAGCombinerInfo &DCI,
8589 const ARMSubtarget *Subtarget) {
8590 EVT VT = N->getValueType(0);
8591 SelectionDAG &DAG = DCI.DAG;
8593 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8596 if (!Subtarget->isThumb1Only()) {
8597 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8598 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8599 if (Result.getNode())
8606 /// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8607 /// the bits being cleared by the AND are not demanded by the BFI.
8608 static SDValue PerformBFICombine(SDNode *N,
8609 TargetLowering::DAGCombinerInfo &DCI) {
8610 SDValue N1 = N->getOperand(1);
8611 if (N1.getOpcode() == ISD::AND) {
8612 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8615 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
8616 unsigned LSB = countTrailingZeros(~InvMask);
8617 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
8618 unsigned Mask = (1 << Width)-1;
8619 unsigned Mask2 = N11C->getZExtValue();
8620 if ((Mask & (~Mask2)) == 0)
8621 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
8622 N->getOperand(0), N1.getOperand(0),
8628 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8629 /// ARMISD::VMOVRRD.
8630 static SDValue PerformVMOVRRDCombine(SDNode *N,
8631 TargetLowering::DAGCombinerInfo &DCI) {
8632 // vmovrrd(vmovdrr x, y) -> x,y
8633 SDValue InDouble = N->getOperand(0);
8634 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8635 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
8637 // vmovrrd(load f64) -> (load i32), (load i32)
8638 SDNode *InNode = InDouble.getNode();
8639 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8640 InNode->getValueType(0) == MVT::f64 &&
8641 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8642 !cast<LoadSDNode>(InNode)->isVolatile()) {
8643 // TODO: Should this be done for non-FrameIndex operands?
8644 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8646 SelectionDAG &DAG = DCI.DAG;
8648 SDValue BasePtr = LD->getBasePtr();
8649 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8650 LD->getPointerInfo(), LD->isVolatile(),
8651 LD->isNonTemporal(), LD->isInvariant(),
8652 LD->getAlignment());
8654 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8655 DAG.getConstant(4, MVT::i32));
8656 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8657 LD->getPointerInfo(), LD->isVolatile(),
8658 LD->isNonTemporal(), LD->isInvariant(),
8659 std::min(4U, LD->getAlignment() / 2));
8661 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8662 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8663 DCI.RemoveFromWorklist(LD);
8671 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8672 /// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8673 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8674 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8675 SDValue Op0 = N->getOperand(0);
8676 SDValue Op1 = N->getOperand(1);
8677 if (Op0.getOpcode() == ISD::BITCAST)
8678 Op0 = Op0.getOperand(0);
8679 if (Op1.getOpcode() == ISD::BITCAST)
8680 Op1 = Op1.getOperand(0);
8681 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8682 Op0.getNode() == Op1.getNode() &&
8683 Op0.getResNo() == 0 && Op1.getResNo() == 1)
8684 return DAG.getNode(ISD::BITCAST, SDLoc(N),
8685 N->getValueType(0), Op0.getOperand(0));
8689 /// PerformSTORECombine - Target-specific dag combine xforms for
8691 static SDValue PerformSTORECombine(SDNode *N,
8692 TargetLowering::DAGCombinerInfo &DCI) {
8693 StoreSDNode *St = cast<StoreSDNode>(N);
8694 if (St->isVolatile())
8697 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
8698 // pack all of the elements in one place. Next, store to memory in fewer
8700 SDValue StVal = St->getValue();
8701 EVT VT = StVal.getValueType();
8702 if (St->isTruncatingStore() && VT.isVector()) {
8703 SelectionDAG &DAG = DCI.DAG;
8704 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8705 EVT StVT = St->getMemoryVT();
8706 unsigned NumElems = VT.getVectorNumElements();
8707 assert(StVT != VT && "Cannot truncate to the same type");
8708 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8709 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8711 // From, To sizes and ElemCount must be pow of two
8712 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8714 // We are going to use the original vector elt for storing.
8715 // Accumulated smaller vector elements must be a multiple of the store size.
8716 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8718 unsigned SizeRatio = FromEltSz / ToEltSz;
8719 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8721 // Create a type on which we perform the shuffle.
8722 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8723 NumElems*SizeRatio);
8724 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8727 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8728 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8729 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8731 // Can't shuffle using an illegal type.
8732 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8734 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8735 DAG.getUNDEF(WideVec.getValueType()),
8737 // At this point all of the data is stored at the bottom of the
8738 // register. We now need to save it to mem.
8740 // Find the largest store unit
8741 MVT StoreType = MVT::i8;
8742 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8743 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8744 MVT Tp = (MVT::SimpleValueType)tp;
8745 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8748 // Didn't find a legal store type.
8749 if (!TLI.isTypeLegal(StoreType))
8752 // Bitcast the original vector into a vector of store-size units
8753 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8754 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8755 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8756 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8757 SmallVector<SDValue, 8> Chains;
8758 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8759 TLI.getPointerTy());
8760 SDValue BasePtr = St->getBasePtr();
8762 // Perform one or more big stores into memory.
8763 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8764 for (unsigned I = 0; I < E; I++) {
8765 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8766 StoreType, ShuffWide,
8767 DAG.getIntPtrConstant(I));
8768 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8769 St->getPointerInfo(), St->isVolatile(),
8770 St->isNonTemporal(), St->getAlignment());
8771 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8773 Chains.push_back(Ch);
8775 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, &Chains[0],
8779 if (!ISD::isNormalStore(St))
8782 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8783 // ARM stores of arguments in the same cache line.
8784 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
8785 StVal.getNode()->hasOneUse()) {
8786 SelectionDAG &DAG = DCI.DAG;
8788 SDValue BasePtr = St->getBasePtr();
8789 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
8790 StVal.getNode()->getOperand(0), BasePtr,
8791 St->getPointerInfo(), St->isVolatile(),
8792 St->isNonTemporal(), St->getAlignment());
8794 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8795 DAG.getConstant(4, MVT::i32));
8796 return DAG.getStore(NewST1.getValue(0), DL, StVal.getNode()->getOperand(1),
8797 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8798 St->isNonTemporal(),
8799 std::min(4U, St->getAlignment() / 2));
8802 if (StVal.getValueType() != MVT::i64 ||
8803 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8806 // Bitcast an i64 store extracted from a vector to f64.
8807 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8808 SelectionDAG &DAG = DCI.DAG;
8810 SDValue IntVec = StVal.getOperand(0);
8811 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8812 IntVec.getValueType().getVectorNumElements());
8813 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8814 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8815 Vec, StVal.getOperand(1));
8817 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8818 // Make the DAGCombiner fold the bitcasts.
8819 DCI.AddToWorklist(Vec.getNode());
8820 DCI.AddToWorklist(ExtElt.getNode());
8821 DCI.AddToWorklist(V.getNode());
8822 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8823 St->getPointerInfo(), St->isVolatile(),
8824 St->isNonTemporal(), St->getAlignment(),
8828 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8829 /// are normal, non-volatile loads. If so, it is profitable to bitcast an
8830 /// i64 vector to have f64 elements, since the value can then be loaded
8831 /// directly into a VFP register.
8832 static bool hasNormalLoadOperand(SDNode *N) {
8833 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8834 for (unsigned i = 0; i < NumElts; ++i) {
8835 SDNode *Elt = N->getOperand(i).getNode();
8836 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8842 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8843 /// ISD::BUILD_VECTOR.
8844 static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8845 TargetLowering::DAGCombinerInfo &DCI){
8846 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8847 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8848 // into a pair of GPRs, which is fine when the value is used as a scalar,
8849 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
8850 SelectionDAG &DAG = DCI.DAG;
8851 if (N->getNumOperands() == 2) {
8852 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8857 // Load i64 elements as f64 values so that type legalization does not split
8858 // them up into i32 values.
8859 EVT VT = N->getValueType(0);
8860 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8863 SmallVector<SDValue, 8> Ops;
8864 unsigned NumElts = VT.getVectorNumElements();
8865 for (unsigned i = 0; i < NumElts; ++i) {
8866 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8868 // Make the DAGCombiner fold the bitcast.
8869 DCI.AddToWorklist(V.getNode());
8871 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
8872 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops.data(), NumElts);
8873 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8876 /// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8878 PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8879 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8880 // At that time, we may have inserted bitcasts from integer to float.
8881 // If these bitcasts have survived DAGCombine, change the lowering of this
8882 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8883 // force to use floating point types.
8885 // Make sure we can change the type of the vector.
8886 // This is possible iff:
8887 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8888 // 1.1. Vector is used only once.
8889 // 1.2. Use is a bit convert to an integer type.
8890 // 2. The size of its operands are 32-bits (64-bits are not legal).
8891 EVT VT = N->getValueType(0);
8892 EVT EltVT = VT.getVectorElementType();
8894 // Check 1.1. and 2.
8895 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8898 // By construction, the input type must be float.
8899 assert(EltVT == MVT::f32 && "Unexpected type!");
8902 SDNode *Use = *N->use_begin();
8903 if (Use->getOpcode() != ISD::BITCAST ||
8904 Use->getValueType(0).isFloatingPoint())
8907 // Check profitability.
8908 // Model is, if more than half of the relevant operands are bitcast from
8909 // i32, turn the build_vector into a sequence of insert_vector_elt.
8910 // Relevant operands are everything that is not statically
8911 // (i.e., at compile time) bitcasted.
8912 unsigned NumOfBitCastedElts = 0;
8913 unsigned NumElts = VT.getVectorNumElements();
8914 unsigned NumOfRelevantElts = NumElts;
8915 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8916 SDValue Elt = N->getOperand(Idx);
8917 if (Elt->getOpcode() == ISD::BITCAST) {
8918 // Assume only bit cast to i32 will go away.
8919 if (Elt->getOperand(0).getValueType() == MVT::i32)
8920 ++NumOfBitCastedElts;
8921 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8922 // Constants are statically casted, thus do not count them as
8923 // relevant operands.
8924 --NumOfRelevantElts;
8927 // Check if more than half of the elements require a non-free bitcast.
8928 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8931 SelectionDAG &DAG = DCI.DAG;
8932 // Create the new vector type.
8933 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8934 // Check if the type is legal.
8935 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8936 if (!TLI.isTypeLegal(VecVT))
8940 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8941 // => BITCAST INSERT_VECTOR_ELT
8942 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8944 SDValue Vec = DAG.getUNDEF(VecVT);
8946 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8947 SDValue V = N->getOperand(Idx);
8948 if (V.getOpcode() == ISD::UNDEF)
8950 if (V.getOpcode() == ISD::BITCAST &&
8951 V->getOperand(0).getValueType() == MVT::i32)
8952 // Fold obvious case.
8953 V = V.getOperand(0);
8955 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
8956 // Make the DAGCombiner fold the bitcasts.
8957 DCI.AddToWorklist(V.getNode());
8959 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8960 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8962 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8963 // Make the DAGCombiner fold the bitcasts.
8964 DCI.AddToWorklist(Vec.getNode());
8968 /// PerformInsertEltCombine - Target-specific dag combine xforms for
8969 /// ISD::INSERT_VECTOR_ELT.
8970 static SDValue PerformInsertEltCombine(SDNode *N,
8971 TargetLowering::DAGCombinerInfo &DCI) {
8972 // Bitcast an i64 load inserted into a vector to f64.
8973 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8974 EVT VT = N->getValueType(0);
8975 SDNode *Elt = N->getOperand(1).getNode();
8976 if (VT.getVectorElementType() != MVT::i64 ||
8977 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8980 SelectionDAG &DAG = DCI.DAG;
8982 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8983 VT.getVectorNumElements());
8984 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8985 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8986 // Make the DAGCombiner fold the bitcasts.
8987 DCI.AddToWorklist(Vec.getNode());
8988 DCI.AddToWorklist(V.getNode());
8989 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8990 Vec, V, N->getOperand(2));
8991 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
8994 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8995 /// ISD::VECTOR_SHUFFLE.
8996 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8997 // The LLVM shufflevector instruction does not require the shuffle mask
8998 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8999 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9000 // operands do not match the mask length, they are extended by concatenating
9001 // them with undef vectors. That is probably the right thing for other
9002 // targets, but for NEON it is better to concatenate two double-register
9003 // size vector operands into a single quad-register size vector. Do that
9004 // transformation here:
9005 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9006 // shuffle(concat(v1, v2), undef)
9007 SDValue Op0 = N->getOperand(0);
9008 SDValue Op1 = N->getOperand(1);
9009 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9010 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9011 Op0.getNumOperands() != 2 ||
9012 Op1.getNumOperands() != 2)
9014 SDValue Concat0Op1 = Op0.getOperand(1);
9015 SDValue Concat1Op1 = Op1.getOperand(1);
9016 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
9017 Concat1Op1.getOpcode() != ISD::UNDEF)
9019 // Skip the transformation if any of the types are illegal.
9020 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9021 EVT VT = N->getValueType(0);
9022 if (!TLI.isTypeLegal(VT) ||
9023 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9024 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9027 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
9028 Op0.getOperand(0), Op1.getOperand(0));
9029 // Translate the shuffle mask.
9030 SmallVector<int, 16> NewMask;
9031 unsigned NumElts = VT.getVectorNumElements();
9032 unsigned HalfElts = NumElts/2;
9033 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9034 for (unsigned n = 0; n < NumElts; ++n) {
9035 int MaskElt = SVN->getMaskElt(n);
9037 if (MaskElt < (int)HalfElts)
9039 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
9040 NewElt = HalfElts + MaskElt - NumElts;
9041 NewMask.push_back(NewElt);
9043 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
9044 DAG.getUNDEF(VT), NewMask.data());
9047 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
9048 /// NEON load/store intrinsics to merge base address updates.
9049 static SDValue CombineBaseUpdate(SDNode *N,
9050 TargetLowering::DAGCombinerInfo &DCI) {
9051 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9054 SelectionDAG &DAG = DCI.DAG;
9055 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9056 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
9057 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
9058 SDValue Addr = N->getOperand(AddrOpIdx);
9060 // Search for a use of the address operand that is an increment.
9061 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9062 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9064 if (User->getOpcode() != ISD::ADD ||
9065 UI.getUse().getResNo() != Addr.getResNo())
9068 // Check that the add is independent of the load/store. Otherwise, folding
9069 // it would create a cycle.
9070 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9073 // Find the new opcode for the updating load/store.
9075 bool isLaneOp = false;
9076 unsigned NewOpc = 0;
9077 unsigned NumVecs = 0;
9079 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9081 default: llvm_unreachable("unexpected intrinsic for Neon base update");
9082 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9084 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9086 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9088 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9090 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9091 NumVecs = 2; isLaneOp = true; break;
9092 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9093 NumVecs = 3; isLaneOp = true; break;
9094 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9095 NumVecs = 4; isLaneOp = true; break;
9096 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
9097 NumVecs = 1; isLoad = false; break;
9098 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
9099 NumVecs = 2; isLoad = false; break;
9100 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
9101 NumVecs = 3; isLoad = false; break;
9102 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
9103 NumVecs = 4; isLoad = false; break;
9104 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
9105 NumVecs = 2; isLoad = false; isLaneOp = true; break;
9106 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
9107 NumVecs = 3; isLoad = false; isLaneOp = true; break;
9108 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
9109 NumVecs = 4; isLoad = false; isLaneOp = true; break;
9113 switch (N->getOpcode()) {
9114 default: llvm_unreachable("unexpected opcode for Neon base update");
9115 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9116 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9117 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
9121 // Find the size of memory referenced by the load/store.
9124 VecTy = N->getValueType(0);
9126 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
9127 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9129 NumBytes /= VecTy.getVectorNumElements();
9131 // If the increment is a constant, it must match the memory ref size.
9132 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9133 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9134 uint64_t IncVal = CInc->getZExtValue();
9135 if (IncVal != NumBytes)
9137 } else if (NumBytes >= 3 * 16) {
9138 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9139 // separate instructions that make it harder to use a non-constant update.
9143 // Create the new updating load/store node.
9145 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
9147 for (n = 0; n < NumResultVecs; ++n)
9149 Tys[n++] = MVT::i32;
9150 Tys[n] = MVT::Other;
9151 SDVTList SDTys = DAG.getVTList(Tys, NumResultVecs+2);
9152 SmallVector<SDValue, 8> Ops;
9153 Ops.push_back(N->getOperand(0)); // incoming chain
9154 Ops.push_back(N->getOperand(AddrOpIdx));
9156 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
9157 Ops.push_back(N->getOperand(i));
9159 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
9160 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
9161 Ops.data(), Ops.size(),
9162 MemInt->getMemoryVT(),
9163 MemInt->getMemOperand());
9166 std::vector<SDValue> NewResults;
9167 for (unsigned i = 0; i < NumResultVecs; ++i) {
9168 NewResults.push_back(SDValue(UpdN.getNode(), i));
9170 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9171 DCI.CombineTo(N, NewResults);
9172 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9179 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9180 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9181 /// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9183 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9184 SelectionDAG &DAG = DCI.DAG;
9185 EVT VT = N->getValueType(0);
9186 // vldN-dup instructions only support 64-bit vectors for N > 1.
9187 if (!VT.is64BitVector())
9190 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9191 SDNode *VLD = N->getOperand(0).getNode();
9192 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9194 unsigned NumVecs = 0;
9195 unsigned NewOpc = 0;
9196 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9197 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9199 NewOpc = ARMISD::VLD2DUP;
9200 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9202 NewOpc = ARMISD::VLD3DUP;
9203 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9205 NewOpc = ARMISD::VLD4DUP;
9210 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9211 // numbers match the load.
9212 unsigned VLDLaneNo =
9213 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9214 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9216 // Ignore uses of the chain result.
9217 if (UI.getUse().getResNo() == NumVecs)
9220 if (User->getOpcode() != ARMISD::VDUPLANE ||
9221 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9225 // Create the vldN-dup node.
9228 for (n = 0; n < NumVecs; ++n)
9230 Tys[n] = MVT::Other;
9231 SDVTList SDTys = DAG.getVTList(Tys, NumVecs+1);
9232 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9233 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
9234 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
9235 Ops, 2, VLDMemInt->getMemoryVT(),
9236 VLDMemInt->getMemOperand());
9239 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9241 unsigned ResNo = UI.getUse().getResNo();
9242 // Ignore uses of the chain result.
9243 if (ResNo == NumVecs)
9246 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9249 // Now the vldN-lane intrinsic is dead except for its chain result.
9250 // Update uses of the chain.
9251 std::vector<SDValue> VLDDupResults;
9252 for (unsigned n = 0; n < NumVecs; ++n)
9253 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9254 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9255 DCI.CombineTo(VLD, VLDDupResults);
9260 /// PerformVDUPLANECombine - Target-specific dag combine xforms for
9261 /// ARMISD::VDUPLANE.
9262 static SDValue PerformVDUPLANECombine(SDNode *N,
9263 TargetLowering::DAGCombinerInfo &DCI) {
9264 SDValue Op = N->getOperand(0);
9266 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9267 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9268 if (CombineVLDDUP(N, DCI))
9269 return SDValue(N, 0);
9271 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9272 // redundant. Ignore bit_converts for now; element sizes are checked below.
9273 while (Op.getOpcode() == ISD::BITCAST)
9274 Op = Op.getOperand(0);
9275 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
9278 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9279 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9280 // The canonical VMOV for a zero vector uses a 32-bit element size.
9281 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9283 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9285 EVT VT = N->getValueType(0);
9286 if (EltSize > VT.getVectorElementType().getSizeInBits())
9289 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
9292 // isConstVecPow2 - Return true if each vector element is a power of 2, all
9293 // elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9294 static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9298 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9300 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9305 APFloat APF = C->getValueAPF();
9306 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9307 != APFloat::opOK || !isExact)
9310 c0 = (I == 0) ? cN : c0;
9311 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9318 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9319 /// can replace combinations of VMUL and VCVT (floating-point to integer)
9320 /// when the VMUL has a constant operand that is a power of 2.
9322 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9323 /// vmul.f32 d16, d17, d16
9324 /// vcvt.s32.f32 d16, d16
9326 /// vcvt.s32.f32 d16, d16, #3
9327 static SDValue PerformVCVTCombine(SDNode *N,
9328 TargetLowering::DAGCombinerInfo &DCI,
9329 const ARMSubtarget *Subtarget) {
9330 SelectionDAG &DAG = DCI.DAG;
9331 SDValue Op = N->getOperand(0);
9333 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9334 Op.getOpcode() != ISD::FMUL)
9338 SDValue N0 = Op->getOperand(0);
9339 SDValue ConstVec = Op->getOperand(1);
9340 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9342 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9343 !isConstVecPow2(ConstVec, isSigned, C))
9346 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9347 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
9348 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9349 // These instructions only exist converting from f32 to i32. We can handle
9350 // smaller integers by generating an extra truncate, but larger ones would
9355 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9356 Intrinsic::arm_neon_vcvtfp2fxu;
9357 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9358 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9359 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9360 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9361 DAG.getConstant(Log2_64(C), MVT::i32));
9363 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9364 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9369 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9370 /// can replace combinations of VCVT (integer to floating-point) and VDIV
9371 /// when the VDIV has a constant operand that is a power of 2.
9373 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9374 /// vcvt.f32.s32 d16, d16
9375 /// vdiv.f32 d16, d17, d16
9377 /// vcvt.f32.s32 d16, d16, #3
9378 static SDValue PerformVDIVCombine(SDNode *N,
9379 TargetLowering::DAGCombinerInfo &DCI,
9380 const ARMSubtarget *Subtarget) {
9381 SelectionDAG &DAG = DCI.DAG;
9382 SDValue Op = N->getOperand(0);
9383 unsigned OpOpcode = Op.getNode()->getOpcode();
9385 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9386 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9390 SDValue ConstVec = N->getOperand(1);
9391 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9393 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9394 !isConstVecPow2(ConstVec, isSigned, C))
9397 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9398 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9399 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9400 // These instructions only exist converting from i32 to f32. We can handle
9401 // smaller integers by generating an extra extend, but larger ones would
9406 SDValue ConvInput = Op.getOperand(0);
9407 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9408 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9409 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9410 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9413 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
9414 Intrinsic::arm_neon_vcvtfxu2fp;
9415 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9417 DAG.getConstant(IntrinsicOpcode, MVT::i32),
9418 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
9421 /// Getvshiftimm - Check if this is a valid build_vector for the immediate
9422 /// operand of a vector shift operation, where all the elements of the
9423 /// build_vector must have the same constant integer value.
9424 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9425 // Ignore bit_converts.
9426 while (Op.getOpcode() == ISD::BITCAST)
9427 Op = Op.getOperand(0);
9428 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9429 APInt SplatBits, SplatUndef;
9430 unsigned SplatBitSize;
9432 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9433 HasAnyUndefs, ElementBits) ||
9434 SplatBitSize > ElementBits)
9436 Cnt = SplatBits.getSExtValue();
9440 /// isVShiftLImm - Check if this is a valid build_vector for the immediate
9441 /// operand of a vector shift left operation. That value must be in the range:
9442 /// 0 <= Value < ElementBits for a left shift; or
9443 /// 0 <= Value <= ElementBits for a long left shift.
9444 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
9445 assert(VT.isVector() && "vector shift count is not a vector type");
9446 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9447 if (! getVShiftImm(Op, ElementBits, Cnt))
9449 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9452 /// isVShiftRImm - Check if this is a valid build_vector for the immediate
9453 /// operand of a vector shift right operation. For a shift opcode, the value
9454 /// is positive, but for an intrinsic the value count must be negative. The
9455 /// absolute value must be in the range:
9456 /// 1 <= |Value| <= ElementBits for a right shift; or
9457 /// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
9458 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
9460 assert(VT.isVector() && "vector shift count is not a vector type");
9461 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9462 if (! getVShiftImm(Op, ElementBits, Cnt))
9466 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9469 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9470 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9471 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9474 // Don't do anything for most intrinsics.
9477 // Vector shifts: check for immediate versions and lower them.
9478 // Note: This is done during DAG combining instead of DAG legalizing because
9479 // the build_vectors for 64-bit vector element shift counts are generally
9480 // not legal, and it is hard to see their values after they get legalized to
9481 // loads from a constant pool.
9482 case Intrinsic::arm_neon_vshifts:
9483 case Intrinsic::arm_neon_vshiftu:
9484 case Intrinsic::arm_neon_vshiftls:
9485 case Intrinsic::arm_neon_vshiftlu:
9486 case Intrinsic::arm_neon_vshiftn:
9487 case Intrinsic::arm_neon_vrshifts:
9488 case Intrinsic::arm_neon_vrshiftu:
9489 case Intrinsic::arm_neon_vrshiftn:
9490 case Intrinsic::arm_neon_vqshifts:
9491 case Intrinsic::arm_neon_vqshiftu:
9492 case Intrinsic::arm_neon_vqshiftsu:
9493 case Intrinsic::arm_neon_vqshiftns:
9494 case Intrinsic::arm_neon_vqshiftnu:
9495 case Intrinsic::arm_neon_vqshiftnsu:
9496 case Intrinsic::arm_neon_vqrshiftns:
9497 case Intrinsic::arm_neon_vqrshiftnu:
9498 case Intrinsic::arm_neon_vqrshiftnsu: {
9499 EVT VT = N->getOperand(1).getValueType();
9501 unsigned VShiftOpc = 0;
9504 case Intrinsic::arm_neon_vshifts:
9505 case Intrinsic::arm_neon_vshiftu:
9506 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9507 VShiftOpc = ARMISD::VSHL;
9510 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9511 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9512 ARMISD::VSHRs : ARMISD::VSHRu);
9517 case Intrinsic::arm_neon_vshiftls:
9518 case Intrinsic::arm_neon_vshiftlu:
9519 if (isVShiftLImm(N->getOperand(2), VT, true, Cnt))
9521 llvm_unreachable("invalid shift count for vshll intrinsic");
9523 case Intrinsic::arm_neon_vrshifts:
9524 case Intrinsic::arm_neon_vrshiftu:
9525 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9529 case Intrinsic::arm_neon_vqshifts:
9530 case Intrinsic::arm_neon_vqshiftu:
9531 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9535 case Intrinsic::arm_neon_vqshiftsu:
9536 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9538 llvm_unreachable("invalid shift count for vqshlu intrinsic");
9540 case Intrinsic::arm_neon_vshiftn:
9541 case Intrinsic::arm_neon_vrshiftn:
9542 case Intrinsic::arm_neon_vqshiftns:
9543 case Intrinsic::arm_neon_vqshiftnu:
9544 case Intrinsic::arm_neon_vqshiftnsu:
9545 case Intrinsic::arm_neon_vqrshiftns:
9546 case Intrinsic::arm_neon_vqrshiftnu:
9547 case Intrinsic::arm_neon_vqrshiftnsu:
9548 // Narrowing shifts require an immediate right shift.
9549 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9551 llvm_unreachable("invalid shift count for narrowing vector shift "
9555 llvm_unreachable("unhandled vector shift");
9559 case Intrinsic::arm_neon_vshifts:
9560 case Intrinsic::arm_neon_vshiftu:
9561 // Opcode already set above.
9563 case Intrinsic::arm_neon_vshiftls:
9564 case Intrinsic::arm_neon_vshiftlu:
9565 if (Cnt == VT.getVectorElementType().getSizeInBits())
9566 VShiftOpc = ARMISD::VSHLLi;
9568 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshiftls ?
9569 ARMISD::VSHLLs : ARMISD::VSHLLu);
9571 case Intrinsic::arm_neon_vshiftn:
9572 VShiftOpc = ARMISD::VSHRN; break;
9573 case Intrinsic::arm_neon_vrshifts:
9574 VShiftOpc = ARMISD::VRSHRs; break;
9575 case Intrinsic::arm_neon_vrshiftu:
9576 VShiftOpc = ARMISD::VRSHRu; break;
9577 case Intrinsic::arm_neon_vrshiftn:
9578 VShiftOpc = ARMISD::VRSHRN; break;
9579 case Intrinsic::arm_neon_vqshifts:
9580 VShiftOpc = ARMISD::VQSHLs; break;
9581 case Intrinsic::arm_neon_vqshiftu:
9582 VShiftOpc = ARMISD::VQSHLu; break;
9583 case Intrinsic::arm_neon_vqshiftsu:
9584 VShiftOpc = ARMISD::VQSHLsu; break;
9585 case Intrinsic::arm_neon_vqshiftns:
9586 VShiftOpc = ARMISD::VQSHRNs; break;
9587 case Intrinsic::arm_neon_vqshiftnu:
9588 VShiftOpc = ARMISD::VQSHRNu; break;
9589 case Intrinsic::arm_neon_vqshiftnsu:
9590 VShiftOpc = ARMISD::VQSHRNsu; break;
9591 case Intrinsic::arm_neon_vqrshiftns:
9592 VShiftOpc = ARMISD::VQRSHRNs; break;
9593 case Intrinsic::arm_neon_vqrshiftnu:
9594 VShiftOpc = ARMISD::VQRSHRNu; break;
9595 case Intrinsic::arm_neon_vqrshiftnsu:
9596 VShiftOpc = ARMISD::VQRSHRNsu; break;
9599 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
9600 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
9603 case Intrinsic::arm_neon_vshiftins: {
9604 EVT VT = N->getOperand(1).getValueType();
9606 unsigned VShiftOpc = 0;
9608 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9609 VShiftOpc = ARMISD::VSLI;
9610 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9611 VShiftOpc = ARMISD::VSRI;
9613 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
9616 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
9617 N->getOperand(1), N->getOperand(2),
9618 DAG.getConstant(Cnt, MVT::i32));
9621 case Intrinsic::arm_neon_vqrshifts:
9622 case Intrinsic::arm_neon_vqrshiftu:
9623 // No immediate versions of these to check for.
9630 /// PerformShiftCombine - Checks for immediate versions of vector shifts and
9631 /// lowers them. As with the vector shift intrinsics, this is done during DAG
9632 /// combining instead of DAG legalizing because the build_vectors for 64-bit
9633 /// vector element shift counts are generally not legal, and it is hard to see
9634 /// their values after they get legalized to loads from a constant pool.
9635 static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9636 const ARMSubtarget *ST) {
9637 EVT VT = N->getValueType(0);
9638 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9639 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9640 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9641 SDValue N1 = N->getOperand(1);
9642 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9643 SDValue N0 = N->getOperand(0);
9644 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9645 DAG.MaskedValueIsZero(N0.getOperand(0),
9646 APInt::getHighBitsSet(32, 16)))
9647 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
9651 // Nothing to be done for scalar shifts.
9652 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9653 if (!VT.isVector() || !TLI.isTypeLegal(VT))
9656 assert(ST->hasNEON() && "unexpected vector shift");
9659 switch (N->getOpcode()) {
9660 default: llvm_unreachable("unexpected shift opcode");
9663 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
9664 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
9665 DAG.getConstant(Cnt, MVT::i32));
9670 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9671 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9672 ARMISD::VSHRs : ARMISD::VSHRu);
9673 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
9674 DAG.getConstant(Cnt, MVT::i32));
9680 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9681 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9682 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9683 const ARMSubtarget *ST) {
9684 SDValue N0 = N->getOperand(0);
9686 // Check for sign- and zero-extensions of vector extract operations of 8-
9687 // and 16-bit vector elements. NEON supports these directly. They are
9688 // handled during DAG combining because type legalization will promote them
9689 // to 32-bit types and it is messy to recognize the operations after that.
9690 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9691 SDValue Vec = N0.getOperand(0);
9692 SDValue Lane = N0.getOperand(1);
9693 EVT VT = N->getValueType(0);
9694 EVT EltVT = N0.getValueType();
9695 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9697 if (VT == MVT::i32 &&
9698 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
9699 TLI.isTypeLegal(Vec.getValueType()) &&
9700 isa<ConstantSDNode>(Lane)) {
9703 switch (N->getOpcode()) {
9704 default: llvm_unreachable("unexpected opcode");
9705 case ISD::SIGN_EXTEND:
9706 Opc = ARMISD::VGETLANEs;
9708 case ISD::ZERO_EXTEND:
9709 case ISD::ANY_EXTEND:
9710 Opc = ARMISD::VGETLANEu;
9713 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
9720 /// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9721 /// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9722 static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9723 const ARMSubtarget *ST) {
9724 // If the target supports NEON, try to use vmax/vmin instructions for f32
9725 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
9726 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9727 // a NaN; only do the transformation when it matches that behavior.
9729 // For now only do this when using NEON for FP operations; if using VFP, it
9730 // is not obvious that the benefit outweighs the cost of switching to the
9732 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9733 N->getValueType(0) != MVT::f32)
9736 SDValue CondLHS = N->getOperand(0);
9737 SDValue CondRHS = N->getOperand(1);
9738 SDValue LHS = N->getOperand(2);
9739 SDValue RHS = N->getOperand(3);
9740 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9742 unsigned Opcode = 0;
9744 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
9745 IsReversed = false; // x CC y ? x : y
9746 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
9747 IsReversed = true ; // x CC y ? y : x
9761 // If LHS is NaN, an ordered comparison will be false and the result will
9762 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9763 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9764 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9765 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9767 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9768 // will return -0, so vmin can only be used for unsafe math or if one of
9769 // the operands is known to be nonzero.
9770 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
9771 !DAG.getTarget().Options.UnsafeFPMath &&
9772 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9774 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
9783 // If LHS is NaN, an ordered comparison will be false and the result will
9784 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9785 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9786 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9787 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9789 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9790 // will return +0, so vmax can only be used for unsafe math or if one of
9791 // the operands is known to be nonzero.
9792 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
9793 !DAG.getTarget().Options.UnsafeFPMath &&
9794 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9796 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
9802 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
9805 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9807 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9808 SDValue Cmp = N->getOperand(4);
9809 if (Cmp.getOpcode() != ARMISD::CMPZ)
9810 // Only looking at EQ and NE cases.
9813 EVT VT = N->getValueType(0);
9815 SDValue LHS = Cmp.getOperand(0);
9816 SDValue RHS = Cmp.getOperand(1);
9817 SDValue FalseVal = N->getOperand(0);
9818 SDValue TrueVal = N->getOperand(1);
9819 SDValue ARMcc = N->getOperand(2);
9820 ARMCC::CondCodes CC =
9821 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
9839 /// FIXME: Turn this into a target neutral optimization?
9841 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
9842 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9843 N->getOperand(3), Cmp);
9844 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9846 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9847 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9848 N->getOperand(3), NewCmp);
9851 if (Res.getNode()) {
9852 APInt KnownZero, KnownOne;
9853 DAG.ComputeMaskedBits(SDValue(N,0), KnownZero, KnownOne);
9854 // Capture demanded bits information that would be otherwise lost.
9855 if (KnownZero == 0xfffffffe)
9856 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9857 DAG.getValueType(MVT::i1));
9858 else if (KnownZero == 0xffffff00)
9859 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9860 DAG.getValueType(MVT::i8));
9861 else if (KnownZero == 0xffff0000)
9862 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9863 DAG.getValueType(MVT::i16));
9869 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
9870 DAGCombinerInfo &DCI) const {
9871 switch (N->getOpcode()) {
9873 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
9874 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
9875 case ISD::SUB: return PerformSUBCombine(N, DCI);
9876 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
9877 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
9878 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9879 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
9880 case ARMISD::BFI: return PerformBFICombine(N, DCI);
9881 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
9882 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
9883 case ISD::STORE: return PerformSTORECombine(N, DCI);
9884 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9885 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
9886 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
9887 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
9888 case ISD::FP_TO_SINT:
9889 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9890 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
9891 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
9894 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
9895 case ISD::SIGN_EXTEND:
9896 case ISD::ZERO_EXTEND:
9897 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9898 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
9899 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
9900 case ARMISD::VLD2DUP:
9901 case ARMISD::VLD3DUP:
9902 case ARMISD::VLD4DUP:
9903 return CombineBaseUpdate(N, DCI);
9904 case ARMISD::BUILD_VECTOR:
9905 return PerformARMBUILD_VECTORCombine(N, DCI);
9906 case ISD::INTRINSIC_VOID:
9907 case ISD::INTRINSIC_W_CHAIN:
9908 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9909 case Intrinsic::arm_neon_vld1:
9910 case Intrinsic::arm_neon_vld2:
9911 case Intrinsic::arm_neon_vld3:
9912 case Intrinsic::arm_neon_vld4:
9913 case Intrinsic::arm_neon_vld2lane:
9914 case Intrinsic::arm_neon_vld3lane:
9915 case Intrinsic::arm_neon_vld4lane:
9916 case Intrinsic::arm_neon_vst1:
9917 case Intrinsic::arm_neon_vst2:
9918 case Intrinsic::arm_neon_vst3:
9919 case Intrinsic::arm_neon_vst4:
9920 case Intrinsic::arm_neon_vst2lane:
9921 case Intrinsic::arm_neon_vst3lane:
9922 case Intrinsic::arm_neon_vst4lane:
9923 return CombineBaseUpdate(N, DCI);
9931 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9933 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9936 bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
9937 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
9938 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
9940 switch (VT.getSimpleVT().SimpleTy) {
9946 // Unaligned access can use (for example) LRDB, LRDH, LDR
9947 if (AllowsUnaligned) {
9949 *Fast = Subtarget->hasV7Ops();
9956 // For any little-endian targets with neon, we can support unaligned ld/st
9957 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
9958 // A big-endian target may also explictly support unaligned accesses
9959 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9969 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9970 unsigned AlignCheck) {
9971 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9972 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9975 EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9976 unsigned DstAlign, unsigned SrcAlign,
9977 bool IsMemset, bool ZeroMemset,
9979 MachineFunction &MF) const {
9980 const Function *F = MF.getFunction();
9982 // See if we can use NEON instructions for this...
9983 if ((!IsMemset || ZeroMemset) &&
9984 Subtarget->hasNEON() &&
9985 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9986 Attribute::NoImplicitFloat)) {
9989 (memOpAlign(SrcAlign, DstAlign, 16) ||
9990 (allowsUnalignedMemoryAccesses(MVT::v2f64, &Fast) && Fast))) {
9992 } else if (Size >= 8 &&
9993 (memOpAlign(SrcAlign, DstAlign, 8) ||
9994 (allowsUnalignedMemoryAccesses(MVT::f64, &Fast) && Fast))) {
9999 // Lowering to i32/i16 if the size permits.
10002 else if (Size >= 2)
10005 // Let the target-independent logic figure it out.
10009 bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10010 if (Val.getOpcode() != ISD::LOAD)
10013 EVT VT1 = Val.getValueType();
10014 if (!VT1.isSimple() || !VT1.isInteger() ||
10015 !VT2.isSimple() || !VT2.isInteger())
10018 switch (VT1.getSimpleVT().SimpleTy) {
10023 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10030 bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10031 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10034 if (!isTypeLegal(EVT::getEVT(Ty1)))
10037 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10039 // Assuming the caller doesn't have a zeroext or signext return parameter,
10040 // truncation all the way down to i1 is valid.
10045 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10049 unsigned Scale = 1;
10050 switch (VT.getSimpleVT().SimpleTy) {
10051 default: return false;
10066 if ((V & (Scale - 1)) != 0)
10069 return V == (V & ((1LL << 5) - 1));
10072 static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10073 const ARMSubtarget *Subtarget) {
10074 bool isNeg = false;
10080 switch (VT.getSimpleVT().SimpleTy) {
10081 default: return false;
10086 // + imm12 or - imm8
10088 return V == (V & ((1LL << 8) - 1));
10089 return V == (V & ((1LL << 12) - 1));
10092 // Same as ARM mode. FIXME: NEON?
10093 if (!Subtarget->hasVFP2())
10098 return V == (V & ((1LL << 8) - 1));
10102 /// isLegalAddressImmediate - Return true if the integer value can be used
10103 /// as the offset of the target addressing mode for load / store of the
10105 static bool isLegalAddressImmediate(int64_t V, EVT VT,
10106 const ARMSubtarget *Subtarget) {
10110 if (!VT.isSimple())
10113 if (Subtarget->isThumb1Only())
10114 return isLegalT1AddressImmediate(V, VT);
10115 else if (Subtarget->isThumb2())
10116 return isLegalT2AddressImmediate(V, VT, Subtarget);
10121 switch (VT.getSimpleVT().SimpleTy) {
10122 default: return false;
10127 return V == (V & ((1LL << 12) - 1));
10130 return V == (V & ((1LL << 8) - 1));
10133 if (!Subtarget->hasVFP2()) // FIXME: NEON?
10138 return V == (V & ((1LL << 8) - 1));
10142 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10144 int Scale = AM.Scale;
10148 switch (VT.getSimpleVT().SimpleTy) {
10149 default: return false;
10157 Scale = Scale & ~1;
10158 return Scale == 2 || Scale == 4 || Scale == 8;
10161 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10165 // Note, we allow "void" uses (basically, uses that aren't loads or
10166 // stores), because arm allows folding a scale into many arithmetic
10167 // operations. This should be made more precise and revisited later.
10169 // Allow r << imm, but the imm has to be a multiple of two.
10170 if (Scale & 1) return false;
10171 return isPowerOf2_32(Scale);
10175 /// isLegalAddressingMode - Return true if the addressing mode represented
10176 /// by AM is legal for this target, for a load/store of the specified type.
10177 bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
10179 EVT VT = getValueType(Ty, true);
10180 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
10183 // Can never fold addr of global into load/store.
10187 switch (AM.Scale) {
10188 case 0: // no scale reg, must be "r+i" or "r", or "i".
10191 if (Subtarget->isThumb1Only())
10195 // ARM doesn't support any R+R*scale+imm addr modes.
10199 if (!VT.isSimple())
10202 if (Subtarget->isThumb2())
10203 return isLegalT2ScaledAddressingMode(AM, VT);
10205 int Scale = AM.Scale;
10206 switch (VT.getSimpleVT().SimpleTy) {
10207 default: return false;
10211 if (Scale < 0) Scale = -Scale;
10215 return isPowerOf2_32(Scale & ~1);
10219 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10224 // Note, we allow "void" uses (basically, uses that aren't loads or
10225 // stores), because arm allows folding a scale into many arithmetic
10226 // operations. This should be made more precise and revisited later.
10228 // Allow r << imm, but the imm has to be a multiple of two.
10229 if (Scale & 1) return false;
10230 return isPowerOf2_32(Scale);
10236 /// isLegalICmpImmediate - Return true if the specified immediate is legal
10237 /// icmp immediate, that is the target has icmp instructions which can compare
10238 /// a register against the immediate without having to materialize the
10239 /// immediate into a register.
10240 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
10241 // Thumb2 and ARM modes can use cmn for negative immediates.
10242 if (!Subtarget->isThumb())
10243 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
10244 if (Subtarget->isThumb2())
10245 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
10246 // Thumb1 doesn't have cmn, and only 8-bit immediates.
10247 return Imm >= 0 && Imm <= 255;
10250 /// isLegalAddImmediate - Return true if the specified immediate is a legal add
10251 /// *or sub* immediate, that is the target has add or sub instructions which can
10252 /// add a register with the immediate without having to materialize the
10253 /// immediate into a register.
10254 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
10255 // Same encoding for add/sub, just flip the sign.
10256 int64_t AbsImm = llvm::abs64(Imm);
10257 if (!Subtarget->isThumb())
10258 return ARM_AM::getSOImmVal(AbsImm) != -1;
10259 if (Subtarget->isThumb2())
10260 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10261 // Thumb1 only has 8-bit unsigned immediate.
10262 return AbsImm >= 0 && AbsImm <= 255;
10265 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
10266 bool isSEXTLoad, SDValue &Base,
10267 SDValue &Offset, bool &isInc,
10268 SelectionDAG &DAG) {
10269 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10272 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
10273 // AddressingMode 3
10274 Base = Ptr->getOperand(0);
10275 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10276 int RHSC = (int)RHS->getZExtValue();
10277 if (RHSC < 0 && RHSC > -256) {
10278 assert(Ptr->getOpcode() == ISD::ADD);
10280 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10284 isInc = (Ptr->getOpcode() == ISD::ADD);
10285 Offset = Ptr->getOperand(1);
10287 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
10288 // AddressingMode 2
10289 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10290 int RHSC = (int)RHS->getZExtValue();
10291 if (RHSC < 0 && RHSC > -0x1000) {
10292 assert(Ptr->getOpcode() == ISD::ADD);
10294 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10295 Base = Ptr->getOperand(0);
10300 if (Ptr->getOpcode() == ISD::ADD) {
10302 ARM_AM::ShiftOpc ShOpcVal=
10303 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
10304 if (ShOpcVal != ARM_AM::no_shift) {
10305 Base = Ptr->getOperand(1);
10306 Offset = Ptr->getOperand(0);
10308 Base = Ptr->getOperand(0);
10309 Offset = Ptr->getOperand(1);
10314 isInc = (Ptr->getOpcode() == ISD::ADD);
10315 Base = Ptr->getOperand(0);
10316 Offset = Ptr->getOperand(1);
10320 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
10324 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
10325 bool isSEXTLoad, SDValue &Base,
10326 SDValue &Offset, bool &isInc,
10327 SelectionDAG &DAG) {
10328 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10331 Base = Ptr->getOperand(0);
10332 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10333 int RHSC = (int)RHS->getZExtValue();
10334 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10335 assert(Ptr->getOpcode() == ISD::ADD);
10337 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10339 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10340 isInc = Ptr->getOpcode() == ISD::ADD;
10341 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10349 /// getPreIndexedAddressParts - returns true by value, base pointer and
10350 /// offset pointer and addressing mode by reference if the node's address
10351 /// can be legally represented as pre-indexed load / store address.
10353 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10355 ISD::MemIndexedMode &AM,
10356 SelectionDAG &DAG) const {
10357 if (Subtarget->isThumb1Only())
10362 bool isSEXTLoad = false;
10363 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10364 Ptr = LD->getBasePtr();
10365 VT = LD->getMemoryVT();
10366 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10367 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10368 Ptr = ST->getBasePtr();
10369 VT = ST->getMemoryVT();
10374 bool isLegal = false;
10375 if (Subtarget->isThumb2())
10376 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10377 Offset, isInc, DAG);
10379 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10380 Offset, isInc, DAG);
10384 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10388 /// getPostIndexedAddressParts - returns true by value, base pointer and
10389 /// offset pointer and addressing mode by reference if this node can be
10390 /// combined with a load / store to form a post-indexed load / store.
10391 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
10394 ISD::MemIndexedMode &AM,
10395 SelectionDAG &DAG) const {
10396 if (Subtarget->isThumb1Only())
10401 bool isSEXTLoad = false;
10402 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10403 VT = LD->getMemoryVT();
10404 Ptr = LD->getBasePtr();
10405 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10406 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10407 VT = ST->getMemoryVT();
10408 Ptr = ST->getBasePtr();
10413 bool isLegal = false;
10414 if (Subtarget->isThumb2())
10415 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10418 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10424 // Swap base ptr and offset to catch more post-index load / store when
10425 // it's legal. In Thumb2 mode, offset must be an immediate.
10426 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10427 !Subtarget->isThumb2())
10428 std::swap(Base, Offset);
10430 // Post-indexed load / store update the base pointer.
10435 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10439 void ARMTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
10442 const SelectionDAG &DAG,
10443 unsigned Depth) const {
10444 unsigned BitWidth = KnownOne.getBitWidth();
10445 KnownZero = KnownOne = APInt(BitWidth, 0);
10446 switch (Op.getOpcode()) {
10452 // These nodes' second result is a boolean
10453 if (Op.getResNo() == 0)
10455 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10457 case ARMISD::CMOV: {
10458 // Bits are known zero/one if known on the LHS and RHS.
10459 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
10460 if (KnownZero == 0 && KnownOne == 0) return;
10462 APInt KnownZeroRHS, KnownOneRHS;
10463 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
10464 KnownZero &= KnownZeroRHS;
10465 KnownOne &= KnownOneRHS;
10471 //===----------------------------------------------------------------------===//
10472 // ARM Inline Assembly Support
10473 //===----------------------------------------------------------------------===//
10475 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10476 // Looking for "rev" which is V6+.
10477 if (!Subtarget->hasV6Ops())
10480 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10481 std::string AsmStr = IA->getAsmString();
10482 SmallVector<StringRef, 4> AsmPieces;
10483 SplitString(AsmStr, AsmPieces, ";\n");
10485 switch (AsmPieces.size()) {
10486 default: return false;
10488 AsmStr = AsmPieces[0];
10490 SplitString(AsmStr, AsmPieces, " \t,");
10493 if (AsmPieces.size() == 3 &&
10494 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10495 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
10496 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
10497 if (Ty && Ty->getBitWidth() == 32)
10498 return IntrinsicLowering::LowerToByteSwap(CI);
10506 /// getConstraintType - Given a constraint letter, return the type of
10507 /// constraint it is for this target.
10508 ARMTargetLowering::ConstraintType
10509 ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10510 if (Constraint.size() == 1) {
10511 switch (Constraint[0]) {
10513 case 'l': return C_RegisterClass;
10514 case 'w': return C_RegisterClass;
10515 case 'h': return C_RegisterClass;
10516 case 'x': return C_RegisterClass;
10517 case 't': return C_RegisterClass;
10518 case 'j': return C_Other; // Constant for movw.
10519 // An address with a single base register. Due to the way we
10520 // currently handle addresses it is the same as an 'r' memory constraint.
10521 case 'Q': return C_Memory;
10523 } else if (Constraint.size() == 2) {
10524 switch (Constraint[0]) {
10526 // All 'U+' constraints are addresses.
10527 case 'U': return C_Memory;
10530 return TargetLowering::getConstraintType(Constraint);
10533 /// Examine constraint type and operand type and determine a weight value.
10534 /// This object must already have been set up with the operand type
10535 /// and the current alternative constraint selected.
10536 TargetLowering::ConstraintWeight
10537 ARMTargetLowering::getSingleConstraintMatchWeight(
10538 AsmOperandInfo &info, const char *constraint) const {
10539 ConstraintWeight weight = CW_Invalid;
10540 Value *CallOperandVal = info.CallOperandVal;
10541 // If we don't have a value, we can't do a match,
10542 // but allow it at the lowest weight.
10543 if (CallOperandVal == NULL)
10545 Type *type = CallOperandVal->getType();
10546 // Look at the constraint type.
10547 switch (*constraint) {
10549 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10552 if (type->isIntegerTy()) {
10553 if (Subtarget->isThumb())
10554 weight = CW_SpecificReg;
10556 weight = CW_Register;
10560 if (type->isFloatingPointTy())
10561 weight = CW_Register;
10567 typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10569 ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
10571 if (Constraint.size() == 1) {
10572 // GCC ARM Constraint Letters
10573 switch (Constraint[0]) {
10574 case 'l': // Low regs or general regs.
10575 if (Subtarget->isThumb())
10576 return RCPair(0U, &ARM::tGPRRegClass);
10577 return RCPair(0U, &ARM::GPRRegClass);
10578 case 'h': // High regs or no regs.
10579 if (Subtarget->isThumb())
10580 return RCPair(0U, &ARM::hGPRRegClass);
10583 return RCPair(0U, &ARM::GPRRegClass);
10585 if (VT == MVT::f32)
10586 return RCPair(0U, &ARM::SPRRegClass);
10587 if (VT.getSizeInBits() == 64)
10588 return RCPair(0U, &ARM::DPRRegClass);
10589 if (VT.getSizeInBits() == 128)
10590 return RCPair(0U, &ARM::QPRRegClass);
10593 if (VT == MVT::f32)
10594 return RCPair(0U, &ARM::SPR_8RegClass);
10595 if (VT.getSizeInBits() == 64)
10596 return RCPair(0U, &ARM::DPR_8RegClass);
10597 if (VT.getSizeInBits() == 128)
10598 return RCPair(0U, &ARM::QPR_8RegClass);
10601 if (VT == MVT::f32)
10602 return RCPair(0U, &ARM::SPRRegClass);
10606 if (StringRef("{cc}").equals_lower(Constraint))
10607 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
10609 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10612 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10613 /// vector. If it is invalid, don't add anything to Ops.
10614 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
10615 std::string &Constraint,
10616 std::vector<SDValue>&Ops,
10617 SelectionDAG &DAG) const {
10618 SDValue Result(0, 0);
10620 // Currently only support length 1 constraints.
10621 if (Constraint.length() != 1) return;
10623 char ConstraintLetter = Constraint[0];
10624 switch (ConstraintLetter) {
10627 case 'I': case 'J': case 'K': case 'L':
10628 case 'M': case 'N': case 'O':
10629 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10633 int64_t CVal64 = C->getSExtValue();
10634 int CVal = (int) CVal64;
10635 // None of these constraints allow values larger than 32 bits. Check
10636 // that the value fits in an int.
10637 if (CVal != CVal64)
10640 switch (ConstraintLetter) {
10642 // Constant suitable for movw, must be between 0 and
10644 if (Subtarget->hasV6T2Ops())
10645 if (CVal >= 0 && CVal <= 65535)
10649 if (Subtarget->isThumb1Only()) {
10650 // This must be a constant between 0 and 255, for ADD
10652 if (CVal >= 0 && CVal <= 255)
10654 } else if (Subtarget->isThumb2()) {
10655 // A constant that can be used as an immediate value in a
10656 // data-processing instruction.
10657 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10660 // A constant that can be used as an immediate value in a
10661 // data-processing instruction.
10662 if (ARM_AM::getSOImmVal(CVal) != -1)
10668 if (Subtarget->isThumb()) { // FIXME thumb2
10669 // This must be a constant between -255 and -1, for negated ADD
10670 // immediates. This can be used in GCC with an "n" modifier that
10671 // prints the negated value, for use with SUB instructions. It is
10672 // not useful otherwise but is implemented for compatibility.
10673 if (CVal >= -255 && CVal <= -1)
10676 // This must be a constant between -4095 and 4095. It is not clear
10677 // what this constraint is intended for. Implemented for
10678 // compatibility with GCC.
10679 if (CVal >= -4095 && CVal <= 4095)
10685 if (Subtarget->isThumb1Only()) {
10686 // A 32-bit value where only one byte has a nonzero value. Exclude
10687 // zero to match GCC. This constraint is used by GCC internally for
10688 // constants that can be loaded with a move/shift combination.
10689 // It is not useful otherwise but is implemented for compatibility.
10690 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10692 } else if (Subtarget->isThumb2()) {
10693 // A constant whose bitwise inverse can be used as an immediate
10694 // value in a data-processing instruction. This can be used in GCC
10695 // with a "B" modifier that prints the inverted value, for use with
10696 // BIC and MVN instructions. It is not useful otherwise but is
10697 // implemented for compatibility.
10698 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10701 // A constant whose bitwise inverse can be used as an immediate
10702 // value in a data-processing instruction. This can be used in GCC
10703 // with a "B" modifier that prints the inverted value, for use with
10704 // BIC and MVN instructions. It is not useful otherwise but is
10705 // implemented for compatibility.
10706 if (ARM_AM::getSOImmVal(~CVal) != -1)
10712 if (Subtarget->isThumb1Only()) {
10713 // This must be a constant between -7 and 7,
10714 // for 3-operand ADD/SUB immediate instructions.
10715 if (CVal >= -7 && CVal < 7)
10717 } else if (Subtarget->isThumb2()) {
10718 // A constant whose negation can be used as an immediate value in a
10719 // data-processing instruction. This can be used in GCC with an "n"
10720 // modifier that prints the negated value, for use with SUB
10721 // instructions. It is not useful otherwise but is implemented for
10723 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10726 // A constant whose negation can be used as an immediate value in a
10727 // data-processing instruction. This can be used in GCC with an "n"
10728 // modifier that prints the negated value, for use with SUB
10729 // instructions. It is not useful otherwise but is implemented for
10731 if (ARM_AM::getSOImmVal(-CVal) != -1)
10737 if (Subtarget->isThumb()) { // FIXME thumb2
10738 // This must be a multiple of 4 between 0 and 1020, for
10739 // ADD sp + immediate.
10740 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10743 // A power of two or a constant between 0 and 32. This is used in
10744 // GCC for the shift amount on shifted register operands, but it is
10745 // useful in general for any shift amounts.
10746 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10752 if (Subtarget->isThumb()) { // FIXME thumb2
10753 // This must be a constant between 0 and 31, for shift amounts.
10754 if (CVal >= 0 && CVal <= 31)
10760 if (Subtarget->isThumb()) { // FIXME thumb2
10761 // This must be a multiple of 4 between -508 and 508, for
10762 // ADD/SUB sp = sp + immediate.
10763 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10768 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10772 if (Result.getNode()) {
10773 Ops.push_back(Result);
10776 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
10779 SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10780 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10781 unsigned Opcode = Op->getOpcode();
10782 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
10783 "Invalid opcode for Div/Rem lowering");
10784 bool isSigned = (Opcode == ISD::SDIVREM);
10785 EVT VT = Op->getValueType(0);
10786 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10789 switch (VT.getSimpleVT().SimpleTy) {
10790 default: llvm_unreachable("Unexpected request for libcall!");
10791 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10792 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10793 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10794 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
10797 SDValue InChain = DAG.getEntryNode();
10799 TargetLowering::ArgListTy Args;
10800 TargetLowering::ArgListEntry Entry;
10801 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10802 EVT ArgVT = Op->getOperand(i).getValueType();
10803 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10804 Entry.Node = Op->getOperand(i);
10806 Entry.isSExt = isSigned;
10807 Entry.isZExt = !isSigned;
10808 Args.push_back(Entry);
10811 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10814 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10818 CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, true,
10819 0, getLibcallCallingConv(LC), /*isTailCall=*/false,
10820 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
10821 Callee, Args, DAG, dl);
10822 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
10824 return CallInfo.first;
10828 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10829 // The ARM target isn't yet aware of offsets.
10833 bool ARM::isBitFieldInvertedMask(unsigned v) {
10834 if (v == 0xffffffff)
10837 // there can be 1's on either or both "outsides", all the "inside"
10838 // bits must be 0's
10839 unsigned TO = CountTrailingOnes_32(v);
10840 unsigned LO = CountLeadingOnes_32(v);
10841 v = (v >> TO) << TO;
10842 v = (v << LO) >> LO;
10846 /// isFPImmLegal - Returns true if the target can instruction select the
10847 /// specified FP immediate natively. If false, the legalizer will
10848 /// materialize the FP immediate as a load from a constant pool.
10849 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10850 if (!Subtarget->hasVFP3())
10852 if (VT == MVT::f32)
10853 return ARM_AM::getFP32Imm(Imm) != -1;
10854 if (VT == MVT::f64)
10855 return ARM_AM::getFP64Imm(Imm) != -1;
10859 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
10860 /// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10861 /// specified in the intrinsic calls.
10862 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10864 unsigned Intrinsic) const {
10865 switch (Intrinsic) {
10866 case Intrinsic::arm_neon_vld1:
10867 case Intrinsic::arm_neon_vld2:
10868 case Intrinsic::arm_neon_vld3:
10869 case Intrinsic::arm_neon_vld4:
10870 case Intrinsic::arm_neon_vld2lane:
10871 case Intrinsic::arm_neon_vld3lane:
10872 case Intrinsic::arm_neon_vld4lane: {
10873 Info.opc = ISD::INTRINSIC_W_CHAIN;
10874 // Conservatively set memVT to the entire set of vectors loaded.
10875 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
10876 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10877 Info.ptrVal = I.getArgOperand(0);
10879 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10880 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10881 Info.vol = false; // volatile loads with NEON intrinsics not supported
10882 Info.readMem = true;
10883 Info.writeMem = false;
10886 case Intrinsic::arm_neon_vst1:
10887 case Intrinsic::arm_neon_vst2:
10888 case Intrinsic::arm_neon_vst3:
10889 case Intrinsic::arm_neon_vst4:
10890 case Intrinsic::arm_neon_vst2lane:
10891 case Intrinsic::arm_neon_vst3lane:
10892 case Intrinsic::arm_neon_vst4lane: {
10893 Info.opc = ISD::INTRINSIC_VOID;
10894 // Conservatively set memVT to the entire set of vectors stored.
10895 unsigned NumElts = 0;
10896 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
10897 Type *ArgTy = I.getArgOperand(ArgI)->getType();
10898 if (!ArgTy->isVectorTy())
10900 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
10902 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10903 Info.ptrVal = I.getArgOperand(0);
10905 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10906 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10907 Info.vol = false; // volatile stores with NEON intrinsics not supported
10908 Info.readMem = false;
10909 Info.writeMem = true;
10912 case Intrinsic::arm_ldrex: {
10913 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10914 Info.opc = ISD::INTRINSIC_W_CHAIN;
10915 Info.memVT = MVT::getVT(PtrTy->getElementType());
10916 Info.ptrVal = I.getArgOperand(0);
10918 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10920 Info.readMem = true;
10921 Info.writeMem = false;
10924 case Intrinsic::arm_strex: {
10925 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10926 Info.opc = ISD::INTRINSIC_W_CHAIN;
10927 Info.memVT = MVT::getVT(PtrTy->getElementType());
10928 Info.ptrVal = I.getArgOperand(1);
10930 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10932 Info.readMem = false;
10933 Info.writeMem = true;
10936 case Intrinsic::arm_strexd: {
10937 Info.opc = ISD::INTRINSIC_W_CHAIN;
10938 Info.memVT = MVT::i64;
10939 Info.ptrVal = I.getArgOperand(2);
10943 Info.readMem = false;
10944 Info.writeMem = true;
10947 case Intrinsic::arm_ldrexd: {
10948 Info.opc = ISD::INTRINSIC_W_CHAIN;
10949 Info.memVT = MVT::i64;
10950 Info.ptrVal = I.getArgOperand(0);
10954 Info.readMem = true;
10955 Info.writeMem = false;