1 //===- ARMISelLowering.cpp - ARM DAG Lowering Implementation --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the interfaces that ARM uses to lower LLVM code into a
10 // selection DAG.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ARMISelLowering.h"
15 #include "ARMBaseInstrInfo.h"
16 #include "ARMBaseRegisterInfo.h"
17 #include "ARMCallingConv.h"
18 #include "ARMConstantPoolValue.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMPerfectShuffle.h"
21 #include "ARMRegisterInfo.h"
22 #include "ARMSelectionDAGInfo.h"
23 #include "ARMSubtarget.h"
24 #include "MCTargetDesc/ARMAddressingModes.h"
25 #include "MCTargetDesc/ARMBaseInfo.h"
26 #include "Utils/ARMBaseInfo.h"
27 #include "llvm/ADT/APFloat.h"
28 #include "llvm/ADT/APInt.h"
29 #include "llvm/ADT/ArrayRef.h"
30 #include "llvm/ADT/BitVector.h"
31 #include "llvm/ADT/DenseMap.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/SmallPtrSet.h"
34 #include "llvm/ADT/SmallVector.h"
35 #include "llvm/ADT/Statistic.h"
36 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/ADT/StringRef.h"
38 #include "llvm/ADT/StringSwitch.h"
39 #include "llvm/ADT/Triple.h"
40 #include "llvm/ADT/Twine.h"
41 #include "llvm/Analysis/VectorUtils.h"
42 #include "llvm/CodeGen/CallingConvLower.h"
43 #include "llvm/CodeGen/ISDOpcodes.h"
44 #include "llvm/CodeGen/IntrinsicLowering.h"
45 #include "llvm/CodeGen/MachineBasicBlock.h"
46 #include "llvm/CodeGen/MachineConstantPool.h"
47 #include "llvm/CodeGen/MachineFrameInfo.h"
48 #include "llvm/CodeGen/MachineFunction.h"
49 #include "llvm/CodeGen/MachineInstr.h"
50 #include "llvm/CodeGen/MachineInstrBuilder.h"
51 #include "llvm/CodeGen/MachineJumpTableInfo.h"
52 #include "llvm/CodeGen/MachineMemOperand.h"
53 #include "llvm/CodeGen/MachineOperand.h"
54 #include "llvm/CodeGen/MachineRegisterInfo.h"
55 #include "llvm/CodeGen/RuntimeLibcalls.h"
56 #include "llvm/CodeGen/SelectionDAG.h"
57 #include "llvm/CodeGen/SelectionDAGNodes.h"
58 #include "llvm/CodeGen/TargetInstrInfo.h"
59 #include "llvm/CodeGen/TargetLowering.h"
60 #include "llvm/CodeGen/TargetOpcodes.h"
61 #include "llvm/CodeGen/TargetRegisterInfo.h"
62 #include "llvm/CodeGen/TargetSubtargetInfo.h"
63 #include "llvm/CodeGen/ValueTypes.h"
64 #include "llvm/IR/Attributes.h"
65 #include "llvm/IR/CallingConv.h"
66 #include "llvm/IR/Constant.h"
67 #include "llvm/IR/Constants.h"
68 #include "llvm/IR/DataLayout.h"
69 #include "llvm/IR/DebugLoc.h"
70 #include "llvm/IR/DerivedTypes.h"
71 #include "llvm/IR/Function.h"
72 #include "llvm/IR/GlobalAlias.h"
73 #include "llvm/IR/GlobalValue.h"
74 #include "llvm/IR/GlobalVariable.h"
75 #include "llvm/IR/IRBuilder.h"
76 #include "llvm/IR/InlineAsm.h"
77 #include "llvm/IR/Instruction.h"
78 #include "llvm/IR/Instructions.h"
79 #include "llvm/IR/IntrinsicInst.h"
80 #include "llvm/IR/Intrinsics.h"
81 #include "llvm/IR/Module.h"
82 #include "llvm/IR/PatternMatch.h"
83 #include "llvm/IR/Type.h"
84 #include "llvm/IR/User.h"
85 #include "llvm/IR/Value.h"
86 #include "llvm/MC/MCInstrDesc.h"
87 #include "llvm/MC/MCInstrItineraries.h"
88 #include "llvm/MC/MCRegisterInfo.h"
89 #include "llvm/MC/MCSchedule.h"
90 #include "llvm/Support/AtomicOrdering.h"
91 #include "llvm/Support/BranchProbability.h"
92 #include "llvm/Support/Casting.h"
93 #include "llvm/Support/CodeGen.h"
94 #include "llvm/Support/CommandLine.h"
95 #include "llvm/Support/Compiler.h"
96 #include "llvm/Support/Debug.h"
97 #include "llvm/Support/ErrorHandling.h"
98 #include "llvm/Support/KnownBits.h"
99 #include "llvm/Support/MachineValueType.h"
100 #include "llvm/Support/MathExtras.h"
101 #include "llvm/Support/raw_ostream.h"
102 #include "llvm/Target/TargetMachine.h"
103 #include "llvm/Target/TargetOptions.h"
104 #include <algorithm>
105 #include <cassert>
106 #include <cstdint>
107 #include <cstdlib>
108 #include <iterator>
109 #include <limits>
110 #include <string>
111 #include <tuple>
112 #include <utility>
113 #include <vector>
114 
115 using namespace llvm;
116 using namespace llvm::PatternMatch;
117 
118 #define DEBUG_TYPE "arm-isel"
119 
120 STATISTIC(NumTailCalls, "Number of tail calls");
121 STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
122 STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
123 STATISTIC(NumConstpoolPromoted,
124   "Number of constants with their storage promoted into constant pools");
125 
126 static cl::opt<bool>
127 ARMInterworking("arm-interworking", cl::Hidden,
128   cl::desc("Enable / disable ARM interworking (for debugging only)"),
129   cl::init(true));
130 
131 static cl::opt<bool> EnableConstpoolPromotion(
132     "arm-promote-constant", cl::Hidden,
133     cl::desc("Enable / disable promotion of unnamed_addr constants into "
134              "constant pools"),
135     cl::init(false)); // FIXME: set to true by default once PR32780 is fixed
136 static cl::opt<unsigned> ConstpoolPromotionMaxSize(
137     "arm-promote-constant-max-size", cl::Hidden,
138     cl::desc("Maximum size of constant to promote into a constant pool"),
139     cl::init(64));
140 static cl::opt<unsigned> ConstpoolPromotionMaxTotal(
141     "arm-promote-constant-max-total", cl::Hidden,
142     cl::desc("Maximum size of ALL constants to promote into a constant pool"),
143     cl::init(128));
144 
145 // The APCS parameter registers.
146 static const MCPhysReg GPRArgRegs[] = {
147   ARM::R0, ARM::R1, ARM::R2, ARM::R3
148 };
149 
150 void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
151                                        MVT PromotedBitwiseVT) {
152   if (VT != PromotedLdStVT) {
153     setOperationAction(ISD::LOAD, VT, Promote);
154     AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
155 
156     setOperationAction(ISD::STORE, VT, Promote);
157     AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
158   }
159 
160   MVT ElemTy = VT.getVectorElementType();
161   if (ElemTy != MVT::f64)
162     setOperationAction(ISD::SETCC, VT, Custom);
163   setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
164   setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
165   if (ElemTy == MVT::i32) {
166     setOperationAction(ISD::SINT_TO_FP, VT, Custom);
167     setOperationAction(ISD::UINT_TO_FP, VT, Custom);
168     setOperationAction(ISD::FP_TO_SINT, VT, Custom);
169     setOperationAction(ISD::FP_TO_UINT, VT, Custom);
170   } else {
171     setOperationAction(ISD::SINT_TO_FP, VT, Expand);
172     setOperationAction(ISD::UINT_TO_FP, VT, Expand);
173     setOperationAction(ISD::FP_TO_SINT, VT, Expand);
174     setOperationAction(ISD::FP_TO_UINT, VT, Expand);
175   }
176   setOperationAction(ISD::BUILD_VECTOR,      VT, Custom);
177   setOperationAction(ISD::VECTOR_SHUFFLE,    VT, Custom);
178   setOperationAction(ISD::CONCAT_VECTORS,    VT, Legal);
179   setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
180   setOperationAction(ISD::SELECT,            VT, Expand);
181   setOperationAction(ISD::SELECT_CC,         VT, Expand);
182   setOperationAction(ISD::VSELECT,           VT, Expand);
183   setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
184   if (VT.isInteger()) {
185     setOperationAction(ISD::SHL, VT, Custom);
186     setOperationAction(ISD::SRA, VT, Custom);
187     setOperationAction(ISD::SRL, VT, Custom);
188   }
189 
190   // Promote all bit-wise operations.
191   if (VT.isInteger() && VT != PromotedBitwiseVT) {
192     setOperationAction(ISD::AND, VT, Promote);
193     AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
194     setOperationAction(ISD::OR,  VT, Promote);
195     AddPromotedToType (ISD::OR,  VT, PromotedBitwiseVT);
196     setOperationAction(ISD::XOR, VT, Promote);
197     AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
198   }
199 
200   // Neon does not support vector divide/remainder operations.
201   setOperationAction(ISD::SDIV, VT, Expand);
202   setOperationAction(ISD::UDIV, VT, Expand);
203   setOperationAction(ISD::FDIV, VT, Expand);
204   setOperationAction(ISD::SREM, VT, Expand);
205   setOperationAction(ISD::UREM, VT, Expand);
206   setOperationAction(ISD::FREM, VT, Expand);
207 
208   if (!VT.isFloatingPoint() &&
209       VT != MVT::v2i64 && VT != MVT::v1i64)
210     for (auto Opcode : {ISD::ABS, ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
211       setOperationAction(Opcode, VT, Legal);
212 }
213 
214 void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
215   addRegisterClass(VT, &ARM::DPRRegClass);
216   addTypeForNEON(VT, MVT::f64, MVT::v2i32);
217 }
218 
219 void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
220   addRegisterClass(VT, &ARM::DPairRegClass);
221   addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
222 }
223 
224 ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
225                                      const ARMSubtarget &STI)
226     : TargetLowering(TM), Subtarget(&STI) {
227   RegInfo = Subtarget->getRegisterInfo();
228   Itins = Subtarget->getInstrItineraryData();
229 
230   setBooleanContents(ZeroOrOneBooleanContent);
231   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
232 
233   if (!Subtarget->isTargetDarwin() && !Subtarget->isTargetIOS() &&
234       !Subtarget->isTargetWatchOS()) {
235     bool IsHFTarget = TM.Options.FloatABIType == FloatABI::Hard;
236     for (int LCID = 0; LCID < RTLIB::UNKNOWN_LIBCALL; ++LCID)
237       setLibcallCallingConv(static_cast<RTLIB::Libcall>(LCID),
238                             IsHFTarget ? CallingConv::ARM_AAPCS_VFP
239                                        : CallingConv::ARM_AAPCS);
240   }
241 
242   if (Subtarget->isTargetMachO()) {
243     // Uses VFP for Thumb libfuncs if available.
244     if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
245         Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
246       static const struct {
247         const RTLIB::Libcall Op;
248         const char * const Name;
249         const ISD::CondCode Cond;
250       } LibraryCalls[] = {
251         // Single-precision floating-point arithmetic.
252         { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID },
253         { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID },
254         { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID },
255         { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID },
256 
257         // Double-precision floating-point arithmetic.
258         { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID },
259         { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID },
260         { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID },
261         { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID },
262 
263         // Single-precision comparisons.
264         { RTLIB::OEQ_F32, "__eqsf2vfp",    ISD::SETNE },
265         { RTLIB::UNE_F32, "__nesf2vfp",    ISD::SETNE },
266         { RTLIB::OLT_F32, "__ltsf2vfp",    ISD::SETNE },
267         { RTLIB::OLE_F32, "__lesf2vfp",    ISD::SETNE },
268         { RTLIB::OGE_F32, "__gesf2vfp",    ISD::SETNE },
269         { RTLIB::OGT_F32, "__gtsf2vfp",    ISD::SETNE },
270         { RTLIB::UO_F32,  "__unordsf2vfp", ISD::SETNE },
271         { RTLIB::O_F32,   "__unordsf2vfp", ISD::SETEQ },
272 
273         // Double-precision comparisons.
274         { RTLIB::OEQ_F64, "__eqdf2vfp",    ISD::SETNE },
275         { RTLIB::UNE_F64, "__nedf2vfp",    ISD::SETNE },
276         { RTLIB::OLT_F64, "__ltdf2vfp",    ISD::SETNE },
277         { RTLIB::OLE_F64, "__ledf2vfp",    ISD::SETNE },
278         { RTLIB::OGE_F64, "__gedf2vfp",    ISD::SETNE },
279         { RTLIB::OGT_F64, "__gtdf2vfp",    ISD::SETNE },
280         { RTLIB::UO_F64,  "__unorddf2vfp", ISD::SETNE },
281         { RTLIB::O_F64,   "__unorddf2vfp", ISD::SETEQ },
282 
283         // Floating-point to integer conversions.
284         // i64 conversions are done via library routines even when generating VFP
285         // instructions, so use the same ones.
286         { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp",    ISD::SETCC_INVALID },
287         { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID },
288         { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp",    ISD::SETCC_INVALID },
289         { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID },
290 
291         // Conversions between floating types.
292         { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp",  ISD::SETCC_INVALID },
293         { RTLIB::FPEXT_F32_F64,   "__extendsfdf2vfp", ISD::SETCC_INVALID },
294 
295         // Integer to floating-point conversions.
296         // i64 conversions are done via library routines even when generating VFP
297         // instructions, so use the same ones.
298         // FIXME: There appears to be some naming inconsistency in ARM libgcc:
299         // e.g., __floatunsidf vs. __floatunssidfvfp.
300         { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp",    ISD::SETCC_INVALID },
301         { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID },
302         { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp",    ISD::SETCC_INVALID },
303         { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID },
304       };
305 
306       for (const auto &LC : LibraryCalls) {
307         setLibcallName(LC.Op, LC.Name);
308         if (LC.Cond != ISD::SETCC_INVALID)
309           setCmpLibcallCC(LC.Op, LC.Cond);
310       }
311     }
312   }
313 
314   // These libcalls are not available in 32-bit.
315   setLibcallName(RTLIB::SHL_I128, nullptr);
316   setLibcallName(RTLIB::SRL_I128, nullptr);
317   setLibcallName(RTLIB::SRA_I128, nullptr);
318 
319   // RTLIB
320   if (Subtarget->isAAPCS_ABI() &&
321       (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
322        Subtarget->isTargetMuslAEABI() || Subtarget->isTargetAndroid())) {
323     static const struct {
324       const RTLIB::Libcall Op;
325       const char * const Name;
326       const CallingConv::ID CC;
327       const ISD::CondCode Cond;
328     } LibraryCalls[] = {
329       // Double-precision floating-point arithmetic helper functions
330       // RTABI chapter 4.1.2, Table 2
331       { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332       { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333       { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334       { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 
336       // Double-precision floating-point comparison helper functions
337       // RTABI chapter 4.1.2, Table 3
338       { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
339       { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
340       { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
341       { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
342       { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
343       { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
344       { RTLIB::UO_F64,  "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
345       { RTLIB::O_F64,   "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
346 
347       // Single-precision floating-point arithmetic helper functions
348       // RTABI chapter 4.1.2, Table 4
349       { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
350       { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
351       { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
352       { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
353 
354       // Single-precision floating-point comparison helper functions
355       // RTABI chapter 4.1.2, Table 5
356       { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
357       { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
358       { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
359       { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
360       { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
361       { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
362       { RTLIB::UO_F32,  "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
363       { RTLIB::O_F32,   "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
364 
365       // Floating-point to integer conversions.
366       // RTABI chapter 4.1.2, Table 6
367       { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
368       { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
369       { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
370       { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
371       { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
372       { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
373       { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
374       { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
375 
376       // Conversions between floating types.
377       // RTABI chapter 4.1.2, Table 7
378       { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
379       { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
380       { RTLIB::FPEXT_F32_F64,   "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
381 
382       // Integer to floating-point conversions.
383       // RTABI chapter 4.1.2, Table 8
384       { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
385       { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
386       { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
387       { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
388       { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
389       { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
390       { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
391       { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
392 
393       // Long long helper functions
394       // RTABI chapter 4.2, Table 9
395       { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
396       { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
397       { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
398       { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
399 
400       // Integer division functions
401       // RTABI chapter 4.3.1
402       { RTLIB::SDIV_I8,  "__aeabi_idiv",     CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
403       { RTLIB::SDIV_I16, "__aeabi_idiv",     CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
404       { RTLIB::SDIV_I32, "__aeabi_idiv",     CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
405       { RTLIB::SDIV_I64, "__aeabi_ldivmod",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
406       { RTLIB::UDIV_I8,  "__aeabi_uidiv",    CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
407       { RTLIB::UDIV_I16, "__aeabi_uidiv",    CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
408       { RTLIB::UDIV_I32, "__aeabi_uidiv",    CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
409       { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
410     };
411 
412     for (const auto &LC : LibraryCalls) {
413       setLibcallName(LC.Op, LC.Name);
414       setLibcallCallingConv(LC.Op, LC.CC);
415       if (LC.Cond != ISD::SETCC_INVALID)
416         setCmpLibcallCC(LC.Op, LC.Cond);
417     }
418 
419     // EABI dependent RTLIB
420     if (TM.Options.EABIVersion == EABI::EABI4 ||
421         TM.Options.EABIVersion == EABI::EABI5) {
422       static const struct {
423         const RTLIB::Libcall Op;
424         const char *const Name;
425         const CallingConv::ID CC;
426         const ISD::CondCode Cond;
427       } MemOpsLibraryCalls[] = {
428         // Memory operations
429         // RTABI chapter 4.3.4
430         { RTLIB::MEMCPY,  "__aeabi_memcpy",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
431         { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
432         { RTLIB::MEMSET,  "__aeabi_memset",  CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
433       };
434 
435       for (const auto &LC : MemOpsLibraryCalls) {
436         setLibcallName(LC.Op, LC.Name);
437         setLibcallCallingConv(LC.Op, LC.CC);
438         if (LC.Cond != ISD::SETCC_INVALID)
439           setCmpLibcallCC(LC.Op, LC.Cond);
440       }
441     }
442   }
443 
444   if (Subtarget->isTargetWindows()) {
445     static const struct {
446       const RTLIB::Libcall Op;
447       const char * const Name;
448       const CallingConv::ID CC;
449     } LibraryCalls[] = {
450       { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
451       { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
452       { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
453       { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
454       { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
455       { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
456       { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
457       { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
458     };
459 
460     for (const auto &LC : LibraryCalls) {
461       setLibcallName(LC.Op, LC.Name);
462       setLibcallCallingConv(LC.Op, LC.CC);
463     }
464   }
465 
466   // Use divmod compiler-rt calls for iOS 5.0 and later.
467   if (Subtarget->isTargetMachO() &&
468       !(Subtarget->isTargetIOS() &&
469         Subtarget->getTargetTriple().isOSVersionLT(5, 0))) {
470     setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
471     setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
472   }
473 
474   // The half <-> float conversion functions are always soft-float on
475   // non-watchos platforms, but are needed for some targets which use a
476   // hard-float calling convention by default.
477   if (!Subtarget->isTargetWatchABI()) {
478     if (Subtarget->isAAPCS_ABI()) {
479       setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
480       setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
481       setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
482     } else {
483       setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
484       setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
485       setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
486     }
487   }
488 
489   // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
490   // a __gnu_ prefix (which is the default).
491   if (Subtarget->isTargetAEABI()) {
492     static const struct {
493       const RTLIB::Libcall Op;
494       const char * const Name;
495       const CallingConv::ID CC;
496     } LibraryCalls[] = {
497       { RTLIB::FPROUND_F32_F16, "__aeabi_f2h", CallingConv::ARM_AAPCS },
498       { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS },
499       { RTLIB::FPEXT_F16_F32, "__aeabi_h2f", CallingConv::ARM_AAPCS },
500     };
501 
502     for (const auto &LC : LibraryCalls) {
503       setLibcallName(LC.Op, LC.Name);
504       setLibcallCallingConv(LC.Op, LC.CC);
505     }
506   }
507 
508   if (Subtarget->isThumb1Only())
509     addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
510   else
511     addRegisterClass(MVT::i32, &ARM::GPRRegClass);
512 
513   if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
514       !Subtarget->isThumb1Only()) {
515     addRegisterClass(MVT::f32, &ARM::SPRRegClass);
516     addRegisterClass(MVT::f64, &ARM::DPRRegClass);
517   }
518 
519   if (Subtarget->hasFullFP16()) {
520     addRegisterClass(MVT::f16, &ARM::HPRRegClass);
521     setOperationAction(ISD::BITCAST, MVT::i16, Custom);
522     setOperationAction(ISD::BITCAST, MVT::i32, Custom);
523     setOperationAction(ISD::BITCAST, MVT::f16, Custom);
524 
525     setOperationAction(ISD::FMINNUM, MVT::f16, Legal);
526     setOperationAction(ISD::FMAXNUM, MVT::f16, Legal);
527   }
528 
529   for (MVT VT : MVT::vector_valuetypes()) {
530     for (MVT InnerVT : MVT::vector_valuetypes()) {
531       setTruncStoreAction(VT, InnerVT, Expand);
532       setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
533       setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
534       setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
535     }
536 
537     setOperationAction(ISD::MULHS, VT, Expand);
538     setOperationAction(ISD::SMUL_LOHI, VT, Expand);
539     setOperationAction(ISD::MULHU, VT, Expand);
540     setOperationAction(ISD::UMUL_LOHI, VT, Expand);
541 
542     setOperationAction(ISD::BSWAP, VT, Expand);
543   }
544 
545   setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
546   setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
547 
548   setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
549   setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
550 
551   if (Subtarget->hasNEON()) {
552     addDRTypeForNEON(MVT::v2f32);
553     addDRTypeForNEON(MVT::v8i8);
554     addDRTypeForNEON(MVT::v4i16);
555     addDRTypeForNEON(MVT::v2i32);
556     addDRTypeForNEON(MVT::v1i64);
557 
558     addQRTypeForNEON(MVT::v4f32);
559     addQRTypeForNEON(MVT::v2f64);
560     addQRTypeForNEON(MVT::v16i8);
561     addQRTypeForNEON(MVT::v8i16);
562     addQRTypeForNEON(MVT::v4i32);
563     addQRTypeForNEON(MVT::v2i64);
564 
565     if (Subtarget->hasFullFP16()) {
566       addQRTypeForNEON(MVT::v8f16);
567       addDRTypeForNEON(MVT::v4f16);
568     }
569 
570     // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
571     // neither Neon nor VFP support any arithmetic operations on it.
572     // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
573     // supported for v4f32.
574     setOperationAction(ISD::FADD, MVT::v2f64, Expand);
575     setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
576     setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
577     // FIXME: Code duplication: FDIV and FREM are expanded always, see
578     // ARMTargetLowering::addTypeForNEON method for details.
579     setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
580     setOperationAction(ISD::FREM, MVT::v2f64, Expand);
581     // FIXME: Create unittest.
582     // In another words, find a way when "copysign" appears in DAG with vector
583     // operands.
584     setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
585     // FIXME: Code duplication: SETCC has custom operation action, see
586     // ARMTargetLowering::addTypeForNEON method for details.
587     setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
588     // FIXME: Create unittest for FNEG and for FABS.
589     setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
590     setOperationAction(ISD::FABS, MVT::v2f64, Expand);
591     setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
592     setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
593     setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
594     setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
595     setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
596     setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
597     setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
598     setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
599     setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
600     // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
601     setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
602     setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
603     setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
604     setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
605     setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
606     setOperationAction(ISD::FMA, MVT::v2f64, Expand);
607 
608     setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
609     setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
610     setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
611     setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
612     setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
613     setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
614     setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
615     setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
616     setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
617     setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
618     setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
619     setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
620     setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
621     setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
622 
623     // Mark v2f32 intrinsics.
624     setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
625     setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
626     setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
627     setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
628     setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
629     setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
630     setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
631     setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
632     setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
633     setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
634     setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
635     setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
636     setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
637     setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
638 
639     // Neon does not support some operations on v1i64 and v2i64 types.
640     setOperationAction(ISD::MUL, MVT::v1i64, Expand);
641     // Custom handling for some quad-vector types to detect VMULL.
642     setOperationAction(ISD::MUL, MVT::v8i16, Custom);
643     setOperationAction(ISD::MUL, MVT::v4i32, Custom);
644     setOperationAction(ISD::MUL, MVT::v2i64, Custom);
645     // Custom handling for some vector types to avoid expensive expansions
646     setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
647     setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
648     setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
649     setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
650     // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
651     // a destination type that is wider than the source, and nor does
652     // it have a FP_TO_[SU]INT instruction with a narrower destination than
653     // source.
654     setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
655     setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Custom);
656     setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
657     setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
658     setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
659     setOperationAction(ISD::FP_TO_UINT, MVT::v8i16, Custom);
660     setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
661     setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
662 
663     setOperationAction(ISD::FP_ROUND,   MVT::v2f32, Expand);
664     setOperationAction(ISD::FP_EXTEND,  MVT::v2f64, Expand);
665 
666     // NEON does not have single instruction CTPOP for vectors with element
667     // types wider than 8-bits.  However, custom lowering can leverage the
668     // v8i8/v16i8 vcnt instruction.
669     setOperationAction(ISD::CTPOP,      MVT::v2i32, Custom);
670     setOperationAction(ISD::CTPOP,      MVT::v4i32, Custom);
671     setOperationAction(ISD::CTPOP,      MVT::v4i16, Custom);
672     setOperationAction(ISD::CTPOP,      MVT::v8i16, Custom);
673     setOperationAction(ISD::CTPOP,      MVT::v1i64, Custom);
674     setOperationAction(ISD::CTPOP,      MVT::v2i64, Custom);
675 
676     setOperationAction(ISD::CTLZ,       MVT::v1i64, Expand);
677     setOperationAction(ISD::CTLZ,       MVT::v2i64, Expand);
678 
679     // NEON does not have single instruction CTTZ for vectors.
680     setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
681     setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
682     setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
683     setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
684 
685     setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
686     setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
687     setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
688     setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
689 
690     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
691     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
692     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
693     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
694 
695     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
696     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
697     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
698     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
699 
700     // NEON only has FMA instructions as of VFP4.
701     if (!Subtarget->hasVFP4()) {
702       setOperationAction(ISD::FMA, MVT::v2f32, Expand);
703       setOperationAction(ISD::FMA, MVT::v4f32, Expand);
704     }
705 
706     setTargetDAGCombine(ISD::INTRINSIC_VOID);
707     setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
708     setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
709     setTargetDAGCombine(ISD::SHL);
710     setTargetDAGCombine(ISD::SRL);
711     setTargetDAGCombine(ISD::SRA);
712     setTargetDAGCombine(ISD::SIGN_EXTEND);
713     setTargetDAGCombine(ISD::ZERO_EXTEND);
714     setTargetDAGCombine(ISD::ANY_EXTEND);
715     setTargetDAGCombine(ISD::BUILD_VECTOR);
716     setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
717     setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
718     setTargetDAGCombine(ISD::STORE);
719     setTargetDAGCombine(ISD::FP_TO_SINT);
720     setTargetDAGCombine(ISD::FP_TO_UINT);
721     setTargetDAGCombine(ISD::FDIV);
722     setTargetDAGCombine(ISD::LOAD);
723 
724     // It is legal to extload from v4i8 to v4i16 or v4i32.
725     for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
726                    MVT::v2i32}) {
727       for (MVT VT : MVT::integer_vector_valuetypes()) {
728         setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
729         setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
730         setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
731       }
732     }
733   }
734 
735   if (Subtarget->isFPOnlySP()) {
736     // When targeting a floating-point unit with only single-precision
737     // operations, f64 is legal for the few double-precision instructions which
738     // are present However, no double-precision operations other than moves,
739     // loads and stores are provided by the hardware.
740     setOperationAction(ISD::FADD,       MVT::f64, Expand);
741     setOperationAction(ISD::FSUB,       MVT::f64, Expand);
742     setOperationAction(ISD::FMUL,       MVT::f64, Expand);
743     setOperationAction(ISD::FMA,        MVT::f64, Expand);
744     setOperationAction(ISD::FDIV,       MVT::f64, Expand);
745     setOperationAction(ISD::FREM,       MVT::f64, Expand);
746     setOperationAction(ISD::FCOPYSIGN,  MVT::f64, Expand);
747     setOperationAction(ISD::FGETSIGN,   MVT::f64, Expand);
748     setOperationAction(ISD::FNEG,       MVT::f64, Expand);
749     setOperationAction(ISD::FABS,       MVT::f64, Expand);
750     setOperationAction(ISD::FSQRT,      MVT::f64, Expand);
751     setOperationAction(ISD::FSIN,       MVT::f64, Expand);
752     setOperationAction(ISD::FCOS,       MVT::f64, Expand);
753     setOperationAction(ISD::FPOW,       MVT::f64, Expand);
754     setOperationAction(ISD::FLOG,       MVT::f64, Expand);
755     setOperationAction(ISD::FLOG2,      MVT::f64, Expand);
756     setOperationAction(ISD::FLOG10,     MVT::f64, Expand);
757     setOperationAction(ISD::FEXP,       MVT::f64, Expand);
758     setOperationAction(ISD::FEXP2,      MVT::f64, Expand);
759     setOperationAction(ISD::FCEIL,      MVT::f64, Expand);
760     setOperationAction(ISD::FTRUNC,     MVT::f64, Expand);
761     setOperationAction(ISD::FRINT,      MVT::f64, Expand);
762     setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
763     setOperationAction(ISD::FFLOOR,     MVT::f64, Expand);
764     setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
765     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
766     setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
767     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
768     setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
769     setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
770     setOperationAction(ISD::FP_ROUND,   MVT::f32, Custom);
771     setOperationAction(ISD::FP_EXTEND,  MVT::f64, Custom);
772   }
773 
774   computeRegisterProperties(Subtarget->getRegisterInfo());
775 
776   // ARM does not have floating-point extending loads.
777   for (MVT VT : MVT::fp_valuetypes()) {
778     setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
779     setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
780   }
781 
782   // ... or truncating stores
783   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
784   setTruncStoreAction(MVT::f32, MVT::f16, Expand);
785   setTruncStoreAction(MVT::f64, MVT::f16, Expand);
786 
787   // ARM does not have i1 sign extending load.
788   for (MVT VT : MVT::integer_valuetypes())
789     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
790 
791   // ARM supports all 4 flavors of integer indexed load / store.
792   if (!Subtarget->isThumb1Only()) {
793     for (unsigned im = (unsigned)ISD::PRE_INC;
794          im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
795       setIndexedLoadAction(im,  MVT::i1,  Legal);
796       setIndexedLoadAction(im,  MVT::i8,  Legal);
797       setIndexedLoadAction(im,  MVT::i16, Legal);
798       setIndexedLoadAction(im,  MVT::i32, Legal);
799       setIndexedStoreAction(im, MVT::i1,  Legal);
800       setIndexedStoreAction(im, MVT::i8,  Legal);
801       setIndexedStoreAction(im, MVT::i16, Legal);
802       setIndexedStoreAction(im, MVT::i32, Legal);
803     }
804   } else {
805     // Thumb-1 has limited post-inc load/store support - LDM r0!, {r1}.
806     setIndexedLoadAction(ISD::POST_INC, MVT::i32,  Legal);
807     setIndexedStoreAction(ISD::POST_INC, MVT::i32,  Legal);
808   }
809 
810   setOperationAction(ISD::SADDO, MVT::i32, Custom);
811   setOperationAction(ISD::UADDO, MVT::i32, Custom);
812   setOperationAction(ISD::SSUBO, MVT::i32, Custom);
813   setOperationAction(ISD::USUBO, MVT::i32, Custom);
814 
815   setOperationAction(ISD::ADDCARRY, MVT::i32, Custom);
816   setOperationAction(ISD::SUBCARRY, MVT::i32, Custom);
817 
818   // i64 operation support.
819   setOperationAction(ISD::MUL,     MVT::i64, Expand);
820   setOperationAction(ISD::MULHU,   MVT::i32, Expand);
821   if (Subtarget->isThumb1Only()) {
822     setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
823     setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
824   }
825   if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
826       || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
827     setOperationAction(ISD::MULHS, MVT::i32, Expand);
828 
829   setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
830   setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
831   setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
832   setOperationAction(ISD::SRL,       MVT::i64, Custom);
833   setOperationAction(ISD::SRA,       MVT::i64, Custom);
834   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
835 
836   // Expand to __aeabi_l{lsl,lsr,asr} calls for Thumb1.
837   if (Subtarget->isThumb1Only()) {
838     setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
839     setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
840     setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
841   }
842 
843   if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops())
844     setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
845 
846   // ARM does not have ROTL.
847   setOperationAction(ISD::ROTL, MVT::i32, Expand);
848   for (MVT VT : MVT::vector_valuetypes()) {
849     setOperationAction(ISD::ROTL, VT, Expand);
850     setOperationAction(ISD::ROTR, VT, Expand);
851   }
852   setOperationAction(ISD::CTTZ,  MVT::i32, Custom);
853   setOperationAction(ISD::CTPOP, MVT::i32, Expand);
854   if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only()) {
855     setOperationAction(ISD::CTLZ, MVT::i32, Expand);
856     setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, LibCall);
857   }
858 
859   // @llvm.readcyclecounter requires the Performance Monitors extension.
860   // Default to the 0 expansion on unsupported platforms.
861   // FIXME: Technically there are older ARM CPUs that have
862   // implementation-specific ways of obtaining this information.
863   if (Subtarget->hasPerfMon())
864     setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
865 
866   // Only ARMv6 has BSWAP.
867   if (!Subtarget->hasV6Ops())
868     setOperationAction(ISD::BSWAP, MVT::i32, Expand);
869 
870   bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivideInThumbMode()
871                                         : Subtarget->hasDivideInARMMode();
872   if (!hasDivide) {
873     // These are expanded into libcalls if the cpu doesn't have HW divider.
874     setOperationAction(ISD::SDIV,  MVT::i32, LibCall);
875     setOperationAction(ISD::UDIV,  MVT::i32, LibCall);
876   }
877 
878   if (Subtarget->isTargetWindows() && !Subtarget->hasDivideInThumbMode()) {
879     setOperationAction(ISD::SDIV, MVT::i32, Custom);
880     setOperationAction(ISD::UDIV, MVT::i32, Custom);
881 
882     setOperationAction(ISD::SDIV, MVT::i64, Custom);
883     setOperationAction(ISD::UDIV, MVT::i64, Custom);
884   }
885 
886   setOperationAction(ISD::SREM,  MVT::i32, Expand);
887   setOperationAction(ISD::UREM,  MVT::i32, Expand);
888 
889   // Register based DivRem for AEABI (RTABI 4.2)
890   if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
891       Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() ||
892       Subtarget->isTargetWindows()) {
893     setOperationAction(ISD::SREM, MVT::i64, Custom);
894     setOperationAction(ISD::UREM, MVT::i64, Custom);
895     HasStandaloneRem = false;
896 
897     if (Subtarget->isTargetWindows()) {
898       const struct {
899         const RTLIB::Libcall Op;
900         const char * const Name;
901         const CallingConv::ID CC;
902       } LibraryCalls[] = {
903         { RTLIB::SDIVREM_I8, "__rt_sdiv", CallingConv::ARM_AAPCS },
904         { RTLIB::SDIVREM_I16, "__rt_sdiv", CallingConv::ARM_AAPCS },
905         { RTLIB::SDIVREM_I32, "__rt_sdiv", CallingConv::ARM_AAPCS },
906         { RTLIB::SDIVREM_I64, "__rt_sdiv64", CallingConv::ARM_AAPCS },
907 
908         { RTLIB::UDIVREM_I8, "__rt_udiv", CallingConv::ARM_AAPCS },
909         { RTLIB::UDIVREM_I16, "__rt_udiv", CallingConv::ARM_AAPCS },
910         { RTLIB::UDIVREM_I32, "__rt_udiv", CallingConv::ARM_AAPCS },
911         { RTLIB::UDIVREM_I64, "__rt_udiv64", CallingConv::ARM_AAPCS },
912       };
913 
914       for (const auto &LC : LibraryCalls) {
915         setLibcallName(LC.Op, LC.Name);
916         setLibcallCallingConv(LC.Op, LC.CC);
917       }
918     } else {
919       const struct {
920         const RTLIB::Libcall Op;
921         const char * const Name;
922         const CallingConv::ID CC;
923       } LibraryCalls[] = {
924         { RTLIB::SDIVREM_I8, "__aeabi_idivmod", CallingConv::ARM_AAPCS },
925         { RTLIB::SDIVREM_I16, "__aeabi_idivmod", CallingConv::ARM_AAPCS },
926         { RTLIB::SDIVREM_I32, "__aeabi_idivmod", CallingConv::ARM_AAPCS },
927         { RTLIB::SDIVREM_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS },
928 
929         { RTLIB::UDIVREM_I8, "__aeabi_uidivmod", CallingConv::ARM_AAPCS },
930         { RTLIB::UDIVREM_I16, "__aeabi_uidivmod", CallingConv::ARM_AAPCS },
931         { RTLIB::UDIVREM_I32, "__aeabi_uidivmod", CallingConv::ARM_AAPCS },
932         { RTLIB::UDIVREM_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS },
933       };
934 
935       for (const auto &LC : LibraryCalls) {
936         setLibcallName(LC.Op, LC.Name);
937         setLibcallCallingConv(LC.Op, LC.CC);
938       }
939     }
940 
941     setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
942     setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
943     setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
944     setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
945   } else {
946     setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
947     setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
948   }
949 
950   if (Subtarget->isTargetWindows() && Subtarget->getTargetTriple().isOSMSVCRT())
951     for (auto &VT : {MVT::f32, MVT::f64})
952       setOperationAction(ISD::FPOWI, VT, Custom);
953 
954   setOperationAction(ISD::GlobalAddress, MVT::i32,   Custom);
955   setOperationAction(ISD::ConstantPool,  MVT::i32,   Custom);
956   setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
957   setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
958 
959   setOperationAction(ISD::TRAP, MVT::Other, Legal);
960   setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
961 
962   // Use the default implementation.
963   setOperationAction(ISD::VASTART,            MVT::Other, Custom);
964   setOperationAction(ISD::VAARG,              MVT::Other, Expand);
965   setOperationAction(ISD::VACOPY,             MVT::Other, Expand);
966   setOperationAction(ISD::VAEND,              MVT::Other, Expand);
967   setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand);
968   setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
969 
970   if (Subtarget->isTargetWindows())
971     setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
972   else
973     setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
974 
975   // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
976   // the default expansion.
977   InsertFencesForAtomic = false;
978   if (Subtarget->hasAnyDataBarrier() &&
979       (!Subtarget->isThumb() || Subtarget->hasV8MBaselineOps())) {
980     // ATOMIC_FENCE needs custom lowering; the others should have been expanded
981     // to ldrex/strex loops already.
982     setOperationAction(ISD::ATOMIC_FENCE,     MVT::Other, Custom);
983     if (!Subtarget->isThumb() || !Subtarget->isMClass())
984       setOperationAction(ISD::ATOMIC_CMP_SWAP,  MVT::i64, Custom);
985 
986     // On v8, we have particularly efficient implementations of atomic fences
987     // if they can be combined with nearby atomic loads and stores.
988     if (!Subtarget->hasAcquireRelease() ||
989         getTargetMachine().getOptLevel() == 0) {
990       // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
991       InsertFencesForAtomic = true;
992     }
993   } else {
994     // If there's anything we can use as a barrier, go through custom lowering
995     // for ATOMIC_FENCE.
996     // If target has DMB in thumb, Fences can be inserted.
997     if (Subtarget->hasDataBarrier())
998       InsertFencesForAtomic = true;
999 
1000     setOperationAction(ISD::ATOMIC_FENCE,   MVT::Other,
1001                        Subtarget->hasAnyDataBarrier() ? Custom : Expand);
1002 
1003     // Set them all for expansion, which will force libcalls.
1004     setOperationAction(ISD::ATOMIC_CMP_SWAP,  MVT::i32, Expand);
1005     setOperationAction(ISD::ATOMIC_SWAP,      MVT::i32, Expand);
1006     setOperationAction(ISD::ATOMIC_LOAD_ADD,  MVT::i32, Expand);
1007     setOperationAction(ISD::ATOMIC_LOAD_SUB,  MVT::i32, Expand);
1008     setOperationAction(ISD::ATOMIC_LOAD_AND,  MVT::i32, Expand);
1009     setOperationAction(ISD::ATOMIC_LOAD_OR,   MVT::i32, Expand);
1010     setOperationAction(ISD::ATOMIC_LOAD_XOR,  MVT::i32, Expand);
1011     setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
1012     setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
1013     setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
1014     setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
1015     setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
1016     // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
1017     // Unordered/Monotonic case.
1018     if (!InsertFencesForAtomic) {
1019       setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1020       setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1021     }
1022   }
1023 
1024   setOperationAction(ISD::PREFETCH,         MVT::Other, Custom);
1025 
1026   // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
1027   if (!Subtarget->hasV6Ops()) {
1028     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1029     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8,  Expand);
1030   }
1031   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1032 
1033   if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
1034       !Subtarget->isThumb1Only()) {
1035     // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
1036     // iff target supports vfp2.
1037     setOperationAction(ISD::BITCAST, MVT::i64, Custom);
1038     setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
1039   }
1040 
1041   // We want to custom lower some of our intrinsics.
1042   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1043   setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
1044   setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
1045   setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
1046   if (Subtarget->useSjLjEH())
1047     setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
1048 
1049   setOperationAction(ISD::SETCC,     MVT::i32, Expand);
1050   setOperationAction(ISD::SETCC,     MVT::f32, Expand);
1051   setOperationAction(ISD::SETCC,     MVT::f64, Expand);
1052   setOperationAction(ISD::SELECT,    MVT::i32, Custom);
1053   setOperationAction(ISD::SELECT,    MVT::f32, Custom);
1054   setOperationAction(ISD::SELECT,    MVT::f64, Custom);
1055   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1056   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1057   setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
1058   if (Subtarget->hasFullFP16()) {
1059     setOperationAction(ISD::SETCC,     MVT::f16, Expand);
1060     setOperationAction(ISD::SELECT,    MVT::f16, Custom);
1061     setOperationAction(ISD::SELECT_CC, MVT::f16, Custom);
1062   }
1063 
1064   setOperationAction(ISD::SETCCCARRY, MVT::i32, Custom);
1065 
1066   setOperationAction(ISD::BRCOND,    MVT::Other, Custom);
1067   setOperationAction(ISD::BR_CC,     MVT::i32,   Custom);
1068   if (Subtarget->hasFullFP16())
1069       setOperationAction(ISD::BR_CC, MVT::f16,   Custom);
1070   setOperationAction(ISD::BR_CC,     MVT::f32,   Custom);
1071   setOperationAction(ISD::BR_CC,     MVT::f64,   Custom);
1072   setOperationAction(ISD::BR_JT,     MVT::Other, Custom);
1073 
1074   // We don't support sin/cos/fmod/copysign/pow
1075   setOperationAction(ISD::FSIN,      MVT::f64, Expand);
1076   setOperationAction(ISD::FSIN,      MVT::f32, Expand);
1077   setOperationAction(ISD::FCOS,      MVT::f32, Expand);
1078   setOperationAction(ISD::FCOS,      MVT::f64, Expand);
1079   setOperationAction(ISD::FSINCOS,   MVT::f64, Expand);
1080   setOperationAction(ISD::FSINCOS,   MVT::f32, Expand);
1081   setOperationAction(ISD::FREM,      MVT::f64, Expand);
1082   setOperationAction(ISD::FREM,      MVT::f32, Expand);
1083   if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
1084       !Subtarget->isThumb1Only()) {
1085     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
1086     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
1087   }
1088   setOperationAction(ISD::FPOW,      MVT::f64, Expand);
1089   setOperationAction(ISD::FPOW,      MVT::f32, Expand);
1090 
1091   if (!Subtarget->hasVFP4()) {
1092     setOperationAction(ISD::FMA, MVT::f64, Expand);
1093     setOperationAction(ISD::FMA, MVT::f32, Expand);
1094   }
1095 
1096   // Various VFP goodness
1097   if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
1098     // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
1099     if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
1100       setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
1101       setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
1102     }
1103 
1104     // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
1105     if (!Subtarget->hasFP16()) {
1106       setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
1107       setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
1108     }
1109   }
1110 
1111   // Use __sincos_stret if available.
1112   if (getLibcallName(RTLIB::SINCOS_STRET_F32) != nullptr &&
1113       getLibcallName(RTLIB::SINCOS_STRET_F64) != nullptr) {
1114     setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1115     setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1116   }
1117 
1118   // FP-ARMv8 implements a lot of rounding-like FP operations.
1119   if (Subtarget->hasFPARMv8()) {
1120     setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
1121     setOperationAction(ISD::FCEIL, MVT::f32, Legal);
1122     setOperationAction(ISD::FROUND, MVT::f32, Legal);
1123     setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
1124     setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
1125     setOperationAction(ISD::FRINT, MVT::f32, Legal);
1126     setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
1127     setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
1128     setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
1129     setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
1130     setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
1131     setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
1132 
1133     if (!Subtarget->isFPOnlySP()) {
1134       setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
1135       setOperationAction(ISD::FCEIL, MVT::f64, Legal);
1136       setOperationAction(ISD::FROUND, MVT::f64, Legal);
1137       setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1138       setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1139       setOperationAction(ISD::FRINT, MVT::f64, Legal);
1140       setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
1141       setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
1142     }
1143   }
1144 
1145   if (Subtarget->hasNEON()) {
1146     // vmin and vmax aren't available in a scalar form, so we use
1147     // a NEON instruction with an undef lane instead.
1148     setOperationAction(ISD::FMINIMUM, MVT::f16, Legal);
1149     setOperationAction(ISD::FMAXIMUM, MVT::f16, Legal);
1150     setOperationAction(ISD::FMINIMUM, MVT::f32, Legal);
1151     setOperationAction(ISD::FMAXIMUM, MVT::f32, Legal);
1152     setOperationAction(ISD::FMINIMUM, MVT::v2f32, Legal);
1153     setOperationAction(ISD::FMAXIMUM, MVT::v2f32, Legal);
1154     setOperationAction(ISD::FMINIMUM, MVT::v4f32, Legal);
1155     setOperationAction(ISD::FMAXIMUM, MVT::v4f32, Legal);
1156 
1157     if (Subtarget->hasFullFP16()) {
1158       setOperationAction(ISD::FMINNUM, MVT::v4f16, Legal);
1159       setOperationAction(ISD::FMAXNUM, MVT::v4f16, Legal);
1160       setOperationAction(ISD::FMINNUM, MVT::v8f16, Legal);
1161       setOperationAction(ISD::FMAXNUM, MVT::v8f16, Legal);
1162 
1163       setOperationAction(ISD::FMINIMUM, MVT::v4f16, Legal);
1164       setOperationAction(ISD::FMAXIMUM, MVT::v4f16, Legal);
1165       setOperationAction(ISD::FMINIMUM, MVT::v8f16, Legal);
1166       setOperationAction(ISD::FMAXIMUM, MVT::v8f16, Legal);
1167     }
1168   }
1169 
1170   // We have target-specific dag combine patterns for the following nodes:
1171   // ARMISD::VMOVRRD  - No need to call setTargetDAGCombine
1172   setTargetDAGCombine(ISD::ADD);
1173   setTargetDAGCombine(ISD::SUB);
1174   setTargetDAGCombine(ISD::MUL);
1175   setTargetDAGCombine(ISD::AND);
1176   setTargetDAGCombine(ISD::OR);
1177   setTargetDAGCombine(ISD::XOR);
1178 
1179   if (Subtarget->hasV6Ops())
1180     setTargetDAGCombine(ISD::SRL);
1181   if (Subtarget->isThumb1Only())
1182     setTargetDAGCombine(ISD::SHL);
1183 
1184   setStackPointerRegisterToSaveRestore(ARM::SP);
1185 
1186   if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
1187       !Subtarget->hasVFP2())
1188     setSchedulingPreference(Sched::RegPressure);
1189   else
1190     setSchedulingPreference(Sched::Hybrid);
1191 
1192   //// temporary - rewrite interface to use type
1193   MaxStoresPerMemset = 8;
1194   MaxStoresPerMemsetOptSize = 4;
1195   MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
1196   MaxStoresPerMemcpyOptSize = 2;
1197   MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
1198   MaxStoresPerMemmoveOptSize = 2;
1199 
1200   // On ARM arguments smaller than 4 bytes are extended, so all arguments
1201   // are at least 4 bytes aligned.
1202   setMinStackArgumentAlignment(4);
1203 
1204   // Prefer likely predicted branches to selects on out-of-order cores.
1205   PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder();
1206 
1207   setPrefLoopAlignment(Subtarget->getPrefLoopAlignment());
1208 
1209   setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
1210 
1211   if (Subtarget->isThumb() || Subtarget->isThumb2())
1212     setTargetDAGCombine(ISD::ABS);
1213 }
1214 
1215 bool ARMTargetLowering::useSoftFloat() const {
1216   return Subtarget->useSoftFloat();
1217 }
1218 
1219 // FIXME: It might make sense to define the representative register class as the
1220 // nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1221 // a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1222 // SPR's representative would be DPR_VFP2. This should work well if register
1223 // pressure tracking were modified such that a register use would increment the
1224 // pressure of the register class's representative and all of it's super
1225 // classes' representatives transitively. We have not implemented this because
1226 // of the difficulty prior to coalescing of modeling operand register classes
1227 // due to the common occurrence of cross class copies and subregister insertions
1228 // and extractions.
1229 std::pair<const TargetRegisterClass *, uint8_t>
1230 ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1231                                            MVT VT) const {
1232   const TargetRegisterClass *RRC = nullptr;
1233   uint8_t Cost = 1;
1234   switch (VT.SimpleTy) {
1235   default:
1236     return TargetLowering::findRepresentativeClass(TRI, VT);
1237   // Use DPR as representative register class for all floating point
1238   // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1239   // the cost is 1 for both f32 and f64.
1240   case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
1241   case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
1242     RRC = &ARM::DPRRegClass;
1243     // When NEON is used for SP, only half of the register file is available
1244     // because operations that define both SP and DP results will be constrained
1245     // to the VFP2 class (D0-D15). We currently model this constraint prior to
1246     // coalescing by double-counting the SP regs. See the FIXME above.
1247     if (Subtarget->useNEONForSinglePrecisionFP())
1248       Cost = 2;
1249     break;
1250   case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1251   case MVT::v4f32: case MVT::v2f64:
1252     RRC = &ARM::DPRRegClass;
1253     Cost = 2;
1254     break;
1255   case MVT::v4i64:
1256     RRC = &ARM::DPRRegClass;
1257     Cost = 4;
1258     break;
1259   case MVT::v8i64:
1260     RRC = &ARM::DPRRegClass;
1261     Cost = 8;
1262     break;
1263   }
1264   return std::make_pair(RRC, Cost);
1265 }
1266 
1267 const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
1268   switch ((ARMISD::NodeType)Opcode) {
1269   case ARMISD::FIRST_NUMBER:  break;
1270   case ARMISD::Wrapper:       return "ARMISD::Wrapper";
1271   case ARMISD::WrapperPIC:    return "ARMISD::WrapperPIC";
1272   case ARMISD::WrapperJT:     return "ARMISD::WrapperJT";
1273   case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
1274   case ARMISD::CALL:          return "ARMISD::CALL";
1275   case ARMISD::CALL_PRED:     return "ARMISD::CALL_PRED";
1276   case ARMISD::CALL_NOLINK:   return "ARMISD::CALL_NOLINK";
1277   case ARMISD::BRCOND:        return "ARMISD::BRCOND";
1278   case ARMISD::BR_JT:         return "ARMISD::BR_JT";
1279   case ARMISD::BR2_JT:        return "ARMISD::BR2_JT";
1280   case ARMISD::RET_FLAG:      return "ARMISD::RET_FLAG";
1281   case ARMISD::INTRET_FLAG:   return "ARMISD::INTRET_FLAG";
1282   case ARMISD::PIC_ADD:       return "ARMISD::PIC_ADD";
1283   case ARMISD::CMP:           return "ARMISD::CMP";
1284   case ARMISD::CMN:           return "ARMISD::CMN";
1285   case ARMISD::CMPZ:          return "ARMISD::CMPZ";
1286   case ARMISD::CMPFP:         return "ARMISD::CMPFP";
1287   case ARMISD::CMPFPw0:       return "ARMISD::CMPFPw0";
1288   case ARMISD::BCC_i64:       return "ARMISD::BCC_i64";
1289   case ARMISD::FMSTAT:        return "ARMISD::FMSTAT";
1290 
1291   case ARMISD::CMOV:          return "ARMISD::CMOV";
1292   case ARMISD::SUBS:          return "ARMISD::SUBS";
1293 
1294   case ARMISD::SSAT:          return "ARMISD::SSAT";
1295   case ARMISD::USAT:          return "ARMISD::USAT";
1296 
1297   case ARMISD::SRL_FLAG:      return "ARMISD::SRL_FLAG";
1298   case ARMISD::SRA_FLAG:      return "ARMISD::SRA_FLAG";
1299   case ARMISD::RRX:           return "ARMISD::RRX";
1300 
1301   case ARMISD::ADDC:          return "ARMISD::ADDC";
1302   case ARMISD::ADDE:          return "ARMISD::ADDE";
1303   case ARMISD::SUBC:          return "ARMISD::SUBC";
1304   case ARMISD::SUBE:          return "ARMISD::SUBE";
1305 
1306   case ARMISD::VMOVRRD:       return "ARMISD::VMOVRRD";
1307   case ARMISD::VMOVDRR:       return "ARMISD::VMOVDRR";
1308   case ARMISD::VMOVhr:        return "ARMISD::VMOVhr";
1309   case ARMISD::VMOVrh:        return "ARMISD::VMOVrh";
1310   case ARMISD::VMOVSR:        return "ARMISD::VMOVSR";
1311 
1312   case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1313   case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1314   case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
1315 
1316   case ARMISD::TC_RETURN:     return "ARMISD::TC_RETURN";
1317 
1318   case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
1319 
1320   case ARMISD::DYN_ALLOC:     return "ARMISD::DYN_ALLOC";
1321 
1322   case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
1323 
1324   case ARMISD::PRELOAD:       return "ARMISD::PRELOAD";
1325 
1326   case ARMISD::WIN__CHKSTK:   return "ARMISD::WIN__CHKSTK";
1327   case ARMISD::WIN__DBZCHK:   return "ARMISD::WIN__DBZCHK";
1328 
1329   case ARMISD::VCEQ:          return "ARMISD::VCEQ";
1330   case ARMISD::VCEQZ:         return "ARMISD::VCEQZ";
1331   case ARMISD::VCGE:          return "ARMISD::VCGE";
1332   case ARMISD::VCGEZ:         return "ARMISD::VCGEZ";
1333   case ARMISD::VCLEZ:         return "ARMISD::VCLEZ";
1334   case ARMISD::VCGEU:         return "ARMISD::VCGEU";
1335   case ARMISD::VCGT:          return "ARMISD::VCGT";
1336   case ARMISD::VCGTZ:         return "ARMISD::VCGTZ";
1337   case ARMISD::VCLTZ:         return "ARMISD::VCLTZ";
1338   case ARMISD::VCGTU:         return "ARMISD::VCGTU";
1339   case ARMISD::VTST:          return "ARMISD::VTST";
1340 
1341   case ARMISD::VSHL:          return "ARMISD::VSHL";
1342   case ARMISD::VSHRs:         return "ARMISD::VSHRs";
1343   case ARMISD::VSHRu:         return "ARMISD::VSHRu";
1344   case ARMISD::VRSHRs:        return "ARMISD::VRSHRs";
1345   case ARMISD::VRSHRu:        return "ARMISD::VRSHRu";
1346   case ARMISD::VRSHRN:        return "ARMISD::VRSHRN";
1347   case ARMISD::VQSHLs:        return "ARMISD::VQSHLs";
1348   case ARMISD::VQSHLu:        return "ARMISD::VQSHLu";
1349   case ARMISD::VQSHLsu:       return "ARMISD::VQSHLsu";
1350   case ARMISD::VQSHRNs:       return "ARMISD::VQSHRNs";
1351   case ARMISD::VQSHRNu:       return "ARMISD::VQSHRNu";
1352   case ARMISD::VQSHRNsu:      return "ARMISD::VQSHRNsu";
1353   case ARMISD::VQRSHRNs:      return "ARMISD::VQRSHRNs";
1354   case ARMISD::VQRSHRNu:      return "ARMISD::VQRSHRNu";
1355   case ARMISD::VQRSHRNsu:     return "ARMISD::VQRSHRNsu";
1356   case ARMISD::VSLI:          return "ARMISD::VSLI";
1357   case ARMISD::VSRI:          return "ARMISD::VSRI";
1358   case ARMISD::VGETLANEu:     return "ARMISD::VGETLANEu";
1359   case ARMISD::VGETLANEs:     return "ARMISD::VGETLANEs";
1360   case ARMISD::VMOVIMM:       return "ARMISD::VMOVIMM";
1361   case ARMISD::VMVNIMM:       return "ARMISD::VMVNIMM";
1362   case ARMISD::VMOVFPIMM:     return "ARMISD::VMOVFPIMM";
1363   case ARMISD::VDUP:          return "ARMISD::VDUP";
1364   case ARMISD::VDUPLANE:      return "ARMISD::VDUPLANE";
1365   case ARMISD::VEXT:          return "ARMISD::VEXT";
1366   case ARMISD::VREV64:        return "ARMISD::VREV64";
1367   case ARMISD::VREV32:        return "ARMISD::VREV32";
1368   case ARMISD::VREV16:        return "ARMISD::VREV16";
1369   case ARMISD::VZIP:          return "ARMISD::VZIP";
1370   case ARMISD::VUZP:          return "ARMISD::VUZP";
1371   case ARMISD::VTRN:          return "ARMISD::VTRN";
1372   case ARMISD::VTBL1:         return "ARMISD::VTBL1";
1373   case ARMISD::VTBL2:         return "ARMISD::VTBL2";
1374   case ARMISD::VMULLs:        return "ARMISD::VMULLs";
1375   case ARMISD::VMULLu:        return "ARMISD::VMULLu";
1376   case ARMISD::UMAAL:         return "ARMISD::UMAAL";
1377   case ARMISD::UMLAL:         return "ARMISD::UMLAL";
1378   case ARMISD::SMLAL:         return "ARMISD::SMLAL";
1379   case ARMISD::SMLALBB:       return "ARMISD::SMLALBB";
1380   case ARMISD::SMLALBT:       return "ARMISD::SMLALBT";
1381   case ARMISD::SMLALTB:       return "ARMISD::SMLALTB";
1382   case ARMISD::SMLALTT:       return "ARMISD::SMLALTT";
1383   case ARMISD::SMULWB:        return "ARMISD::SMULWB";
1384   case ARMISD::SMULWT:        return "ARMISD::SMULWT";
1385   case ARMISD::SMLALD:        return "ARMISD::SMLALD";
1386   case ARMISD::SMLALDX:       return "ARMISD::SMLALDX";
1387   case ARMISD::SMLSLD:        return "ARMISD::SMLSLD";
1388   case ARMISD::SMLSLDX:       return "ARMISD::SMLSLDX";
1389   case ARMISD::SMMLAR:        return "ARMISD::SMMLAR";
1390   case ARMISD::SMMLSR:        return "ARMISD::SMMLSR";
1391   case ARMISD::BUILD_VECTOR:  return "ARMISD::BUILD_VECTOR";
1392   case ARMISD::BFI:           return "ARMISD::BFI";
1393   case ARMISD::VORRIMM:       return "ARMISD::VORRIMM";
1394   case ARMISD::VBICIMM:       return "ARMISD::VBICIMM";
1395   case ARMISD::VBSL:          return "ARMISD::VBSL";
1396   case ARMISD::MEMCPY:        return "ARMISD::MEMCPY";
1397   case ARMISD::VLD1DUP:       return "ARMISD::VLD1DUP";
1398   case ARMISD::VLD2DUP:       return "ARMISD::VLD2DUP";
1399   case ARMISD::VLD3DUP:       return "ARMISD::VLD3DUP";
1400   case ARMISD::VLD4DUP:       return "ARMISD::VLD4DUP";
1401   case ARMISD::VLD1_UPD:      return "ARMISD::VLD1_UPD";
1402   case ARMISD::VLD2_UPD:      return "ARMISD::VLD2_UPD";
1403   case ARMISD::VLD3_UPD:      return "ARMISD::VLD3_UPD";
1404   case ARMISD::VLD4_UPD:      return "ARMISD::VLD4_UPD";
1405   case ARMISD::VLD2LN_UPD:    return "ARMISD::VLD2LN_UPD";
1406   case ARMISD::VLD3LN_UPD:    return "ARMISD::VLD3LN_UPD";
1407   case ARMISD::VLD4LN_UPD:    return "ARMISD::VLD4LN_UPD";
1408   case ARMISD::VLD1DUP_UPD:   return "ARMISD::VLD1DUP_UPD";
1409   case ARMISD::VLD2DUP_UPD:   return "ARMISD::VLD2DUP_UPD";
1410   case ARMISD::VLD3DUP_UPD:   return "ARMISD::VLD3DUP_UPD";
1411   case ARMISD::VLD4DUP_UPD:   return "ARMISD::VLD4DUP_UPD";
1412   case ARMISD::VST1_UPD:      return "ARMISD::VST1_UPD";
1413   case ARMISD::VST2_UPD:      return "ARMISD::VST2_UPD";
1414   case ARMISD::VST3_UPD:      return "ARMISD::VST3_UPD";
1415   case ARMISD::VST4_UPD:      return "ARMISD::VST4_UPD";
1416   case ARMISD::VST2LN_UPD:    return "ARMISD::VST2LN_UPD";
1417   case ARMISD::VST3LN_UPD:    return "ARMISD::VST3LN_UPD";
1418   case ARMISD::VST4LN_UPD:    return "ARMISD::VST4LN_UPD";
1419   }
1420   return nullptr;
1421 }
1422 
1423 EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1424                                           EVT VT) const {
1425   if (!VT.isVector())
1426     return getPointerTy(DL);
1427   return VT.changeVectorElementTypeToInteger();
1428 }
1429 
1430 /// getRegClassFor - Return the register class that should be used for the
1431 /// specified value type.
1432 const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
1433   // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1434   // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1435   // load / store 4 to 8 consecutive D registers.
1436   if (Subtarget->hasNEON()) {
1437     if (VT == MVT::v4i64)
1438       return &ARM::QQPRRegClass;
1439     if (VT == MVT::v8i64)
1440       return &ARM::QQQQPRRegClass;
1441   }
1442   return TargetLowering::getRegClassFor(VT);
1443 }
1444 
1445 // memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1446 // source/dest is aligned and the copy size is large enough. We therefore want
1447 // to align such objects passed to memory intrinsics.
1448 bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1449                                                unsigned &PrefAlign) const {
1450   if (!isa<MemIntrinsic>(CI))
1451     return false;
1452   MinSize = 8;
1453   // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1454   // cycle faster than 4-byte aligned LDM.
1455   PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1456   return true;
1457 }
1458 
1459 // Create a fast isel object.
1460 FastISel *
1461 ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1462                                   const TargetLibraryInfo *libInfo) const {
1463   return ARM::createFastISel(funcInfo, libInfo);
1464 }
1465 
1466 Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
1467   unsigned NumVals = N->getNumValues();
1468   if (!NumVals)
1469     return Sched::RegPressure;
1470 
1471   for (unsigned i = 0; i != NumVals; ++i) {
1472     EVT VT = N->getValueType(i);
1473     if (VT == MVT::Glue || VT == MVT::Other)
1474       continue;
1475     if (VT.isFloatingPoint() || VT.isVector())
1476       return Sched::ILP;
1477   }
1478 
1479   if (!N->isMachineOpcode())
1480     return Sched::RegPressure;
1481 
1482   // Load are scheduled for latency even if there instruction itinerary
1483   // is not available.
1484   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
1485   const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
1486 
1487   if (MCID.getNumDefs() == 0)
1488     return Sched::RegPressure;
1489   if (!Itins->isEmpty() &&
1490       Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
1491     return Sched::ILP;
1492 
1493   return Sched::RegPressure;
1494 }
1495 
1496 //===----------------------------------------------------------------------===//
1497 // Lowering Code
1498 //===----------------------------------------------------------------------===//
1499 
1500 static bool isSRL16(const SDValue &Op) {
1501   if (Op.getOpcode() != ISD::SRL)
1502     return false;
1503   if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
1504     return Const->getZExtValue() == 16;
1505   return false;
1506 }
1507 
1508 static bool isSRA16(const SDValue &Op) {
1509   if (Op.getOpcode() != ISD::SRA)
1510     return false;
1511   if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
1512     return Const->getZExtValue() == 16;
1513   return false;
1514 }
1515 
1516 static bool isSHL16(const SDValue &Op) {
1517   if (Op.getOpcode() != ISD::SHL)
1518     return false;
1519   if (auto Const = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
1520     return Const->getZExtValue() == 16;
1521   return false;
1522 }
1523 
1524 // Check for a signed 16-bit value. We special case SRA because it makes it
1525 // more simple when also looking for SRAs that aren't sign extending a
1526 // smaller value. Without the check, we'd need to take extra care with
1527 // checking order for some operations.
1528 static bool isS16(const SDValue &Op, SelectionDAG &DAG) {
1529   if (isSRA16(Op))
1530     return isSHL16(Op.getOperand(0));
1531   return DAG.ComputeNumSignBits(Op) == 17;
1532 }
1533 
1534 /// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1535 static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1536   switch (CC) {
1537   default: llvm_unreachable("Unknown condition code!");
1538   case ISD::SETNE:  return ARMCC::NE;
1539   case ISD::SETEQ:  return ARMCC::EQ;
1540   case ISD::SETGT:  return ARMCC::GT;
1541   case ISD::SETGE:  return ARMCC::GE;
1542   case ISD::SETLT:  return ARMCC::LT;
1543   case ISD::SETLE:  return ARMCC::LE;
1544   case ISD::SETUGT: return ARMCC::HI;
1545   case ISD::SETUGE: return ARMCC::HS;
1546   case ISD::SETULT: return ARMCC::LO;
1547   case ISD::SETULE: return ARMCC::LS;
1548   }
1549 }
1550 
1551 /// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1552 static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
1553                         ARMCC::CondCodes &CondCode2, bool &InvalidOnQNaN) {
1554   CondCode2 = ARMCC::AL;
1555   InvalidOnQNaN = true;
1556   switch (CC) {
1557   default: llvm_unreachable("Unknown FP condition!");
1558   case ISD::SETEQ:
1559   case ISD::SETOEQ:
1560     CondCode = ARMCC::EQ;
1561     InvalidOnQNaN = false;
1562     break;
1563   case ISD::SETGT:
1564   case ISD::SETOGT: CondCode = ARMCC::GT; break;
1565   case ISD::SETGE:
1566   case ISD::SETOGE: CondCode = ARMCC::GE; break;
1567   case ISD::SETOLT: CondCode = ARMCC::MI; break;
1568   case ISD::SETOLE: CondCode = ARMCC::LS; break;
1569   case ISD::SETONE:
1570     CondCode = ARMCC::MI;
1571     CondCode2 = ARMCC::GT;
1572     InvalidOnQNaN = false;
1573     break;
1574   case ISD::SETO:   CondCode = ARMCC::VC; break;
1575   case ISD::SETUO:  CondCode = ARMCC::VS; break;
1576   case ISD::SETUEQ:
1577     CondCode = ARMCC::EQ;
1578     CondCode2 = ARMCC::VS;
1579     InvalidOnQNaN = false;
1580     break;
1581   case ISD::SETUGT: CondCode = ARMCC::HI; break;
1582   case ISD::SETUGE: CondCode = ARMCC::PL; break;
1583   case ISD::SETLT:
1584   case ISD::SETULT: CondCode = ARMCC::LT; break;
1585   case ISD::SETLE:
1586   case ISD::SETULE: CondCode = ARMCC::LE; break;
1587   case ISD::SETNE:
1588   case ISD::SETUNE:
1589     CondCode = ARMCC::NE;
1590     InvalidOnQNaN = false;
1591     break;
1592   }
1593 }
1594 
1595 //===----------------------------------------------------------------------===//
1596 //                      Calling Convention Implementation
1597 //===----------------------------------------------------------------------===//
1598 
1599 /// getEffectiveCallingConv - Get the effective calling convention, taking into
1600 /// account presence of floating point hardware and calling convention
1601 /// limitations, such as support for variadic functions.
1602 CallingConv::ID
1603 ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1604                                            bool isVarArg) const {
1605   switch (CC) {
1606   default:
1607     report_fatal_error("Unsupported calling convention");
1608   case CallingConv::ARM_AAPCS:
1609   case CallingConv::ARM_APCS:
1610   case CallingConv::GHC:
1611     return CC;
1612   case CallingConv::PreserveMost:
1613     return CallingConv::PreserveMost;
1614   case CallingConv::ARM_AAPCS_VFP:
1615   case CallingConv::Swift:
1616     return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1617   case CallingConv::C:
1618     if (!Subtarget->isAAPCS_ABI())
1619       return CallingConv::ARM_APCS;
1620     else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
1621              getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1622              !isVarArg)
1623       return CallingConv::ARM_AAPCS_VFP;
1624     else
1625       return CallingConv::ARM_AAPCS;
1626   case CallingConv::Fast:
1627   case CallingConv::CXX_FAST_TLS:
1628     if (!Subtarget->isAAPCS_ABI()) {
1629       if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
1630         return CallingConv::Fast;
1631       return CallingConv::ARM_APCS;
1632     } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
1633       return CallingConv::ARM_AAPCS_VFP;
1634     else
1635       return CallingConv::ARM_AAPCS;
1636   }
1637 }
1638 
1639 CCAssignFn *ARMTargetLowering::CCAssignFnForCall(CallingConv::ID CC,
1640                                                  bool isVarArg) const {
1641   return CCAssignFnForNode(CC, false, isVarArg);
1642 }
1643 
1644 CCAssignFn *ARMTargetLowering::CCAssignFnForReturn(CallingConv::ID CC,
1645                                                    bool isVarArg) const {
1646   return CCAssignFnForNode(CC, true, isVarArg);
1647 }
1648 
1649 /// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1650 /// CallingConvention.
1651 CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1652                                                  bool Return,
1653                                                  bool isVarArg) const {
1654   switch (getEffectiveCallingConv(CC, isVarArg)) {
1655   default:
1656     report_fatal_error("Unsupported calling convention");
1657   case CallingConv::ARM_APCS:
1658     return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
1659   case CallingConv::ARM_AAPCS:
1660     return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1661   case CallingConv::ARM_AAPCS_VFP:
1662     return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1663   case CallingConv::Fast:
1664     return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
1665   case CallingConv::GHC:
1666     return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
1667   case CallingConv::PreserveMost:
1668     return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1669   }
1670 }
1671 
1672 /// LowerCallResult - Lower the result values of a call into the
1673 /// appropriate copies out of appropriate physical registers.
1674 SDValue ARMTargetLowering::LowerCallResult(
1675     SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
1676     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1677     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool isThisReturn,
1678     SDValue ThisVal) const {
1679   // Assign locations to each value returned by this call.
1680   SmallVector<CCValAssign, 16> RVLocs;
1681   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1682                  *DAG.getContext());
1683   CCInfo.AnalyzeCallResult(Ins, CCAssignFnForReturn(CallConv, isVarArg));
1684 
1685   // Copy all of the result registers out of their specified physreg.
1686   for (unsigned i = 0; i != RVLocs.size(); ++i) {
1687     CCValAssign VA = RVLocs[i];
1688 
1689     // Pass 'this' value directly from the argument to return value, to avoid
1690     // reg unit interference
1691     if (i == 0 && isThisReturn) {
1692       assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1693              "unexpected return calling convention register assignment");
1694       InVals.push_back(ThisVal);
1695       continue;
1696     }
1697 
1698     SDValue Val;
1699     if (VA.needsCustom()) {
1700       // Handle f64 or half of a v2f64.
1701       SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
1702                                       InFlag);
1703       Chain = Lo.getValue(1);
1704       InFlag = Lo.getValue(2);
1705       VA = RVLocs[++i]; // skip ahead to next loc
1706       SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
1707                                       InFlag);
1708       Chain = Hi.getValue(1);
1709       InFlag = Hi.getValue(2);
1710       if (!Subtarget->isLittle())
1711         std::swap (Lo, Hi);
1712       Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
1713 
1714       if (VA.getLocVT() == MVT::v2f64) {
1715         SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1716         Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1717                           DAG.getConstant(0, dl, MVT::i32));
1718 
1719         VA = RVLocs[++i]; // skip ahead to next loc
1720         Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
1721         Chain = Lo.getValue(1);
1722         InFlag = Lo.getValue(2);
1723         VA = RVLocs[++i]; // skip ahead to next loc
1724         Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
1725         Chain = Hi.getValue(1);
1726         InFlag = Hi.getValue(2);
1727         if (!Subtarget->isLittle())
1728           std::swap (Lo, Hi);
1729         Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
1730         Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1731                           DAG.getConstant(1, dl, MVT::i32));
1732       }
1733     } else {
1734       Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1735                                InFlag);
1736       Chain = Val.getValue(1);
1737       InFlag = Val.getValue(2);
1738     }
1739 
1740     switch (VA.getLocInfo()) {
1741     default: llvm_unreachable("Unknown loc info!");
1742     case CCValAssign::Full: break;
1743     case CCValAssign::BCvt:
1744       Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
1745       break;
1746     }
1747 
1748     InVals.push_back(Val);
1749   }
1750 
1751   return Chain;
1752 }
1753 
1754 /// LowerMemOpCallTo - Store the argument to the stack.
1755 SDValue ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr,
1756                                             SDValue Arg, const SDLoc &dl,
1757                                             SelectionDAG &DAG,
1758                                             const CCValAssign &VA,
1759                                             ISD::ArgFlagsTy Flags) const {
1760   unsigned LocMemOffset = VA.getLocMemOffset();
1761   SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
1762   PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1763                        StackPtr, PtrOff);
1764   return DAG.getStore(
1765       Chain, dl, Arg, PtrOff,
1766       MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset));
1767 }
1768 
1769 void ARMTargetLowering::PassF64ArgInRegs(const SDLoc &dl, SelectionDAG &DAG,
1770                                          SDValue Chain, SDValue &Arg,
1771                                          RegsToPassVector &RegsToPass,
1772                                          CCValAssign &VA, CCValAssign &NextVA,
1773                                          SDValue &StackPtr,
1774                                          SmallVectorImpl<SDValue> &MemOpChains,
1775                                          ISD::ArgFlagsTy Flags) const {
1776   SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
1777                               DAG.getVTList(MVT::i32, MVT::i32), Arg);
1778   unsigned id = Subtarget->isLittle() ? 0 : 1;
1779   RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
1780 
1781   if (NextVA.isRegLoc())
1782     RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
1783   else {
1784     assert(NextVA.isMemLoc());
1785     if (!StackPtr.getNode())
1786       StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1787                                     getPointerTy(DAG.getDataLayout()));
1788 
1789     MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
1790                                            dl, DAG, NextVA,
1791                                            Flags));
1792   }
1793 }
1794 
1795 /// LowerCall - Lowering a call into a callseq_start <-
1796 /// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1797 /// nodes.
1798 SDValue
1799 ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
1800                              SmallVectorImpl<SDValue> &InVals) const {
1801   SelectionDAG &DAG                     = CLI.DAG;
1802   SDLoc &dl                             = CLI.DL;
1803   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1804   SmallVectorImpl<SDValue> &OutVals     = CLI.OutVals;
1805   SmallVectorImpl<ISD::InputArg> &Ins   = CLI.Ins;
1806   SDValue Chain                         = CLI.Chain;
1807   SDValue Callee                        = CLI.Callee;
1808   bool &isTailCall                      = CLI.IsTailCall;
1809   CallingConv::ID CallConv              = CLI.CallConv;
1810   bool doesNotRet                       = CLI.DoesNotReturn;
1811   bool isVarArg                         = CLI.IsVarArg;
1812 
1813   MachineFunction &MF = DAG.getMachineFunction();
1814   bool isStructRet    = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1815   bool isThisReturn   = false;
1816   bool isSibCall      = false;
1817   auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls");
1818 
1819   // Disable tail calls if they're not supported.
1820   if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
1821     isTailCall = false;
1822 
1823   if (isTailCall) {
1824     // Check if it's really possible to do a tail call.
1825     isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1826                     isVarArg, isStructRet, MF.getFunction().hasStructRetAttr(),
1827                                                    Outs, OutVals, Ins, DAG);
1828     if (!isTailCall && CLI.CS && CLI.CS.isMustTailCall())
1829       report_fatal_error("failed to perform tail call elimination on a call "
1830                          "site marked musttail");
1831     // We don't support GuaranteedTailCallOpt for ARM, only automatically
1832     // detected sibcalls.
1833     if (isTailCall) {
1834       ++NumTailCalls;
1835       isSibCall = true;
1836     }
1837   }
1838 
1839   // Analyze operands of the call, assigning locations to each operand.
1840   SmallVector<CCValAssign, 16> ArgLocs;
1841   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1842                  *DAG.getContext());
1843   CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CallConv, isVarArg));
1844 
1845   // Get a count of how many bytes are to be pushed on the stack.
1846   unsigned NumBytes = CCInfo.getNextStackOffset();
1847 
1848   // For tail calls, memory operands are available in our caller's stack.
1849   if (isSibCall)
1850     NumBytes = 0;
1851 
1852   // Adjust the stack pointer for the new arguments...
1853   // These operations are automatically eliminated by the prolog/epilog pass
1854   if (!isSibCall)
1855     Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
1856 
1857   SDValue StackPtr =
1858       DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
1859 
1860   RegsToPassVector RegsToPass;
1861   SmallVector<SDValue, 8> MemOpChains;
1862 
1863   // Walk the register/memloc assignments, inserting copies/loads.  In the case
1864   // of tail call optimization, arguments are handled later.
1865   for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1866        i != e;
1867        ++i, ++realArgIdx) {
1868     CCValAssign &VA = ArgLocs[i];
1869     SDValue Arg = OutVals[realArgIdx];
1870     ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
1871     bool isByVal = Flags.isByVal();
1872 
1873     // Promote the value if needed.
1874     switch (VA.getLocInfo()) {
1875     default: llvm_unreachable("Unknown loc info!");
1876     case CCValAssign::Full: break;
1877     case CCValAssign::SExt:
1878       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1879       break;
1880     case CCValAssign::ZExt:
1881       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1882       break;
1883     case CCValAssign::AExt:
1884       Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1885       break;
1886     case CCValAssign::BCvt:
1887       Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
1888       break;
1889     }
1890 
1891     // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
1892     if (VA.needsCustom()) {
1893       if (VA.getLocVT() == MVT::v2f64) {
1894         SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1895                                   DAG.getConstant(0, dl, MVT::i32));
1896         SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1897                                   DAG.getConstant(1, dl, MVT::i32));
1898 
1899         PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
1900                          VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1901 
1902         VA = ArgLocs[++i]; // skip ahead to next loc
1903         if (VA.isRegLoc()) {
1904           PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
1905                            VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1906         } else {
1907           assert(VA.isMemLoc());
1908 
1909           MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1910                                                  dl, DAG, VA, Flags));
1911         }
1912       } else {
1913         PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
1914                          StackPtr, MemOpChains, Flags);
1915       }
1916     } else if (VA.isRegLoc()) {
1917       if (realArgIdx == 0 && Flags.isReturned() && !Flags.isSwiftSelf() &&
1918           Outs[0].VT == MVT::i32) {
1919         assert(VA.getLocVT() == MVT::i32 &&
1920                "unexpected calling convention register assignment");
1921         assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
1922                "unexpected use of 'returned'");
1923         isThisReturn = true;
1924       }
1925       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1926     } else if (isByVal) {
1927       assert(VA.isMemLoc());
1928       unsigned offset = 0;
1929 
1930       // True if this byval aggregate will be split between registers
1931       // and memory.
1932       unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1933       unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
1934 
1935       if (CurByValIdx < ByValArgsCount) {
1936 
1937         unsigned RegBegin, RegEnd;
1938         CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1939 
1940         EVT PtrVT =
1941             DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
1942         unsigned int i, j;
1943         for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
1944           SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
1945           SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1946           SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1947                                      MachinePointerInfo(),
1948                                      DAG.InferPtrAlignment(AddArg));
1949           MemOpChains.push_back(Load.getValue(1));
1950           RegsToPass.push_back(std::make_pair(j, Load));
1951         }
1952 
1953         // If parameter size outsides register area, "offset" value
1954         // helps us to calculate stack slot for remained part properly.
1955         offset = RegEnd - RegBegin;
1956 
1957         CCInfo.nextInRegsParam();
1958       }
1959 
1960       if (Flags.getByValSize() > 4*offset) {
1961         auto PtrVT = getPointerTy(DAG.getDataLayout());
1962         unsigned LocMemOffset = VA.getLocMemOffset();
1963         SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
1964         SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
1965         SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
1966         SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
1967         SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
1968                                            MVT::i32);
1969         SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1970                                             MVT::i32);
1971 
1972         SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
1973         SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
1974         MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
1975                                           Ops));
1976       }
1977     } else if (!isSibCall) {
1978       assert(VA.isMemLoc());
1979 
1980       MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1981                                              dl, DAG, VA, Flags));
1982     }
1983   }
1984 
1985   if (!MemOpChains.empty())
1986     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
1987 
1988   // Build a sequence of copy-to-reg nodes chained together with token chain
1989   // and flag operands which copy the outgoing args into the appropriate regs.
1990   SDValue InFlag;
1991   // Tail call byval lowering might overwrite argument registers so in case of
1992   // tail call optimization the copies to registers are lowered later.
1993   if (!isTailCall)
1994     for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1995       Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1996                                RegsToPass[i].second, InFlag);
1997       InFlag = Chain.getValue(1);
1998     }
1999 
2000   // For tail calls lower the arguments to the 'real' stack slot.
2001   if (isTailCall) {
2002     // Force all the incoming stack arguments to be loaded from the stack
2003     // before any new outgoing arguments are stored to the stack, because the
2004     // outgoing stack slots may alias the incoming argument stack slots, and
2005     // the alias isn't otherwise explicit. This is slightly more conservative
2006     // than necessary, because it means that each store effectively depends
2007     // on every argument instead of just those arguments it would clobber.
2008 
2009     // Do not flag preceding copytoreg stuff together with the following stuff.
2010     InFlag = SDValue();
2011     for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2012       Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2013                                RegsToPass[i].second, InFlag);
2014       InFlag = Chain.getValue(1);
2015     }
2016     InFlag = SDValue();
2017   }
2018 
2019   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2020   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2021   // node so that legalize doesn't hack it.
2022   bool isDirect = false;
2023 
2024   const TargetMachine &TM = getTargetMachine();
2025   const Module *Mod = MF.getFunction().getParent();
2026   const GlobalValue *GV = nullptr;
2027   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2028     GV = G->getGlobal();
2029   bool isStub =
2030       !TM.shouldAssumeDSOLocal(*Mod, GV) && Subtarget->isTargetMachO();
2031 
2032   bool isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
2033   bool isLocalARMFunc = false;
2034   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2035   auto PtrVt = getPointerTy(DAG.getDataLayout());
2036 
2037   if (Subtarget->genLongCalls()) {
2038     assert((!isPositionIndependent() || Subtarget->isTargetWindows()) &&
2039            "long-calls codegen is not position independent!");
2040     // Handle a global address or an external symbol. If it's not one of
2041     // those, the target's already in a register, so we don't need to do
2042     // anything extra.
2043     if (isa<GlobalAddressSDNode>(Callee)) {
2044       // Create a constant pool entry for the callee address
2045       unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2046       ARMConstantPoolValue *CPV =
2047         ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
2048 
2049       // Get the address of the callee into a register
2050       SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
2051       CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2052       Callee = DAG.getLoad(
2053           PtrVt, dl, DAG.getEntryNode(), CPAddr,
2054           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
2055     } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
2056       const char *Sym = S->getSymbol();
2057 
2058       // Create a constant pool entry for the callee address
2059       unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2060       ARMConstantPoolValue *CPV =
2061         ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
2062                                       ARMPCLabelIndex, 0);
2063       // Get the address of the callee into a register
2064       SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
2065       CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2066       Callee = DAG.getLoad(
2067           PtrVt, dl, DAG.getEntryNode(), CPAddr,
2068           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
2069     }
2070   } else if (isa<GlobalAddressSDNode>(Callee)) {
2071     // If we're optimizing for minimum size and the function is called three or
2072     // more times in this block, we can improve codesize by calling indirectly
2073     // as BLXr has a 16-bit encoding.
2074     auto *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
2075     auto *BB = CLI.CS.getParent();
2076     bool PreferIndirect =
2077         Subtarget->isThumb() && Subtarget->optForMinSize() &&
2078         count_if(GV->users(), [&BB](const User *U) {
2079           return isa<Instruction>(U) && cast<Instruction>(U)->getParent() == BB;
2080         }) > 2;
2081 
2082     if (!PreferIndirect) {
2083       isDirect = true;
2084       bool isDef = GV->isStrongDefinitionForLinker();
2085 
2086       // ARM call to a local ARM function is predicable.
2087       isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
2088       // tBX takes a register source operand.
2089       if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
2090         assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
2091         Callee = DAG.getNode(
2092             ARMISD::WrapperPIC, dl, PtrVt,
2093             DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
2094         Callee = DAG.getLoad(
2095             PtrVt, dl, DAG.getEntryNode(), Callee,
2096             MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2097             /* Alignment = */ 0, MachineMemOperand::MODereferenceable |
2098                                      MachineMemOperand::MOInvariant);
2099       } else if (Subtarget->isTargetCOFF()) {
2100         assert(Subtarget->isTargetWindows() &&
2101                "Windows is the only supported COFF target");
2102         unsigned TargetFlags = GV->hasDLLImportStorageClass()
2103                                    ? ARMII::MO_DLLIMPORT
2104                                    : ARMII::MO_NO_FLAG;
2105         Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0,
2106                                             TargetFlags);
2107         if (GV->hasDLLImportStorageClass())
2108           Callee =
2109               DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
2110                           DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
2111                           MachinePointerInfo::getGOT(DAG.getMachineFunction()));
2112       } else {
2113         Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, 0);
2114       }
2115     }
2116   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2117     isDirect = true;
2118     // tBX takes a register source operand.
2119     const char *Sym = S->getSymbol();
2120     if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
2121       unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2122       ARMConstantPoolValue *CPV =
2123         ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
2124                                       ARMPCLabelIndex, 4);
2125       SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
2126       CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2127       Callee = DAG.getLoad(
2128           PtrVt, dl, DAG.getEntryNode(), CPAddr,
2129           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
2130       SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
2131       Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
2132     } else {
2133       Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, 0);
2134     }
2135   }
2136 
2137   // FIXME: handle tail calls differently.
2138   unsigned CallOpc;
2139   if (Subtarget->isThumb()) {
2140     if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
2141       CallOpc = ARMISD::CALL_NOLINK;
2142     else
2143       CallOpc = ARMISD::CALL;
2144   } else {
2145     if (!isDirect && !Subtarget->hasV5TOps())
2146       CallOpc = ARMISD::CALL_NOLINK;
2147     else if (doesNotRet && isDirect && Subtarget->hasRetAddrStack() &&
2148              // Emit regular call when code size is the priority
2149              !Subtarget->optForMinSize())
2150       // "mov lr, pc; b _foo" to avoid confusing the RSP
2151       CallOpc = ARMISD::CALL_NOLINK;
2152     else
2153       CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
2154   }
2155 
2156   std::vector<SDValue> Ops;
2157   Ops.push_back(Chain);
2158   Ops.push_back(Callee);
2159 
2160   // Add argument registers to the end of the list so that they are known live
2161   // into the call.
2162   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2163     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2164                                   RegsToPass[i].second.getValueType()));
2165 
2166   // Add a register mask operand representing the call-preserved registers.
2167   if (!isTailCall) {
2168     const uint32_t *Mask;
2169     const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
2170     if (isThisReturn) {
2171       // For 'this' returns, use the R0-preserving mask if applicable
2172       Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
2173       if (!Mask) {
2174         // Set isThisReturn to false if the calling convention is not one that
2175         // allows 'returned' to be modeled in this way, so LowerCallResult does
2176         // not try to pass 'this' straight through
2177         isThisReturn = false;
2178         Mask = ARI->getCallPreservedMask(MF, CallConv);
2179       }
2180     } else
2181       Mask = ARI->getCallPreservedMask(MF, CallConv);
2182 
2183     assert(Mask && "Missing call preserved mask for calling convention");
2184     Ops.push_back(DAG.getRegisterMask(Mask));
2185   }
2186 
2187   if (InFlag.getNode())
2188     Ops.push_back(InFlag);
2189 
2190   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2191   if (isTailCall) {
2192     MF.getFrameInfo().setHasTailCall();
2193     return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
2194   }
2195 
2196   // Returns a chain and a flag for retval copy to use.
2197   Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
2198   InFlag = Chain.getValue(1);
2199 
2200   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
2201                              DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
2202   if (!Ins.empty())
2203     InFlag = Chain.getValue(1);
2204 
2205   // Handle result values, copying them out of physregs into vregs that we
2206   // return.
2207   return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
2208                          InVals, isThisReturn,
2209                          isThisReturn ? OutVals[0] : SDValue());
2210 }
2211 
2212 /// HandleByVal - Every parameter *after* a byval parameter is passed
2213 /// on the stack.  Remember the next parameter register to allocate,
2214 /// and then confiscate the rest of the parameter registers to insure
2215 /// this.
2216 void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
2217                                     unsigned Align) const {
2218   // Byval (as with any stack) slots are always at least 4 byte aligned.
2219   Align = std::max(Align, 4U);
2220 
2221   unsigned Reg = State->AllocateReg(GPRArgRegs);
2222   if (!Reg)
2223     return;
2224 
2225   unsigned AlignInRegs = Align / 4;
2226   unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
2227   for (unsigned i = 0; i < Waste; ++i)
2228     Reg = State->AllocateReg(GPRArgRegs);
2229 
2230   if (!Reg)
2231     return;
2232 
2233   unsigned Excess = 4 * (ARM::R4 - Reg);
2234 
2235   // Special case when NSAA != SP and parameter size greater than size of
2236   // all remained GPR regs. In that case we can't split parameter, we must
2237   // send it to stack. We also must set NCRN to R4, so waste all
2238   // remained registers.
2239   const unsigned NSAAOffset = State->getNextStackOffset();
2240   if (NSAAOffset != 0 && Size > Excess) {
2241     while (State->AllocateReg(GPRArgRegs))
2242       ;
2243     return;
2244   }
2245 
2246   // First register for byval parameter is the first register that wasn't
2247   // allocated before this method call, so it would be "reg".
2248   // If parameter is small enough to be saved in range [reg, r4), then
2249   // the end (first after last) register would be reg + param-size-in-regs,
2250   // else parameter would be splitted between registers and stack,
2251   // end register would be r4 in this case.
2252   unsigned ByValRegBegin = Reg;
2253   unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
2254   State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
2255   // Note, first register is allocated in the beginning of function already,
2256   // allocate remained amount of registers we need.
2257   for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2258     State->AllocateReg(GPRArgRegs);
2259   // A byval parameter that is split between registers and memory needs its
2260   // size truncated here.
2261   // In the case where the entire structure fits in registers, we set the
2262   // size in memory to zero.
2263   Size = std::max<int>(Size - Excess, 0);
2264 }
2265 
2266 /// MatchingStackOffset - Return true if the given stack call argument is
2267 /// already available in the same position (relatively) of the caller's
2268 /// incoming argument stack.
2269 static
2270 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2271                          MachineFrameInfo &MFI, const MachineRegisterInfo *MRI,
2272                          const TargetInstrInfo *TII) {
2273   unsigned Bytes = Arg.getValueSizeInBits() / 8;
2274   int FI = std::numeric_limits<int>::max();
2275   if (Arg.getOpcode() == ISD::CopyFromReg) {
2276     unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2277     if (!TargetRegisterInfo::isVirtualRegister(VR))
2278       return false;
2279     MachineInstr *Def = MRI->getVRegDef(VR);
2280     if (!Def)
2281       return false;
2282     if (!Flags.isByVal()) {
2283       if (!TII->isLoadFromStackSlot(*Def, FI))
2284         return false;
2285     } else {
2286       return false;
2287     }
2288   } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2289     if (Flags.isByVal())
2290       // ByVal argument is passed in as a pointer but it's now being
2291       // dereferenced. e.g.
2292       // define @foo(%struct.X* %A) {
2293       //   tail call @bar(%struct.X* byval %A)
2294       // }
2295       return false;
2296     SDValue Ptr = Ld->getBasePtr();
2297     FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2298     if (!FINode)
2299       return false;
2300     FI = FINode->getIndex();
2301   } else
2302     return false;
2303 
2304   assert(FI != std::numeric_limits<int>::max());
2305   if (!MFI.isFixedObjectIndex(FI))
2306     return false;
2307   return Offset == MFI.getObjectOffset(FI) && Bytes == MFI.getObjectSize(FI);
2308 }
2309 
2310 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
2311 /// for tail call optimization. Targets which want to do tail call
2312 /// optimization should implement this function.
2313 bool
2314 ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2315                                                      CallingConv::ID CalleeCC,
2316                                                      bool isVarArg,
2317                                                      bool isCalleeStructRet,
2318                                                      bool isCallerStructRet,
2319                                     const SmallVectorImpl<ISD::OutputArg> &Outs,
2320                                     const SmallVectorImpl<SDValue> &OutVals,
2321                                     const SmallVectorImpl<ISD::InputArg> &Ins,
2322                                                      SelectionDAG& DAG) const {
2323   MachineFunction &MF = DAG.getMachineFunction();
2324   const Function &CallerF = MF.getFunction();
2325   CallingConv::ID CallerCC = CallerF.getCallingConv();
2326 
2327   assert(Subtarget->supportsTailCall());
2328 
2329   // Tail calls to function pointers cannot be optimized for Thumb1 if the args
2330   // to the call take up r0-r3. The reason is that there are no legal registers
2331   // left to hold the pointer to the function to be called.
2332   if (Subtarget->isThumb1Only() && Outs.size() >= 4 &&
2333       !isa<GlobalAddressSDNode>(Callee.getNode()))
2334       return false;
2335 
2336   // Look for obvious safe cases to perform tail call optimization that do not
2337   // require ABI changes. This is what gcc calls sibcall.
2338 
2339   // Exception-handling functions need a special set of instructions to indicate
2340   // a return to the hardware. Tail-calling another function would probably
2341   // break this.
2342   if (CallerF.hasFnAttribute("interrupt"))
2343     return false;
2344 
2345   // Also avoid sibcall optimization if either caller or callee uses struct
2346   // return semantics.
2347   if (isCalleeStructRet || isCallerStructRet)
2348     return false;
2349 
2350   // Externally-defined functions with weak linkage should not be
2351   // tail-called on ARM when the OS does not support dynamic
2352   // pre-emption of symbols, as the AAELF spec requires normal calls
2353   // to undefined weak functions to be replaced with a NOP or jump to the
2354   // next instruction. The behaviour of branch instructions in this
2355   // situation (as used for tail calls) is implementation-defined, so we
2356   // cannot rely on the linker replacing the tail call with a return.
2357   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2358     const GlobalValue *GV = G->getGlobal();
2359     const Triple &TT = getTargetMachine().getTargetTriple();
2360     if (GV->hasExternalWeakLinkage() &&
2361         (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
2362       return false;
2363   }
2364 
2365   // Check that the call results are passed in the same way.
2366   LLVMContext &C = *DAG.getContext();
2367   if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
2368                                   CCAssignFnForReturn(CalleeCC, isVarArg),
2369                                   CCAssignFnForReturn(CallerCC, isVarArg)))
2370     return false;
2371   // The callee has to preserve all registers the caller needs to preserve.
2372   const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2373   const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
2374   if (CalleeCC != CallerCC) {
2375     const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
2376     if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
2377       return false;
2378   }
2379 
2380   // If Caller's vararg or byval argument has been split between registers and
2381   // stack, do not perform tail call, since part of the argument is in caller's
2382   // local frame.
2383   const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>();
2384   if (AFI_Caller->getArgRegsSaveSize())
2385     return false;
2386 
2387   // If the callee takes no arguments then go on to check the results of the
2388   // call.
2389   if (!Outs.empty()) {
2390     // Check if stack adjustment is needed. For now, do not do this if any
2391     // argument is passed on the stack.
2392     SmallVector<CCValAssign, 16> ArgLocs;
2393     CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C);
2394     CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, isVarArg));
2395     if (CCInfo.getNextStackOffset()) {
2396       // Check if the arguments are already laid out in the right way as
2397       // the caller's fixed stack objects.
2398       MachineFrameInfo &MFI = MF.getFrameInfo();
2399       const MachineRegisterInfo *MRI = &MF.getRegInfo();
2400       const TargetInstrInfo *TII = Subtarget->getInstrInfo();
2401       for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2402            i != e;
2403            ++i, ++realArgIdx) {
2404         CCValAssign &VA = ArgLocs[i];
2405         EVT RegVT = VA.getLocVT();
2406         SDValue Arg = OutVals[realArgIdx];
2407         ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
2408         if (VA.getLocInfo() == CCValAssign::Indirect)
2409           return false;
2410         if (VA.needsCustom()) {
2411           // f64 and vector types are split into multiple registers or
2412           // register/stack-slot combinations.  The types will not match
2413           // the registers; give up on memory f64 refs until we figure
2414           // out what to do about this.
2415           if (!VA.isRegLoc())
2416             return false;
2417           if (!ArgLocs[++i].isRegLoc())
2418             return false;
2419           if (RegVT == MVT::v2f64) {
2420             if (!ArgLocs[++i].isRegLoc())
2421               return false;
2422             if (!ArgLocs[++i].isRegLoc())
2423               return false;
2424           }
2425         } else if (!VA.isRegLoc()) {
2426           if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2427                                    MFI, MRI, TII))
2428             return false;
2429         }
2430       }
2431     }
2432 
2433     const MachineRegisterInfo &MRI = MF.getRegInfo();
2434     if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals))
2435       return false;
2436   }
2437 
2438   return true;
2439 }
2440 
2441 bool
2442 ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2443                                   MachineFunction &MF, bool isVarArg,
2444                                   const SmallVectorImpl<ISD::OutputArg> &Outs,
2445                                   LLVMContext &Context) const {
2446   SmallVector<CCValAssign, 16> RVLocs;
2447   CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
2448   return CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg));
2449 }
2450 
2451 static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2452                                     const SDLoc &DL, SelectionDAG &DAG) {
2453   const MachineFunction &MF = DAG.getMachineFunction();
2454   const Function &F = MF.getFunction();
2455 
2456   StringRef IntKind = F.getFnAttribute("interrupt").getValueAsString();
2457 
2458   // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2459   // version of the "preferred return address". These offsets affect the return
2460   // instruction if this is a return from PL1 without hypervisor extensions.
2461   //    IRQ/FIQ: +4     "subs pc, lr, #4"
2462   //    SWI:     0      "subs pc, lr, #0"
2463   //    ABORT:   +4     "subs pc, lr, #4"
2464   //    UNDEF:   +4/+2  "subs pc, lr, #0"
2465   // UNDEF varies depending on where the exception came from ARM or Thumb
2466   // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2467 
2468   int64_t LROffset;
2469   if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2470       IntKind == "ABORT")
2471     LROffset = 4;
2472   else if (IntKind == "SWI" || IntKind == "UNDEF")
2473     LROffset = 0;
2474   else
2475     report_fatal_error("Unsupported interrupt attribute. If present, value "
2476                        "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2477 
2478   RetOps.insert(RetOps.begin() + 1,
2479                 DAG.getConstant(LROffset, DL, MVT::i32, false));
2480 
2481   return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
2482 }
2483 
2484 SDValue
2485 ARMTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
2486                                bool isVarArg,
2487                                const SmallVectorImpl<ISD::OutputArg> &Outs,
2488                                const SmallVectorImpl<SDValue> &OutVals,
2489                                const SDLoc &dl, SelectionDAG &DAG) const {
2490   // CCValAssign - represent the assignment of the return value to a location.
2491   SmallVector<CCValAssign, 16> RVLocs;
2492 
2493   // CCState - Info about the registers and stack slots.
2494   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2495                  *DAG.getContext());
2496 
2497   // Analyze outgoing return values.
2498   CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg));
2499 
2500   SDValue Flag;
2501   SmallVector<SDValue, 4> RetOps;
2502   RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
2503   bool isLittleEndian = Subtarget->isLittle();
2504 
2505   MachineFunction &MF = DAG.getMachineFunction();
2506   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2507   AFI->setReturnRegsCount(RVLocs.size());
2508 
2509   // Copy the result values into the output registers.
2510   for (unsigned i = 0, realRVLocIdx = 0;
2511        i != RVLocs.size();
2512        ++i, ++realRVLocIdx) {
2513     CCValAssign &VA = RVLocs[i];
2514     assert(VA.isRegLoc() && "Can only return in registers!");
2515 
2516     SDValue Arg = OutVals[realRVLocIdx];
2517     bool ReturnF16 = false;
2518 
2519     if (Subtarget->hasFullFP16() && Subtarget->isTargetHardFloat()) {
2520       // Half-precision return values can be returned like this:
2521       //
2522       // t11 f16 = fadd ...
2523       // t12: i16 = bitcast t11
2524       //   t13: i32 = zero_extend t12
2525       // t14: f32 = bitcast t13  <~~~~~~~ Arg
2526       //
2527       // to avoid code generation for bitcasts, we simply set Arg to the node
2528       // that produces the f16 value, t11 in this case.
2529       //
2530       if (Arg.getValueType() == MVT::f32 && Arg.getOpcode() == ISD::BITCAST) {
2531         SDValue ZE = Arg.getOperand(0);
2532         if (ZE.getOpcode() == ISD::ZERO_EXTEND && ZE.getValueType() == MVT::i32) {
2533           SDValue BC = ZE.getOperand(0);
2534           if (BC.getOpcode() == ISD::BITCAST && BC.getValueType() == MVT::i16) {
2535             Arg = BC.getOperand(0);
2536             ReturnF16 = true;
2537           }
2538         }
2539       }
2540     }
2541 
2542     switch (VA.getLocInfo()) {
2543     default: llvm_unreachable("Unknown loc info!");
2544     case CCValAssign::Full: break;
2545     case CCValAssign::BCvt:
2546       if (!ReturnF16)
2547         Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
2548       break;
2549     }
2550 
2551     if (VA.needsCustom()) {
2552       if (VA.getLocVT() == MVT::v2f64) {
2553         // Extract the first half and return it in two registers.
2554         SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2555                                    DAG.getConstant(0, dl, MVT::i32));
2556         SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
2557                                        DAG.getVTList(MVT::i32, MVT::i32), Half);
2558 
2559         Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2560                                  HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2561                                  Flag);
2562         Flag = Chain.getValue(1);
2563         RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2564         VA = RVLocs[++i]; // skip ahead to next loc
2565         Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2566                                  HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2567                                  Flag);
2568         Flag = Chain.getValue(1);
2569         RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2570         VA = RVLocs[++i]; // skip ahead to next loc
2571 
2572         // Extract the 2nd half and fall through to handle it as an f64 value.
2573         Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2574                           DAG.getConstant(1, dl, MVT::i32));
2575       }
2576       // Legalize ret f64 -> ret 2 x i32.  We always have fmrrd if f64 is
2577       // available.
2578       SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
2579                                   DAG.getVTList(MVT::i32, MVT::i32), Arg);
2580       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2581                                fmrrd.getValue(isLittleEndian ? 0 : 1),
2582                                Flag);
2583       Flag = Chain.getValue(1);
2584       RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2585       VA = RVLocs[++i]; // skip ahead to next loc
2586       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2587                                fmrrd.getValue(isLittleEndian ? 1 : 0),
2588                                Flag);
2589     } else
2590       Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2591 
2592     // Guarantee that all emitted copies are
2593     // stuck together, avoiding something bad.
2594     Flag = Chain.getValue(1);
2595     RetOps.push_back(DAG.getRegister(VA.getLocReg(),
2596                                      ReturnF16 ? MVT::f16 : VA.getLocVT()));
2597   }
2598   const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2599   const MCPhysReg *I =
2600       TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
2601   if (I) {
2602     for (; *I; ++I) {
2603       if (ARM::GPRRegClass.contains(*I))
2604         RetOps.push_back(DAG.getRegister(*I, MVT::i32));
2605       else if (ARM::DPRRegClass.contains(*I))
2606         RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
2607       else
2608         llvm_unreachable("Unexpected register class in CSRsViaCopy!");
2609     }
2610   }
2611 
2612   // Update chain and glue.
2613   RetOps[0] = Chain;
2614   if (Flag.getNode())
2615     RetOps.push_back(Flag);
2616 
2617   // CPUs which aren't M-class use a special sequence to return from
2618   // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2619   // though we use "subs pc, lr, #N").
2620   //
2621   // M-class CPUs actually use a normal return sequence with a special
2622   // (hardware-provided) value in LR, so the normal code path works.
2623   if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt") &&
2624       !Subtarget->isMClass()) {
2625     if (Subtarget->isThumb1Only())
2626       report_fatal_error("interrupt attribute is not supported in Thumb1");
2627     return LowerInterruptReturn(RetOps, dl, DAG);
2628   }
2629 
2630   return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
2631 }
2632 
2633 bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
2634   if (N->getNumValues() != 1)
2635     return false;
2636   if (!N->hasNUsesOfValue(1, 0))
2637     return false;
2638 
2639   SDValue TCChain = Chain;
2640   SDNode *Copy = *N->use_begin();
2641   if (Copy->getOpcode() == ISD::CopyToReg) {
2642     // If the copy has a glue operand, we conservatively assume it isn't safe to
2643     // perform a tail call.
2644     if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2645       return false;
2646     TCChain = Copy->getOperand(0);
2647   } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2648     SDNode *VMov = Copy;
2649     // f64 returned in a pair of GPRs.
2650     SmallPtrSet<SDNode*, 2> Copies;
2651     for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2652          UI != UE; ++UI) {
2653       if (UI->getOpcode() != ISD::CopyToReg)
2654         return false;
2655       Copies.insert(*UI);
2656     }
2657     if (Copies.size() > 2)
2658       return false;
2659 
2660     for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2661          UI != UE; ++UI) {
2662       SDValue UseChain = UI->getOperand(0);
2663       if (Copies.count(UseChain.getNode()))
2664         // Second CopyToReg
2665         Copy = *UI;
2666       else {
2667         // We are at the top of this chain.
2668         // If the copy has a glue operand, we conservatively assume it
2669         // isn't safe to perform a tail call.
2670         if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2671           return false;
2672         // First CopyToReg
2673         TCChain = UseChain;
2674       }
2675     }
2676   } else if (Copy->getOpcode() == ISD::BITCAST) {
2677     // f32 returned in a single GPR.
2678     if (!Copy->hasOneUse())
2679       return false;
2680     Copy = *Copy->use_begin();
2681     if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
2682       return false;
2683     // If the copy has a glue operand, we conservatively assume it isn't safe to
2684     // perform a tail call.
2685     if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2686       return false;
2687     TCChain = Copy->getOperand(0);
2688   } else {
2689     return false;
2690   }
2691 
2692   bool HasRet = false;
2693   for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2694        UI != UE; ++UI) {
2695     if (UI->getOpcode() != ARMISD::RET_FLAG &&
2696         UI->getOpcode() != ARMISD::INTRET_FLAG)
2697       return false;
2698     HasRet = true;
2699   }
2700 
2701   if (!HasRet)
2702     return false;
2703 
2704   Chain = TCChain;
2705   return true;
2706 }
2707 
2708 bool ARMTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
2709   if (!Subtarget->supportsTailCall())
2710     return false;
2711 
2712   auto Attr =
2713       CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2714   if (!CI->isTailCall() || Attr.getValueAsString() == "true")
2715     return false;
2716 
2717   return true;
2718 }
2719 
2720 // Trying to write a 64 bit value so need to split into two 32 bit values first,
2721 // and pass the lower and high parts through.
2722 static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2723   SDLoc DL(Op);
2724   SDValue WriteValue = Op->getOperand(2);
2725 
2726   // This function is only supposed to be called for i64 type argument.
2727   assert(WriteValue.getValueType() == MVT::i64
2728           && "LowerWRITE_REGISTER called for non-i64 type argument.");
2729 
2730   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2731                            DAG.getConstant(0, DL, MVT::i32));
2732   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2733                            DAG.getConstant(1, DL, MVT::i32));
2734   SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2735   return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2736 }
2737 
2738 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2739 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2740 // one of the above mentioned nodes. It has to be wrapped because otherwise
2741 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2742 // be used to form addressing mode. These wrapped nodes will be selected
2743 // into MOVi.
2744 SDValue ARMTargetLowering::LowerConstantPool(SDValue Op,
2745                                              SelectionDAG &DAG) const {
2746   EVT PtrVT = Op.getValueType();
2747   // FIXME there is no actual debug info here
2748   SDLoc dl(Op);
2749   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2750   SDValue Res;
2751 
2752   // When generating execute-only code Constant Pools must be promoted to the
2753   // global data section. It's a bit ugly that we can't share them across basic
2754   // blocks, but this way we guarantee that execute-only behaves correct with
2755   // position-independent addressing modes.
2756   if (Subtarget->genExecuteOnly()) {
2757     auto AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
2758     auto T = const_cast<Type*>(CP->getType());
2759     auto C = const_cast<Constant*>(CP->getConstVal());
2760     auto M = const_cast<Module*>(DAG.getMachineFunction().
2761                                  getFunction().getParent());
2762     auto GV = new GlobalVariable(
2763                     *M, T, /*isConst=*/true, GlobalVariable::InternalLinkage, C,
2764                     Twine(DAG.getDataLayout().getPrivateGlobalPrefix()) + "CP" +
2765                     Twine(DAG.getMachineFunction().getFunctionNumber()) + "_" +
2766                     Twine(AFI->createPICLabelUId())
2767                   );
2768     SDValue GA = DAG.getTargetGlobalAddress(dyn_cast<GlobalValue>(GV),
2769                                             dl, PtrVT);
2770     return LowerGlobalAddress(GA, DAG);
2771   }
2772 
2773   if (CP->isMachineConstantPoolEntry())
2774     Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2775                                     CP->getAlignment());
2776   else
2777     Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2778                                     CP->getAlignment());
2779   return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
2780 }
2781 
2782 unsigned ARMTargetLowering::getJumpTableEncoding() const {
2783   return MachineJumpTableInfo::EK_Inline;
2784 }
2785 
2786 SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2787                                              SelectionDAG &DAG) const {
2788   MachineFunction &MF = DAG.getMachineFunction();
2789   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2790   unsigned ARMPCLabelIndex = 0;
2791   SDLoc DL(Op);
2792   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2793   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
2794   SDValue CPAddr;
2795   bool IsPositionIndependent = isPositionIndependent() || Subtarget->isROPI();
2796   if (!IsPositionIndependent) {
2797     CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2798   } else {
2799     unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2800     ARMPCLabelIndex = AFI->createPICLabelUId();
2801     ARMConstantPoolValue *CPV =
2802       ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2803                                       ARMCP::CPBlockAddress, PCAdj);
2804     CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2805   }
2806   CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2807   SDValue Result = DAG.getLoad(
2808       PtrVT, DL, DAG.getEntryNode(), CPAddr,
2809       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
2810   if (!IsPositionIndependent)
2811     return Result;
2812   SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
2813   return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
2814 }
2815 
2816 /// Convert a TLS address reference into the correct sequence of loads
2817 /// and calls to compute the variable's address for Darwin, and return an
2818 /// SDValue containing the final node.
2819 
2820 /// Darwin only has one TLS scheme which must be capable of dealing with the
2821 /// fully general situation, in the worst case. This means:
2822 ///     + "extern __thread" declaration.
2823 ///     + Defined in a possibly unknown dynamic library.
2824 ///
2825 /// The general system is that each __thread variable has a [3 x i32] descriptor
2826 /// which contains information used by the runtime to calculate the address. The
2827 /// only part of this the compiler needs to know about is the first word, which
2828 /// contains a function pointer that must be called with the address of the
2829 /// entire descriptor in "r0".
2830 ///
2831 /// Since this descriptor may be in a different unit, in general access must
2832 /// proceed along the usual ARM rules. A common sequence to produce is:
2833 ///
2834 ///     movw rT1, :lower16:_var$non_lazy_ptr
2835 ///     movt rT1, :upper16:_var$non_lazy_ptr
2836 ///     ldr r0, [rT1]
2837 ///     ldr rT2, [r0]
2838 ///     blx rT2
2839 ///     [...address now in r0...]
2840 SDValue
2841 ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op,
2842                                                SelectionDAG &DAG) const {
2843   assert(Subtarget->isTargetDarwin() &&
2844          "This function expects a Darwin target");
2845   SDLoc DL(Op);
2846 
2847   // First step is to get the address of the actua global symbol. This is where
2848   // the TLS descriptor lives.
2849   SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG);
2850 
2851   // The first entry in the descriptor is a function pointer that we must call
2852   // to obtain the address of the variable.
2853   SDValue Chain = DAG.getEntryNode();
2854   SDValue FuncTLVGet = DAG.getLoad(
2855       MVT::i32, DL, Chain, DescAddr,
2856       MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2857       /* Alignment = */ 4,
2858       MachineMemOperand::MONonTemporal | MachineMemOperand::MODereferenceable |
2859           MachineMemOperand::MOInvariant);
2860   Chain = FuncTLVGet.getValue(1);
2861 
2862   MachineFunction &F = DAG.getMachineFunction();
2863   MachineFrameInfo &MFI = F.getFrameInfo();
2864   MFI.setAdjustsStack(true);
2865 
2866   // TLS calls preserve all registers except those that absolutely must be
2867   // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be
2868   // silly).
2869   auto TRI =
2870       getTargetMachine().getSubtargetImpl(F.getFunction())->getRegisterInfo();
2871   auto ARI = static_cast<const ARMRegisterInfo *>(TRI);
2872   const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction());
2873 
2874   // Finally, we can make the call. This is just a degenerate version of a
2875   // normal AArch64 call node: r0 takes the address of the descriptor, and
2876   // returns the address of the variable in this thread.
2877   Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue());
2878   Chain =
2879       DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2880                   Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32),
2881                   DAG.getRegisterMask(Mask), Chain.getValue(1));
2882   return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1));
2883 }
2884 
2885 SDValue
2886 ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op,
2887                                                 SelectionDAG &DAG) const {
2888   assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering");
2889 
2890   SDValue Chain = DAG.getEntryNode();
2891   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2892   SDLoc DL(Op);
2893 
2894   // Load the current TEB (thread environment block)
2895   SDValue Ops[] = {Chain,
2896                    DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
2897                    DAG.getConstant(15, DL, MVT::i32),
2898                    DAG.getConstant(0, DL, MVT::i32),
2899                    DAG.getConstant(13, DL, MVT::i32),
2900                    DAG.getConstant(0, DL, MVT::i32),
2901                    DAG.getConstant(2, DL, MVT::i32)};
2902   SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
2903                                    DAG.getVTList(MVT::i32, MVT::Other), Ops);
2904 
2905   SDValue TEB = CurrentTEB.getValue(0);
2906   Chain = CurrentTEB.getValue(1);
2907 
2908   // Load the ThreadLocalStoragePointer from the TEB
2909   // A pointer to the TLS array is located at offset 0x2c from the TEB.
2910   SDValue TLSArray =
2911       DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL));
2912   TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo());
2913 
2914   // The pointer to the thread's TLS data area is at the TLS Index scaled by 4
2915   // offset into the TLSArray.
2916 
2917   // Load the TLS index from the C runtime
2918   SDValue TLSIndex =
2919       DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG);
2920   TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex);
2921   TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo());
2922 
2923   SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex,
2924                               DAG.getConstant(2, DL, MVT::i32));
2925   SDValue TLS = DAG.getLoad(PtrVT, DL, Chain,
2926                             DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot),
2927                             MachinePointerInfo());
2928 
2929   // Get the offset of the start of the .tls section (section base)
2930   const auto *GA = cast<GlobalAddressSDNode>(Op);
2931   auto *CPV = ARMConstantPoolConstant::Create(GA->getGlobal(), ARMCP::SECREL);
2932   SDValue Offset = DAG.getLoad(
2933       PtrVT, DL, Chain, DAG.getNode(ARMISD::Wrapper, DL, MVT::i32,
2934                                     DAG.getTargetConstantPool(CPV, PtrVT, 4)),
2935       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
2936 
2937   return DAG.getNode(ISD::ADD, DL, PtrVT, TLS, Offset);
2938 }
2939 
2940 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
2941 SDValue
2942 ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
2943                                                  SelectionDAG &DAG) const {
2944   SDLoc dl(GA);
2945   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2946   unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2947   MachineFunction &MF = DAG.getMachineFunction();
2948   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2949   unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2950   ARMConstantPoolValue *CPV =
2951     ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2952                                     ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
2953   SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2954   Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
2955   Argument = DAG.getLoad(
2956       PtrVT, dl, DAG.getEntryNode(), Argument,
2957       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
2958   SDValue Chain = Argument.getValue(1);
2959 
2960   SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
2961   Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
2962 
2963   // call __tls_get_addr.
2964   ArgListTy Args;
2965   ArgListEntry Entry;
2966   Entry.Node = Argument;
2967   Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
2968   Args.push_back(Entry);
2969 
2970   // FIXME: is there useful debug info available here?
2971   TargetLowering::CallLoweringInfo CLI(DAG);
2972   CLI.setDebugLoc(dl).setChain(Chain).setLibCallee(
2973       CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
2974       DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args));
2975 
2976   std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
2977   return CallResult.first;
2978 }
2979 
2980 // Lower ISD::GlobalTLSAddress using the "initial exec" or
2981 // "local exec" model.
2982 SDValue
2983 ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
2984                                         SelectionDAG &DAG,
2985                                         TLSModel::Model model) const {
2986   const GlobalValue *GV = GA->getGlobal();
2987   SDLoc dl(GA);
2988   SDValue Offset;
2989   SDValue Chain = DAG.getEntryNode();
2990   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2991   // Get the Thread Pointer
2992   SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2993 
2994   if (model == TLSModel::InitialExec) {
2995     MachineFunction &MF = DAG.getMachineFunction();
2996     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2997     unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2998     // Initial exec model.
2999     unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
3000     ARMConstantPoolValue *CPV =
3001       ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
3002                                       ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
3003                                       true);
3004     Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
3005     Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
3006     Offset = DAG.getLoad(
3007         PtrVT, dl, Chain, Offset,
3008         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
3009     Chain = Offset.getValue(1);
3010 
3011     SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
3012     Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
3013 
3014     Offset = DAG.getLoad(
3015         PtrVT, dl, Chain, Offset,
3016         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
3017   } else {
3018     // local exec model
3019     assert(model == TLSModel::LocalExec);
3020     ARMConstantPoolValue *CPV =
3021       ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
3022     Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
3023     Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
3024     Offset = DAG.getLoad(
3025         PtrVT, dl, Chain, Offset,
3026         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
3027   }
3028 
3029   // The address of the thread local variable is the add of the thread
3030   // pointer with the offset of the variable.
3031   return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
3032 }
3033 
3034 SDValue
3035 ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
3036   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3037   if (DAG.getTarget().useEmulatedTLS())
3038     return LowerToTLSEmulatedModel(GA, DAG);
3039 
3040   if (Subtarget->isTargetDarwin())
3041     return LowerGlobalTLSAddressDarwin(Op, DAG);
3042 
3043   if (Subtarget->isTargetWindows())
3044     return LowerGlobalTLSAddressWindows(Op, DAG);
3045 
3046   // TODO: implement the "local dynamic" model
3047   assert(Subtarget->isTargetELF() && "Only ELF implemented here");
3048   TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
3049 
3050   switch (model) {
3051     case TLSModel::GeneralDynamic:
3052     case TLSModel::LocalDynamic:
3053       return LowerToTLSGeneralDynamicModel(GA, DAG);
3054     case TLSModel::InitialExec:
3055     case TLSModel::LocalExec:
3056       return LowerToTLSExecModels(GA, DAG, model);
3057   }
3058   llvm_unreachable("bogus TLS model");
3059 }
3060 
3061 /// Return true if all users of V are within function F, looking through
3062 /// ConstantExprs.
3063 static bool allUsersAreInFunction(const Value *V, const Function *F) {
3064   SmallVector<const User*,4> Worklist;
3065   for (auto *U : V->users())
3066     Worklist.push_back(U);
3067   while (!Worklist.empty()) {
3068     auto *U = Worklist.pop_back_val();
3069     if (isa<ConstantExpr>(U)) {
3070       for (auto *UU : U->users())
3071         Worklist.push_back(UU);
3072       continue;
3073     }
3074 
3075     auto *I = dyn_cast<Instruction>(U);
3076     if (!I || I->getParent()->getParent() != F)
3077       return false;
3078   }
3079   return true;
3080 }
3081 
3082 static SDValue promoteToConstantPool(const ARMTargetLowering *TLI,
3083                                      const GlobalValue *GV, SelectionDAG &DAG,
3084                                      EVT PtrVT, const SDLoc &dl) {
3085   // If we're creating a pool entry for a constant global with unnamed address,
3086   // and the global is small enough, we can emit it inline into the constant pool
3087   // to save ourselves an indirection.
3088   //
3089   // This is a win if the constant is only used in one function (so it doesn't
3090   // need to be duplicated) or duplicating the constant wouldn't increase code
3091   // size (implying the constant is no larger than 4 bytes).
3092   const Function &F = DAG.getMachineFunction().getFunction();
3093 
3094   // We rely on this decision to inline being idemopotent and unrelated to the
3095   // use-site. We know that if we inline a variable at one use site, we'll
3096   // inline it elsewhere too (and reuse the constant pool entry). Fast-isel
3097   // doesn't know about this optimization, so bail out if it's enabled else
3098   // we could decide to inline here (and thus never emit the GV) but require
3099   // the GV from fast-isel generated code.
3100   if (!EnableConstpoolPromotion ||
3101       DAG.getMachineFunction().getTarget().Options.EnableFastISel)
3102       return SDValue();
3103 
3104   auto *GVar = dyn_cast<GlobalVariable>(GV);
3105   if (!GVar || !GVar->hasInitializer() ||
3106       !GVar->isConstant() || !GVar->hasGlobalUnnamedAddr() ||
3107       !GVar->hasLocalLinkage())
3108     return SDValue();
3109 
3110   // If we inline a value that contains relocations, we move the relocations
3111   // from .data to .text. This is not allowed in position-independent code.
3112   auto *Init = GVar->getInitializer();
3113   if ((TLI->isPositionIndependent() || TLI->getSubtarget()->isROPI()) &&
3114       Init->needsRelocation())
3115     return SDValue();
3116 
3117   // The constant islands pass can only really deal with alignment requests
3118   // <= 4 bytes and cannot pad constants itself. Therefore we cannot promote
3119   // any type wanting greater alignment requirements than 4 bytes. We also
3120   // can only promote constants that are multiples of 4 bytes in size or
3121   // are paddable to a multiple of 4. Currently we only try and pad constants
3122   // that are strings for simplicity.
3123   auto *CDAInit = dyn_cast<ConstantDataArray>(Init);
3124   unsigned Size = DAG.getDataLayout().getTypeAllocSize(Init->getType());
3125   unsigned Align = DAG.getDataLayout().getPreferredAlignment(GVar);
3126   unsigned RequiredPadding = 4 - (Size % 4);
3127   bool PaddingPossible =
3128     RequiredPadding == 4 || (CDAInit && CDAInit->isString());
3129   if (!PaddingPossible || Align > 4 || Size > ConstpoolPromotionMaxSize ||
3130       Size == 0)
3131     return SDValue();
3132 
3133   unsigned PaddedSize = Size + ((RequiredPadding == 4) ? 0 : RequiredPadding);
3134   MachineFunction &MF = DAG.getMachineFunction();
3135   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3136 
3137   // We can't bloat the constant pool too much, else the ConstantIslands pass
3138   // may fail to converge. If we haven't promoted this global yet (it may have
3139   // multiple uses), and promoting it would increase the constant pool size (Sz
3140   // > 4), ensure we have space to do so up to MaxTotal.
3141   if (!AFI->getGlobalsPromotedToConstantPool().count(GVar) && Size > 4)
3142     if (AFI->getPromotedConstpoolIncrease() + PaddedSize - 4 >=
3143         ConstpoolPromotionMaxTotal)
3144       return SDValue();
3145 
3146   // This is only valid if all users are in a single function; we can't clone
3147   // the constant in general. The LLVM IR unnamed_addr allows merging
3148   // constants, but not cloning them.
3149   //
3150   // We could potentially allow cloning if we could prove all uses of the
3151   // constant in the current function don't care about the address, like
3152   // printf format strings. But that isn't implemented for now.
3153   if (!allUsersAreInFunction(GVar, &F))
3154     return SDValue();
3155 
3156   // We're going to inline this global. Pad it out if needed.
3157   if (RequiredPadding != 4) {
3158     StringRef S = CDAInit->getAsString();
3159 
3160     SmallVector<uint8_t,16> V(S.size());
3161     std::copy(S.bytes_begin(), S.bytes_end(), V.begin());
3162     while (RequiredPadding--)
3163       V.push_back(0);
3164     Init = ConstantDataArray::get(*DAG.getContext(), V);
3165   }
3166 
3167   auto CPVal = ARMConstantPoolConstant::Create(GVar, Init);
3168   SDValue CPAddr =
3169     DAG.getTargetConstantPool(CPVal, PtrVT, /*Align=*/4);
3170   if (!AFI->getGlobalsPromotedToConstantPool().count(GVar)) {
3171     AFI->markGlobalAsPromotedToConstantPool(GVar);
3172     AFI->setPromotedConstpoolIncrease(AFI->getPromotedConstpoolIncrease() +
3173                                       PaddedSize - 4);
3174   }
3175   ++NumConstpoolPromoted;
3176   return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
3177 }
3178 
3179 bool ARMTargetLowering::isReadOnly(const GlobalValue *GV) const {
3180   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
3181     if (!(GV = GA->getBaseObject()))
3182       return false;
3183   if (const auto *V = dyn_cast<GlobalVariable>(GV))
3184     return V->isConstant();
3185   return isa<Function>(GV);
3186 }
3187 
3188 SDValue ARMTargetLowering::LowerGlobalAddress(SDValue Op,
3189                                               SelectionDAG &DAG) const {
3190   switch (Subtarget->getTargetTriple().getObjectFormat()) {
3191   default: llvm_unreachable("unknown object format");
3192   case Triple::COFF:
3193     return LowerGlobalAddressWindows(Op, DAG);
3194   case Triple::ELF:
3195     return LowerGlobalAddressELF(Op, DAG);
3196   case Triple::MachO:
3197     return LowerGlobalAddressDarwin(Op, DAG);
3198   }
3199 }
3200 
3201 SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
3202                                                  SelectionDAG &DAG) const {
3203   EVT PtrVT = getPointerTy(DAG.getDataLayout());
3204   SDLoc dl(Op);
3205   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3206   const TargetMachine &TM = getTargetMachine();
3207   bool IsRO = isReadOnly(GV);
3208 
3209   // promoteToConstantPool only if not generating XO text section
3210   if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV) && !Subtarget->genExecuteOnly())
3211     if (SDValue V = promoteToConstantPool(this, GV, DAG, PtrVT, dl))
3212       return V;
3213 
3214   if (isPositionIndependent()) {
3215     bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
3216     SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
3217                                            UseGOT_PREL ? ARMII::MO_GOT : 0);
3218     SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G);
3219     if (UseGOT_PREL)
3220       Result =
3221           DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
3222                       MachinePointerInfo::getGOT(DAG.getMachineFunction()));
3223     return Result;
3224   } else if (Subtarget->isROPI() && IsRO) {
3225     // PC-relative.
3226     SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
3227     SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G);
3228     return Result;
3229   } else if (Subtarget->isRWPI() && !IsRO) {
3230     // SB-relative.
3231     SDValue RelAddr;
3232     if (Subtarget->useMovt()) {
3233       ++NumMovwMovt;
3234       SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_SBREL);
3235       RelAddr = DAG.getNode(ARMISD::Wrapper, dl, PtrVT, G);
3236     } else { // use literal pool for address constant
3237       ARMConstantPoolValue *CPV =
3238         ARMConstantPoolConstant::Create(GV, ARMCP::SBREL);
3239       SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
3240       CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
3241       RelAddr = DAG.getLoad(
3242           PtrVT, dl, DAG.getEntryNode(), CPAddr,
3243           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
3244     }
3245     SDValue SB = DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::R9, PtrVT);
3246     SDValue Result = DAG.getNode(ISD::ADD, dl, PtrVT, SB, RelAddr);
3247     return Result;
3248   }
3249 
3250   // If we have T2 ops, we can materialize the address directly via movt/movw
3251   // pair. This is always cheaper.
3252   if (Subtarget->useMovt()) {
3253     ++NumMovwMovt;
3254     // FIXME: Once remat is capable of dealing with instructions with register
3255     // operands, expand this into two nodes.
3256     return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
3257                        DAG.getTargetGlobalAddress(GV, dl, PtrVT));
3258   } else {
3259     SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
3260     CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
3261     return DAG.getLoad(
3262         PtrVT, dl, DAG.getEntryNode(), CPAddr,
3263         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
3264   }
3265 }
3266 
3267 SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
3268                                                     SelectionDAG &DAG) const {
3269   assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
3270          "ROPI/RWPI not currently supported for Darwin");
3271   EVT PtrVT = getPointerTy(DAG.getDataLayout());
3272   SDLoc dl(Op);
3273   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3274 
3275   if (Subtarget->useMovt())
3276     ++NumMovwMovt;
3277 
3278   // FIXME: Once remat is capable of dealing with instructions with register
3279   // operands, expand this into multiple nodes
3280   unsigned Wrapper =
3281       isPositionIndependent() ? ARMISD::WrapperPIC : ARMISD::Wrapper;
3282 
3283   SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
3284   SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
3285 
3286   if (Subtarget->isGVIndirectSymbol(GV))
3287     Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
3288                          MachinePointerInfo::getGOT(DAG.getMachineFunction()));
3289   return Result;
3290 }
3291 
3292 SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
3293                                                      SelectionDAG &DAG) const {
3294   assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
3295   assert(Subtarget->useMovt() &&
3296          "Windows on ARM expects to use movw/movt");
3297   assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
3298          "ROPI/RWPI not currently supported for Windows");
3299 
3300   const TargetMachine &TM = getTargetMachine();
3301   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3302   ARMII::TOF TargetFlags = ARMII::MO_NO_FLAG;
3303   if (GV->hasDLLImportStorageClass())
3304     TargetFlags = ARMII::MO_DLLIMPORT;
3305   else if (!TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
3306     TargetFlags = ARMII::MO_COFFSTUB;
3307   EVT PtrVT = getPointerTy(DAG.getDataLayout());
3308   SDValue Result;
3309   SDLoc DL(Op);
3310 
3311   ++NumMovwMovt;
3312 
3313   // FIXME: Once remat is capable of dealing with instructions with register
3314   // operands, expand this into two nodes.
3315   Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
3316                        DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
3317                                                   TargetFlags));
3318   if (TargetFlags & (ARMII::MO_DLLIMPORT | ARMII::MO_COFFSTUB))
3319     Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
3320                          MachinePointerInfo::getGOT(DAG.getMachineFunction()));
3321   return Result;
3322 }
3323 
3324 SDValue
3325 ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
3326   SDLoc dl(Op);
3327   SDValue Val = DAG.getConstant(0, dl, MVT::i32);
3328   return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
3329                      DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
3330                      Op.getOperand(1), Val);
3331 }
3332 
3333 SDValue
3334 ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
3335   SDLoc dl(Op);
3336   return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
3337                      Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
3338 }
3339 
3340 SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
3341                                                       SelectionDAG &DAG) const {
3342   SDLoc dl(Op);
3343   return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
3344                      Op.getOperand(0));
3345 }
3346 
3347 SDValue
3348 ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
3349                                           const ARMSubtarget *Subtarget) const {
3350   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3351   SDLoc dl(Op);
3352   switch (IntNo) {
3353   default: return SDValue();    // Don't custom lower most intrinsics.
3354   case Intrinsic::thread_pointer: {
3355     EVT PtrVT = getPointerTy(DAG.getDataLayout());
3356     return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
3357   }
3358   case Intrinsic::eh_sjlj_lsda: {
3359     MachineFunction &MF = DAG.getMachineFunction();
3360     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3361     unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
3362     EVT PtrVT = getPointerTy(DAG.getDataLayout());
3363     SDValue CPAddr;
3364     bool IsPositionIndependent = isPositionIndependent();
3365     unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0;
3366     ARMConstantPoolValue *CPV =
3367       ARMConstantPoolConstant::Create(&MF.getFunction(), ARMPCLabelIndex,
3368                                       ARMCP::CPLSDA, PCAdj);
3369     CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
3370     CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
3371     SDValue Result = DAG.getLoad(
3372         PtrVT, dl, DAG.getEntryNode(), CPAddr,
3373         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
3374 
3375     if (IsPositionIndependent) {
3376       SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
3377       Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
3378     }
3379     return Result;
3380   }
3381   case Intrinsic::arm_neon_vabs:
3382     return DAG.getNode(ISD::ABS, SDLoc(Op), Op.getValueType(),
3383                         Op.getOperand(1));
3384   case Intrinsic::arm_neon_vmulls:
3385   case Intrinsic::arm_neon_vmullu: {
3386     unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
3387       ? ARMISD::VMULLs : ARMISD::VMULLu;
3388     return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3389                        Op.getOperand(1), Op.getOperand(2));
3390   }
3391   case Intrinsic::arm_neon_vminnm:
3392   case Intrinsic::arm_neon_vmaxnm: {
3393     unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
3394       ? ISD::FMINNUM : ISD::FMAXNUM;
3395     return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3396                        Op.getOperand(1), Op.getOperand(2));
3397   }
3398   case Intrinsic::arm_neon_vminu:
3399   case Intrinsic::arm_neon_vmaxu: {
3400     if (Op.getValueType().isFloatingPoint())
3401       return SDValue();
3402     unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
3403       ? ISD::UMIN : ISD::UMAX;
3404     return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3405                          Op.getOperand(1), Op.getOperand(2));
3406   }
3407   case Intrinsic::arm_neon_vmins:
3408   case Intrinsic::arm_neon_vmaxs: {
3409     // v{min,max}s is overloaded between signed integers and floats.
3410     if (!Op.getValueType().isFloatingPoint()) {
3411       unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
3412         ? ISD::SMIN : ISD::SMAX;
3413       return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3414                          Op.getOperand(1), Op.getOperand(2));
3415     }
3416     unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
3417       ? ISD::FMINIMUM : ISD::FMAXIMUM;
3418     return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3419                        Op.getOperand(1), Op.getOperand(2));
3420   }
3421   case Intrinsic::arm_neon_vtbl1:
3422     return DAG.getNode(ARMISD::VTBL1, SDLoc(Op), Op.getValueType(),
3423                        Op.getOperand(1), Op.getOperand(2));
3424   case Intrinsic::arm_neon_vtbl2:
3425     return DAG.getNode(ARMISD::VTBL2, SDLoc(Op), Op.getValueType(),
3426                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3427   }
3428 }
3429 
3430 static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
3431                                  const ARMSubtarget *Subtarget) {
3432   SDLoc dl(Op);
3433   ConstantSDNode *SSIDNode = cast<ConstantSDNode>(Op.getOperand(2));
3434   auto SSID = static_cast<SyncScope::ID>(SSIDNode->getZExtValue());
3435   if (SSID == SyncScope::SingleThread)
3436     return Op;
3437 
3438   if (!Subtarget->hasDataBarrier()) {
3439     // Some ARMv6 cpus can support data barriers with an mcr instruction.
3440     // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
3441     // here.
3442     assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
3443            "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
3444     return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
3445                        DAG.getConstant(0, dl, MVT::i32));
3446   }
3447 
3448   ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
3449   AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
3450   ARM_MB::MemBOpt Domain = ARM_MB::ISH;
3451   if (Subtarget->isMClass()) {
3452     // Only a full system barrier exists in the M-class architectures.
3453     Domain = ARM_MB::SY;
3454   } else if (Subtarget->preferISHSTBarriers() &&
3455              Ord == AtomicOrdering::Release) {
3456     // Swift happens to implement ISHST barriers in a way that's compatible with
3457     // Release semantics but weaker than ISH so we'd be fools not to use
3458     // it. Beware: other processors probably don't!
3459     Domain = ARM_MB::ISHST;
3460   }
3461 
3462   return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
3463                      DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
3464                      DAG.getConstant(Domain, dl, MVT::i32));
3465 }
3466 
3467 static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
3468                              const ARMSubtarget *Subtarget) {
3469   // ARM pre v5TE and Thumb1 does not have preload instructions.
3470   if (!(Subtarget->isThumb2() ||
3471         (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
3472     // Just preserve the chain.
3473     return Op.getOperand(0);
3474 
3475   SDLoc dl(Op);
3476   unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
3477   if (!isRead &&
3478       (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
3479     // ARMv7 with MP extension has PLDW.
3480     return Op.getOperand(0);
3481 
3482   unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3483   if (Subtarget->isThumb()) {
3484     // Invert the bits.
3485     isRead = ~isRead & 1;
3486     isData = ~isData & 1;
3487   }
3488 
3489   return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
3490                      Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
3491                      DAG.getConstant(isData, dl, MVT::i32));
3492 }
3493 
3494 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
3495   MachineFunction &MF = DAG.getMachineFunction();
3496   ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
3497 
3498   // vastart just stores the address of the VarArgsFrameIndex slot into the
3499   // memory location argument.
3500   SDLoc dl(Op);
3501   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
3502   SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
3503   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
3504   return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
3505                       MachinePointerInfo(SV));
3506 }
3507 
3508 SDValue ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA,
3509                                                 CCValAssign &NextVA,
3510                                                 SDValue &Root,
3511                                                 SelectionDAG &DAG,
3512                                                 const SDLoc &dl) const {
3513   MachineFunction &MF = DAG.getMachineFunction();
3514   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3515 
3516   const TargetRegisterClass *RC;
3517   if (AFI->isThumb1OnlyFunction())
3518     RC = &ARM::tGPRRegClass;
3519   else
3520     RC = &ARM::GPRRegClass;
3521 
3522   // Transform the arguments stored in physical registers into virtual ones.
3523   unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
3524   SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
3525 
3526   SDValue ArgValue2;
3527   if (NextVA.isMemLoc()) {
3528     MachineFrameInfo &MFI = MF.getFrameInfo();
3529     int FI = MFI.CreateFixedObject(4, NextVA.getLocMemOffset(), true);
3530 
3531     // Create load node to retrieve arguments from the stack.
3532     SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3533     ArgValue2 = DAG.getLoad(
3534         MVT::i32, dl, Root, FIN,
3535         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
3536   } else {
3537     Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
3538     ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
3539   }
3540   if (!Subtarget->isLittle())
3541     std::swap (ArgValue, ArgValue2);
3542   return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
3543 }
3544 
3545 // The remaining GPRs hold either the beginning of variable-argument
3546 // data, or the beginning of an aggregate passed by value (usually
3547 // byval).  Either way, we allocate stack slots adjacent to the data
3548 // provided by our caller, and store the unallocated registers there.
3549 // If this is a variadic function, the va_list pointer will begin with
3550 // these values; otherwise, this reassembles a (byval) structure that
3551 // was split between registers and memory.
3552 // Return: The frame index registers were stored into.
3553 int ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
3554                                       const SDLoc &dl, SDValue &Chain,
3555                                       const Value *OrigArg,
3556                                       unsigned InRegsParamRecordIdx,
3557                                       int ArgOffset, unsigned ArgSize) const {
3558   // Currently, two use-cases possible:
3559   // Case #1. Non-var-args function, and we meet first byval parameter.
3560   //          Setup first unallocated register as first byval register;
3561   //          eat all remained registers
3562   //          (these two actions are performed by HandleByVal method).
3563   //          Then, here, we initialize stack frame with
3564   //          "store-reg" instructions.
3565   // Case #2. Var-args function, that doesn't contain byval parameters.
3566   //          The same: eat all remained unallocated registers,
3567   //          initialize stack frame.
3568 
3569   MachineFunction &MF = DAG.getMachineFunction();
3570   MachineFrameInfo &MFI = MF.getFrameInfo();
3571   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3572   unsigned RBegin, REnd;
3573   if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3574     CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
3575   } else {
3576     unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3577     RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
3578     REnd = ARM::R4;
3579   }
3580 
3581   if (REnd != RBegin)
3582     ArgOffset = -4 * (ARM::R4 - RBegin);
3583 
3584   auto PtrVT = getPointerTy(DAG.getDataLayout());
3585   int FrameIndex = MFI.CreateFixedObject(ArgSize, ArgOffset, false);
3586   SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
3587 
3588   SmallVector<SDValue, 4> MemOps;
3589   const TargetRegisterClass *RC =
3590       AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
3591 
3592   for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3593     unsigned VReg = MF.addLiveIn(Reg, RC);
3594     SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
3595     SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3596                                  MachinePointerInfo(OrigArg, 4 * i));
3597     MemOps.push_back(Store);
3598     FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
3599   }
3600 
3601   if (!MemOps.empty())
3602     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3603   return FrameIndex;
3604 }
3605 
3606 // Setup stack frame, the va_list pointer will start from.
3607 void ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
3608                                              const SDLoc &dl, SDValue &Chain,
3609                                              unsigned ArgOffset,
3610                                              unsigned TotalArgRegsSaveSize,
3611                                              bool ForceMutable) const {
3612   MachineFunction &MF = DAG.getMachineFunction();
3613   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3614 
3615   // Try to store any remaining integer argument regs
3616   // to their spots on the stack so that they may be loaded by dereferencing
3617   // the result of va_next.
3618   // If there is no regs to be stored, just point address after last
3619   // argument passed via stack.
3620   int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3621                                   CCInfo.getInRegsParamsCount(),
3622                                   CCInfo.getNextStackOffset(), 4);
3623   AFI->setVarArgsFrameIndex(FrameIndex);
3624 }
3625 
3626 SDValue ARMTargetLowering::LowerFormalArguments(
3627     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3628     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3629     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
3630   MachineFunction &MF = DAG.getMachineFunction();
3631   MachineFrameInfo &MFI = MF.getFrameInfo();
3632 
3633   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3634 
3635   // Assign locations to all of the incoming arguments.
3636   SmallVector<CCValAssign, 16> ArgLocs;
3637   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3638                  *DAG.getContext());
3639   CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForCall(CallConv, isVarArg));
3640 
3641   SmallVector<SDValue, 16> ArgValues;
3642   SDValue ArgValue;
3643   Function::const_arg_iterator CurOrigArg = MF.getFunction().arg_begin();
3644   unsigned CurArgIdx = 0;
3645 
3646   // Initially ArgRegsSaveSize is zero.
3647   // Then we increase this value each time we meet byval parameter.
3648   // We also increase this value in case of varargs function.
3649   AFI->setArgRegsSaveSize(0);
3650 
3651   // Calculate the amount of stack space that we need to allocate to store
3652   // byval and variadic arguments that are passed in registers.
3653   // We need to know this before we allocate the first byval or variadic
3654   // argument, as they will be allocated a stack slot below the CFA (Canonical
3655   // Frame Address, the stack pointer at entry to the function).
3656   unsigned ArgRegBegin = ARM::R4;
3657   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3658     if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3659       break;
3660 
3661     CCValAssign &VA = ArgLocs[i];
3662     unsigned Index = VA.getValNo();
3663     ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3664     if (!Flags.isByVal())
3665       continue;
3666 
3667     assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3668     unsigned RBegin, REnd;
3669     CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3670     ArgRegBegin = std::min(ArgRegBegin, RBegin);
3671 
3672     CCInfo.nextInRegsParam();
3673   }
3674   CCInfo.rewindByValRegsInfo();
3675 
3676   int lastInsIndex = -1;
3677   if (isVarArg && MFI.hasVAStart()) {
3678     unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3679     if (RegIdx != array_lengthof(GPRArgRegs))
3680       ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
3681   }
3682 
3683   unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3684   AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
3685   auto PtrVT = getPointerTy(DAG.getDataLayout());
3686 
3687   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3688     CCValAssign &VA = ArgLocs[i];
3689     if (Ins[VA.getValNo()].isOrigArg()) {
3690       std::advance(CurOrigArg,
3691                    Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3692       CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3693     }
3694     // Arguments stored in registers.
3695     if (VA.isRegLoc()) {
3696       EVT RegVT = VA.getLocVT();
3697 
3698       if (VA.needsCustom()) {
3699         // f64 and vector types are split up into multiple registers or
3700         // combinations of registers and stack slots.
3701         if (VA.getLocVT() == MVT::v2f64) {
3702           SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
3703                                                    Chain, DAG, dl);
3704           VA = ArgLocs[++i]; // skip ahead to next loc
3705           SDValue ArgValue2;
3706           if (VA.isMemLoc()) {
3707             int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), true);
3708             SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
3709             ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
3710                                     MachinePointerInfo::getFixedStack(
3711                                         DAG.getMachineFunction(), FI));
3712           } else {
3713             ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3714                                              Chain, DAG, dl);
3715           }
3716           ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3717           ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
3718                                  ArgValue, ArgValue1,
3719                                  DAG.getIntPtrConstant(0, dl));
3720           ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
3721                                  ArgValue, ArgValue2,
3722                                  DAG.getIntPtrConstant(1, dl));
3723         } else
3724           ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
3725       } else {
3726         const TargetRegisterClass *RC;
3727 
3728 
3729         if (RegVT == MVT::f16)
3730           RC = &ARM::HPRRegClass;
3731         else if (RegVT == MVT::f32)
3732           RC = &ARM::SPRRegClass;
3733         else if (RegVT == MVT::f64 || RegVT == MVT::v4f16)
3734           RC = &ARM::DPRRegClass;
3735         else if (RegVT == MVT::v2f64 || RegVT == MVT::v8f16)
3736           RC = &ARM::QPRRegClass;
3737         else if (RegVT == MVT::i32)
3738           RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3739                                            : &ARM::GPRRegClass;
3740         else
3741           llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
3742 
3743         // Transform the arguments in physical registers into virtual ones.
3744         unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
3745         ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
3746       }
3747 
3748       // If this is an 8 or 16-bit value, it is really passed promoted
3749       // to 32 bits.  Insert an assert[sz]ext to capture this, then
3750       // truncate to the right size.
3751       switch (VA.getLocInfo()) {
3752       default: llvm_unreachable("Unknown loc info!");
3753       case CCValAssign::Full: break;
3754       case CCValAssign::BCvt:
3755         ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
3756         break;
3757       case CCValAssign::SExt:
3758         ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3759                                DAG.getValueType(VA.getValVT()));
3760         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3761         break;
3762       case CCValAssign::ZExt:
3763         ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3764                                DAG.getValueType(VA.getValVT()));
3765         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3766         break;
3767       }
3768 
3769       InVals.push_back(ArgValue);
3770     } else { // VA.isRegLoc()
3771       // sanity check
3772       assert(VA.isMemLoc());
3773       assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
3774 
3775       int index = VA.getValNo();
3776 
3777       // Some Ins[] entries become multiple ArgLoc[] entries.
3778       // Process them only once.
3779       if (index != lastInsIndex)
3780         {
3781           ISD::ArgFlagsTy Flags = Ins[index].Flags;
3782           // FIXME: For now, all byval parameter objects are marked mutable.
3783           // This can be changed with more analysis.
3784           // In case of tail call optimization mark all arguments mutable.
3785           // Since they could be overwritten by lowering of arguments in case of
3786           // a tail call.
3787           if (Flags.isByVal()) {
3788             assert(Ins[index].isOrigArg() &&
3789                    "Byval arguments cannot be implicit");
3790             unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
3791 
3792             int FrameIndex = StoreByValRegs(
3793                 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3794                 VA.getLocMemOffset(), Flags.getByValSize());
3795             InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
3796             CCInfo.nextInRegsParam();
3797           } else {
3798             unsigned FIOffset = VA.getLocMemOffset();
3799             int FI = MFI.CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
3800                                            FIOffset, true);
3801 
3802             // Create load nodes to retrieve arguments from the stack.
3803             SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
3804             InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3805                                          MachinePointerInfo::getFixedStack(
3806                                              DAG.getMachineFunction(), FI)));
3807           }
3808           lastInsIndex = index;
3809         }
3810     }
3811   }
3812 
3813   // varargs
3814   if (isVarArg && MFI.hasVAStart())
3815     VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
3816                          CCInfo.getNextStackOffset(),
3817                          TotalArgRegsSaveSize);
3818 
3819   AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3820 
3821   return Chain;
3822 }
3823 
3824 /// isFloatingPointZero - Return true if this is +0.0.
3825 static bool isFloatingPointZero(SDValue Op) {
3826   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
3827     return CFP->getValueAPF().isPosZero();
3828   else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
3829     // Maybe this has already been legalized into the constant pool?
3830     if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
3831       SDValue WrapperOp = Op.getOperand(1).getOperand(0);
3832       if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
3833         if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
3834           return CFP->getValueAPF().isPosZero();
3835     }
3836   } else if (Op->getOpcode() == ISD::BITCAST &&
3837              Op->getValueType(0) == MVT::f64) {
3838     // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3839     // created by LowerConstantFP().
3840     SDValue BitcastOp = Op->getOperand(0);
3841     if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
3842         isNullConstant(BitcastOp->getOperand(0)))
3843       return true;
3844   }
3845   return false;
3846 }
3847 
3848 /// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3849 /// the given operands.
3850 SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
3851                                      SDValue &ARMcc, SelectionDAG &DAG,
3852                                      const SDLoc &dl) const {
3853   if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
3854     unsigned C = RHSC->getZExtValue();
3855     if (!isLegalICmpImmediate((int32_t)C)) {
3856       // Constant does not fit, try adjusting it by one.
3857       switch (CC) {
3858       default: break;
3859       case ISD::SETLT:
3860       case ISD::SETGE:
3861         if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
3862           CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
3863           RHS = DAG.getConstant(C - 1, dl, MVT::i32);
3864         }
3865         break;
3866       case ISD::SETULT:
3867       case ISD::SETUGE:
3868         if (C != 0 && isLegalICmpImmediate(C-1)) {
3869           CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
3870           RHS = DAG.getConstant(C - 1, dl, MVT::i32);
3871         }
3872         break;
3873       case ISD::SETLE:
3874       case ISD::SETGT:
3875         if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
3876           CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
3877           RHS = DAG.getConstant(C + 1, dl, MVT::i32);
3878         }
3879         break;
3880       case ISD::SETULE:
3881       case ISD::SETUGT:
3882         if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
3883           CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
3884           RHS = DAG.getConstant(C + 1, dl, MVT::i32);
3885         }
3886         break;
3887       }
3888     }
3889   } else if ((ARM_AM::getShiftOpcForNode(LHS.getOpcode()) != ARM_AM::no_shift) &&
3890              (ARM_AM::getShiftOpcForNode(RHS.getOpcode()) == ARM_AM::no_shift)) {
3891     // In ARM and Thumb-2, the compare instructions can shift their second
3892     // operand.
3893     CC = ISD::getSetCCSwappedOperands(CC);
3894     std::swap(LHS, RHS);
3895   }
3896 
3897   ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3898   ARMISD::NodeType CompareType;
3899   switch (CondCode) {
3900   default:
3901     CompareType = ARMISD::CMP;
3902     break;
3903   case ARMCC::EQ:
3904   case ARMCC::NE:
3905     // Uses only Z Flag
3906     CompareType = ARMISD::CMPZ;
3907     break;
3908   }
3909   ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
3910   return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
3911 }
3912 
3913 /// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
3914 SDValue ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS,
3915                                      SelectionDAG &DAG, const SDLoc &dl,
3916                                      bool InvalidOnQNaN) const {
3917   assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
3918   SDValue Cmp;
3919   SDValue C = DAG.getConstant(InvalidOnQNaN, dl, MVT::i32);
3920   if (!isFloatingPointZero(RHS))
3921     Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS, C);
3922   else
3923     Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS, C);
3924   return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
3925 }
3926 
3927 /// duplicateCmp - Glue values can have only one use, so this function
3928 /// duplicates a comparison node.
3929 SDValue
3930 ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3931   unsigned Opc = Cmp.getOpcode();
3932   SDLoc DL(Cmp);
3933   if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3934     return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3935 
3936   assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3937   Cmp = Cmp.getOperand(0);
3938   Opc = Cmp.getOpcode();
3939   if (Opc == ARMISD::CMPFP)
3940     Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),
3941                       Cmp.getOperand(1), Cmp.getOperand(2));
3942   else {
3943     assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3944     Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),
3945                       Cmp.getOperand(1));
3946   }
3947   return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3948 }
3949 
3950 // This function returns three things: the arithmetic computation itself
3951 // (Value), a comparison (OverflowCmp), and a condition code (ARMcc).  The
3952 // comparison and the condition code define the case in which the arithmetic
3953 // computation *does not* overflow.
3954 std::pair<SDValue, SDValue>
3955 ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3956                                  SDValue &ARMcc) const {
3957   assert(Op.getValueType() == MVT::i32 &&  "Unsupported value type");
3958 
3959   SDValue Value, OverflowCmp;
3960   SDValue LHS = Op.getOperand(0);
3961   SDValue RHS = Op.getOperand(1);
3962   SDLoc dl(Op);
3963 
3964   // FIXME: We are currently always generating CMPs because we don't support
3965   // generating CMN through the backend. This is not as good as the natural
3966   // CMP case because it causes a register dependency and cannot be folded
3967   // later.
3968 
3969   switch (Op.getOpcode()) {
3970   default:
3971     llvm_unreachable("Unknown overflow instruction!");
3972   case ISD::SADDO:
3973     ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3974     Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3975     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
3976     break;
3977   case ISD::UADDO:
3978     ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3979     // We use ADDC here to correspond to its use in LowerUnsignedALUO.
3980     // We do not use it in the USUBO case as Value may not be used.
3981     Value = DAG.getNode(ARMISD::ADDC, dl,
3982                         DAG.getVTList(Op.getValueType(), MVT::i32), LHS, RHS)
3983                 .getValue(0);
3984     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
3985     break;
3986   case ISD::SSUBO:
3987     ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3988     Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3989     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
3990     break;
3991   case ISD::USUBO:
3992     ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3993     Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3994     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
3995     break;
3996   case ISD::UMULO:
3997     // We generate a UMUL_LOHI and then check if the high word is 0.
3998     ARMcc = DAG.getConstant(ARMCC::EQ, dl, MVT::i32);
3999     Value = DAG.getNode(ISD::UMUL_LOHI, dl,
4000                         DAG.getVTList(Op.getValueType(), Op.getValueType()),
4001                         LHS, RHS);
4002     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value.getValue(1),
4003                               DAG.getConstant(0, dl, MVT::i32));
4004     Value = Value.getValue(0); // We only want the low 32 bits for the result.
4005     break;
4006   case ISD::SMULO:
4007     // We generate a SMUL_LOHI and then check if all the bits of the high word
4008     // are the same as the sign bit of the low word.
4009     ARMcc = DAG.getConstant(ARMCC::EQ, dl, MVT::i32);
4010     Value = DAG.getNode(ISD::SMUL_LOHI, dl,
4011                         DAG.getVTList(Op.getValueType(), Op.getValueType()),
4012                         LHS, RHS);
4013     OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value.getValue(1),
4014                               DAG.getNode(ISD::SRA, dl, Op.getValueType(),
4015                                           Value.getValue(0),
4016                                           DAG.getConstant(31, dl, MVT::i32)));
4017     Value = Value.getValue(0); // We only want the low 32 bits for the result.
4018     break;
4019   } // switch (...)
4020 
4021   return std::make_pair(Value, OverflowCmp);
4022 }
4023 
4024 SDValue
4025 ARMTargetLowering::LowerSignedALUO(SDValue Op, SelectionDAG &DAG) const {
4026   // Let legalize expand this if it isn't a legal type yet.
4027   if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
4028     return SDValue();
4029 
4030   SDValue Value, OverflowCmp;
4031   SDValue ARMcc;
4032   std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
4033   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4034   SDLoc dl(Op);
4035   // We use 0 and 1 as false and true values.
4036   SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
4037   SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
4038   EVT VT = Op.getValueType();
4039 
4040   SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
4041                                  ARMcc, CCR, OverflowCmp);
4042 
4043   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
4044   return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
4045 }
4046 
4047 static SDValue ConvertBooleanCarryToCarryFlag(SDValue BoolCarry,
4048                                               SelectionDAG &DAG) {
4049   SDLoc DL(BoolCarry);
4050   EVT CarryVT = BoolCarry.getValueType();
4051 
4052   // This converts the boolean value carry into the carry flag by doing
4053   // ARMISD::SUBC Carry, 1
4054   SDValue Carry = DAG.getNode(ARMISD::SUBC, DL,
4055                               DAG.getVTList(CarryVT, MVT::i32),
4056                               BoolCarry, DAG.getConstant(1, DL, CarryVT));
4057   return Carry.getValue(1);
4058 }
4059 
4060 static SDValue ConvertCarryFlagToBooleanCarry(SDValue Flags, EVT VT,
4061                                               SelectionDAG &DAG) {
4062   SDLoc DL(Flags);
4063 
4064   // Now convert the carry flag into a boolean carry. We do this
4065   // using ARMISD:ADDE 0, 0, Carry
4066   return DAG.getNode(ARMISD::ADDE, DL, DAG.getVTList(VT, MVT::i32),
4067                      DAG.getConstant(0, DL, MVT::i32),
4068                      DAG.getConstant(0, DL, MVT::i32), Flags);
4069 }
4070 
4071 SDValue ARMTargetLowering::LowerUnsignedALUO(SDValue Op,
4072                                              SelectionDAG &DAG) const {
4073   // Let legalize expand this if it isn't a legal type yet.
4074   if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
4075     return SDValue();
4076 
4077   SDValue LHS = Op.getOperand(0);
4078   SDValue RHS = Op.getOperand(1);
4079   SDLoc dl(Op);
4080 
4081   EVT VT = Op.getValueType();
4082   SDVTList VTs = DAG.getVTList(VT, MVT::i32);
4083   SDValue Value;
4084   SDValue Overflow;
4085   switch (Op.getOpcode()) {
4086   default:
4087     llvm_unreachable("Unknown overflow instruction!");
4088   case ISD::UADDO:
4089     Value = DAG.getNode(ARMISD::ADDC, dl, VTs, LHS, RHS);
4090     // Convert the carry flag into a boolean value.
4091     Overflow = ConvertCarryFlagToBooleanCarry(Value.getValue(1), VT, DAG);
4092     break;
4093   case ISD::USUBO: {
4094     Value = DAG.getNode(ARMISD::SUBC, dl, VTs, LHS, RHS);
4095     // Convert the carry flag into a boolean value.
4096     Overflow = ConvertCarryFlagToBooleanCarry(Value.getValue(1), VT, DAG);
4097     // ARMISD::SUBC returns 0 when we have to borrow, so make it an overflow
4098     // value. So compute 1 - C.
4099     Overflow = DAG.getNode(ISD::SUB, dl, MVT::i32,
4100                            DAG.getConstant(1, dl, MVT::i32), Overflow);
4101     break;
4102   }
4103   }
4104 
4105   return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
4106 }
4107 
4108 SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
4109   SDValue Cond = Op.getOperand(0);
4110   SDValue SelectTrue = Op.getOperand(1);
4111   SDValue SelectFalse = Op.getOperand(2);
4112   SDLoc dl(Op);
4113   unsigned Opc = Cond.getOpcode();
4114 
4115   if (Cond.getResNo() == 1 &&
4116       (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
4117        Opc == ISD::USUBO)) {
4118     if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
4119       return SDValue();
4120 
4121     SDValue Value, OverflowCmp;
4122     SDValue ARMcc;
4123     std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
4124     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4125     EVT VT = Op.getValueType();
4126 
4127     return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
4128                    OverflowCmp, DAG);
4129   }
4130 
4131   // Convert:
4132   //
4133   //   (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
4134   //   (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
4135   //
4136   if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
4137     const ConstantSDNode *CMOVTrue =
4138       dyn_cast<ConstantSDNode>(Cond.getOperand(0));
4139     const ConstantSDNode *CMOVFalse =
4140       dyn_cast<ConstantSDNode>(Cond.getOperand(1));
4141 
4142     if (CMOVTrue && CMOVFalse) {
4143       unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
4144       unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
4145 
4146       SDValue True;
4147       SDValue False;
4148       if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
4149         True = SelectTrue;
4150         False = SelectFalse;
4151       } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
4152         True = SelectFalse;
4153         False = SelectTrue;
4154       }
4155 
4156       if (True.getNode() && False.getNode()) {
4157         EVT VT = Op.getValueType();
4158         SDValue ARMcc = Cond.getOperand(2);
4159         SDValue CCR = Cond.getOperand(3);
4160         SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
4161         assert(True.getValueType() == VT);
4162         return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
4163       }
4164     }
4165   }
4166 
4167   // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
4168   // undefined bits before doing a full-word comparison with zero.
4169   Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
4170                      DAG.getConstant(1, dl, Cond.getValueType()));
4171 
4172   return DAG.getSelectCC(dl, Cond,
4173                          DAG.getConstant(0, dl, Cond.getValueType()),
4174                          SelectTrue, SelectFalse, ISD::SETNE);
4175 }
4176 
4177 static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
4178                                  bool &swpCmpOps, bool &swpVselOps) {
4179   // Start by selecting the GE condition code for opcodes that return true for
4180   // 'equality'
4181   if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
4182       CC == ISD::SETULE || CC == ISD::SETGE  || CC == ISD::SETLE)
4183     CondCode = ARMCC::GE;
4184 
4185   // and GT for opcodes that return false for 'equality'.
4186   else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
4187            CC == ISD::SETULT || CC == ISD::SETGT  || CC == ISD::SETLT)
4188     CondCode = ARMCC::GT;
4189 
4190   // Since we are constrained to GE/GT, if the opcode contains 'less', we need
4191   // to swap the compare operands.
4192   if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
4193       CC == ISD::SETULT || CC == ISD::SETLE  || CC == ISD::SETLT)
4194     swpCmpOps = true;
4195 
4196   // Both GT and GE are ordered comparisons, and return false for 'unordered'.
4197   // If we have an unordered opcode, we need to swap the operands to the VSEL
4198   // instruction (effectively negating the condition).
4199   //
4200   // This also has the effect of swapping which one of 'less' or 'greater'
4201   // returns true, so we also swap the compare operands. It also switches
4202   // whether we return true for 'equality', so we compensate by picking the
4203   // opposite condition code to our original choice.
4204   if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
4205       CC == ISD::SETUGT) {
4206     swpCmpOps = !swpCmpOps;
4207     swpVselOps = !swpVselOps;
4208     CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
4209   }
4210 
4211   // 'ordered' is 'anything but unordered', so use the VS condition code and
4212   // swap the VSEL operands.
4213   if (CC == ISD::SETO) {
4214     CondCode = ARMCC::VS;
4215     swpVselOps = true;
4216   }
4217 
4218   // 'unordered or not equal' is 'anything but equal', so use the EQ condition
4219   // code and swap the VSEL operands. Also do this if we don't care about the
4220   // unordered case.
4221   if (CC == ISD::SETUNE || CC == ISD::SETNE) {
4222     CondCode = ARMCC::EQ;
4223     swpVselOps = true;
4224   }
4225 }
4226 
4227 SDValue ARMTargetLowering::getCMOV(const SDLoc &dl, EVT VT, SDValue FalseVal,
4228                                    SDValue TrueVal, SDValue ARMcc, SDValue CCR,
4229                                    SDValue Cmp, SelectionDAG &DAG) const {
4230   if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
4231     FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
4232                            DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
4233     TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
4234                           DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
4235 
4236     SDValue TrueLow = TrueVal.getValue(0);
4237     SDValue TrueHigh = TrueVal.getValue(1);
4238     SDValue FalseLow = FalseVal.getValue(0);
4239     SDValue FalseHigh = FalseVal.getValue(1);
4240 
4241     SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
4242                               ARMcc, CCR, Cmp);
4243     SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
4244                                ARMcc, CCR, duplicateCmp(Cmp, DAG));
4245 
4246     return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
4247   } else {
4248     return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
4249                        Cmp);
4250   }
4251 }
4252 
4253 static bool isGTorGE(ISD::CondCode CC) {
4254   return CC == ISD::SETGT || CC == ISD::SETGE;
4255 }
4256 
4257 static bool isLTorLE(ISD::CondCode CC) {
4258   return CC == ISD::SETLT || CC == ISD::SETLE;
4259 }
4260 
4261 // See if a conditional (LHS CC RHS ? TrueVal : FalseVal) is lower-saturating.
4262 // All of these conditions (and their <= and >= counterparts) will do:
4263 //          x < k ? k : x
4264 //          x > k ? x : k
4265 //          k < x ? x : k
4266 //          k > x ? k : x
4267 static bool isLowerSaturate(const SDValue LHS, const SDValue RHS,
4268                             const SDValue TrueVal, const SDValue FalseVal,
4269                             const ISD::CondCode CC, const SDValue K) {
4270   return (isGTorGE(CC) &&
4271           ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))) ||
4272          (isLTorLE(CC) &&
4273           ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal)));
4274 }
4275 
4276 // Similar to isLowerSaturate(), but checks for upper-saturating conditions.
4277 static bool isUpperSaturate(const SDValue LHS, const SDValue RHS,
4278                             const SDValue TrueVal, const SDValue FalseVal,
4279                             const ISD::CondCode CC, const SDValue K) {
4280   return (isGTorGE(CC) &&
4281           ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))) ||
4282          (isLTorLE(CC) &&
4283           ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal)));
4284 }
4285 
4286 // Check if two chained conditionals could be converted into SSAT or USAT.
4287 //
4288 // SSAT can replace a set of two conditional selectors that bound a number to an
4289 // interval of type [k, ~k] when k + 1 is a power of 2. Here are some examples:
4290 //
4291 //     x < -k ? -k : (x > k ? k : x)
4292 //     x < -k ? -k : (x < k ? x : k)
4293 //     x > -k ? (x > k ? k : x) : -k
4294 //     x < k ? (x < -k ? -k : x) : k
4295 //     etc.
4296 //
4297 // USAT works similarily to SSAT but bounds on the interval [0, k] where k + 1 is
4298 // a power of 2.
4299 //
4300 // It returns true if the conversion can be done, false otherwise.
4301 // Additionally, the variable is returned in parameter V, the constant in K and
4302 // usat is set to true if the conditional represents an unsigned saturation
4303 static bool isSaturatingConditional(const SDValue &Op, SDValue &V,
4304                                     uint64_t &K, bool &usat) {
4305   SDValue LHS1 = Op.getOperand(0);
4306   SDValue RHS1 = Op.getOperand(1);
4307   SDValue TrueVal1 = Op.getOperand(2);
4308   SDValue FalseVal1 = Op.getOperand(3);
4309   ISD::CondCode CC1 = cast<CondCodeSDNode>(Op.getOperand(4))->get();
4310 
4311   const SDValue Op2 = isa<ConstantSDNode>(TrueVal1) ? FalseVal1 : TrueVal1;
4312   if (Op2.getOpcode() != ISD::SELECT_CC)
4313     return false;
4314 
4315   SDValue LHS2 = Op2.getOperand(0);
4316   SDValue RHS2 = Op2.getOperand(1);
4317   SDValue TrueVal2 = Op2.getOperand(2);
4318   SDValue FalseVal2 = Op2.getOperand(3);
4319   ISD::CondCode CC2 = cast<CondCodeSDNode>(Op2.getOperand(4))->get();
4320 
4321   // Find out which are the constants and which are the variables
4322   // in each conditional
4323   SDValue *K1 = isa<ConstantSDNode>(LHS1) ? &LHS1 : isa<ConstantSDNode>(RHS1)
4324                                                         ? &RHS1
4325                                                         : nullptr;
4326   SDValue *K2 = isa<ConstantSDNode>(LHS2) ? &LHS2 : isa<ConstantSDNode>(RHS2)
4327                                                         ? &RHS2
4328                                                         : nullptr;
4329   SDValue K2Tmp = isa<ConstantSDNode>(TrueVal2) ? TrueVal2 : FalseVal2;
4330   SDValue V1Tmp = (K1 && *K1 == LHS1) ? RHS1 : LHS1;
4331   SDValue V2Tmp = (K2 && *K2 == LHS2) ? RHS2 : LHS2;
4332   SDValue V2 = (K2Tmp == TrueVal2) ? FalseVal2 : TrueVal2;
4333 
4334   // We must detect cases where the original operations worked with 16- or
4335   // 8-bit values. In such case, V2Tmp != V2 because the comparison operations
4336   // must work with sign-extended values but the select operations return
4337   // the original non-extended value.
4338   SDValue V2TmpReg = V2Tmp;
4339   if (V2Tmp->getOpcode() == ISD::SIGN_EXTEND_INREG)
4340     V2TmpReg = V2Tmp->getOperand(0);
4341 
4342   // Check that the registers and the constants have the correct values
4343   // in both conditionals
4344   if (!K1 || !K2 || *K1 == Op2 || *K2 != K2Tmp || V1Tmp != V2Tmp ||
4345       V2TmpReg != V2)
4346     return false;
4347 
4348   // Figure out which conditional is saturating the lower/upper bound.
4349   const SDValue *LowerCheckOp =
4350       isLowerSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1)
4351           ? &Op
4352           : isLowerSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2)
4353                 ? &Op2
4354                 : nullptr;
4355   const SDValue *UpperCheckOp =
4356       isUpperSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1)
4357           ? &Op
4358           : isUpperSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2)
4359                 ? &Op2
4360                 : nullptr;
4361 
4362   if (!UpperCheckOp || !LowerCheckOp || LowerCheckOp == UpperCheckOp)
4363     return false;
4364 
4365   // Check that the constant in the lower-bound check is
4366   // the opposite of the constant in the upper-bound check
4367   // in 1's complement.
4368   int64_t Val1 = cast<ConstantSDNode>(*K1)->getSExtValue();
4369   int64_t Val2 = cast<ConstantSDNode>(*K2)->getSExtValue();
4370   int64_t PosVal = std::max(Val1, Val2);
4371   int64_t NegVal = std::min(Val1, Val2);
4372 
4373   if (((Val1 > Val2 && UpperCheckOp == &Op) ||
4374        (Val1 < Val2 && UpperCheckOp == &Op2)) &&
4375       isPowerOf2_64(PosVal + 1)) {
4376 
4377     // Handle the difference between USAT (unsigned) and SSAT (signed) saturation
4378     if (Val1 == ~Val2)
4379       usat = false;
4380     else if (NegVal == 0)
4381       usat = true;
4382     else
4383       return false;
4384 
4385     V = V2;
4386     K = (uint64_t)PosVal; // At this point, PosVal is guaranteed to be positive
4387 
4388     return true;
4389   }
4390 
4391   return false;
4392 }
4393 
4394 // Check if a condition of the type x < k ? k : x can be converted into a
4395 // bit operation instead of conditional moves.
4396 // Currently this is allowed given:
4397 // - The conditions and values match up
4398 // - k is 0 or -1 (all ones)
4399 // This function will not check the last condition, thats up to the caller
4400 // It returns true if the transformation can be made, and in such case
4401 // returns x in V, and k in SatK.
4402 static bool isLowerSaturatingConditional(const SDValue &Op, SDValue &V,
4403                                          SDValue &SatK)
4404 {
4405   SDValue LHS = Op.getOperand(0);
4406   SDValue RHS = Op.getOperand(1);
4407   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
4408   SDValue TrueVal = Op.getOperand(2);
4409   SDValue FalseVal = Op.getOperand(3);
4410 
4411   SDValue *K = isa<ConstantSDNode>(LHS) ? &LHS : isa<ConstantSDNode>(RHS)
4412                                                ? &RHS
4413                                                : nullptr;
4414 
4415   // No constant operation in comparison, early out
4416   if (!K)
4417     return false;
4418 
4419   SDValue KTmp = isa<ConstantSDNode>(TrueVal) ? TrueVal : FalseVal;
4420   V = (KTmp == TrueVal) ? FalseVal : TrueVal;
4421   SDValue VTmp = (K && *K == LHS) ? RHS : LHS;
4422 
4423   // If the constant on left and right side, or variable on left and right,
4424   // does not match, early out
4425   if (*K != KTmp || V != VTmp)
4426     return false;
4427 
4428   if (isLowerSaturate(LHS, RHS, TrueVal, FalseVal, CC, *K)) {
4429     SatK = *K;
4430     return true;
4431   }
4432 
4433   return false;
4434 }
4435 
4436 SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
4437   EVT VT = Op.getValueType();
4438   SDLoc dl(Op);
4439 
4440   // Try to convert two saturating conditional selects into a single SSAT
4441   SDValue SatValue;
4442   uint64_t SatConstant;
4443   bool SatUSat;
4444   if (((!Subtarget->isThumb() && Subtarget->hasV6Ops()) || Subtarget->isThumb2()) &&
4445       isSaturatingConditional(Op, SatValue, SatConstant, SatUSat)) {
4446     if (SatUSat)
4447       return DAG.getNode(ARMISD::USAT, dl, VT, SatValue,
4448                          DAG.getConstant(countTrailingOnes(SatConstant), dl, VT));
4449     else
4450       return DAG.getNode(ARMISD::SSAT, dl, VT, SatValue,
4451                          DAG.getConstant(countTrailingOnes(SatConstant), dl, VT));
4452   }
4453 
4454   // Try to convert expressions of the form x < k ? k : x (and similar forms)
4455   // into more efficient bit operations, which is possible when k is 0 or -1
4456   // On ARM and Thumb-2 which have flexible operand 2 this will result in
4457   // single instructions. On Thumb the shift and the bit operation will be two
4458   // instructions.
4459   // Only allow this transformation on full-width (32-bit) operations
4460   SDValue LowerSatConstant;
4461   if (VT == MVT::i32 &&
4462       isLowerSaturatingConditional(Op, SatValue, LowerSatConstant)) {
4463     SDValue ShiftV = DAG.getNode(ISD::SRA, dl, VT, SatValue,
4464                                  DAG.getConstant(31, dl, VT));
4465     if (isNullConstant(LowerSatConstant)) {
4466       SDValue NotShiftV = DAG.getNode(ISD::XOR, dl, VT, ShiftV,
4467                                       DAG.getAllOnesConstant(dl, VT));
4468       return DAG.getNode(ISD::AND, dl, VT, SatValue, NotShiftV);
4469     } else if (isAllOnesConstant(LowerSatConstant))
4470       return DAG.getNode(ISD::OR, dl, VT, SatValue, ShiftV);
4471   }
4472 
4473   SDValue LHS = Op.getOperand(0);
4474   SDValue RHS = Op.getOperand(1);
4475   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
4476   SDValue TrueVal = Op.getOperand(2);
4477   SDValue FalseVal = Op.getOperand(3);
4478 
4479   if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
4480     DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
4481                                                     dl);
4482 
4483     // If softenSetCCOperands only returned one value, we should compare it to
4484     // zero.
4485     if (!RHS.getNode()) {
4486       RHS = DAG.getConstant(0, dl, LHS.getValueType());
4487       CC = ISD::SETNE;
4488     }
4489   }
4490 
4491   if (LHS.getValueType() == MVT::i32) {
4492     // Try to generate VSEL on ARMv8.
4493     // The VSEL instruction can't use all the usual ARM condition
4494     // codes: it only has two bits to select the condition code, so it's
4495     // constrained to use only GE, GT, VS and EQ.
4496     //
4497     // To implement all the various ISD::SETXXX opcodes, we sometimes need to
4498     // swap the operands of the previous compare instruction (effectively
4499     // inverting the compare condition, swapping 'less' and 'greater') and
4500     // sometimes need to swap the operands to the VSEL (which inverts the
4501     // condition in the sense of firing whenever the previous condition didn't)
4502     if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f16 ||
4503                                     TrueVal.getValueType() == MVT::f32 ||
4504                                     TrueVal.getValueType() == MVT::f64)) {
4505       ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
4506       if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
4507           CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
4508         CC = ISD::getSetCCInverse(CC, true);
4509         std::swap(TrueVal, FalseVal);
4510       }
4511     }
4512 
4513     SDValue ARMcc;
4514     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4515     SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
4516     return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
4517   }
4518 
4519   ARMCC::CondCodes CondCode, CondCode2;
4520   bool InvalidOnQNaN;
4521   FPCCToARMCC(CC, CondCode, CondCode2, InvalidOnQNaN);
4522 
4523   // Normalize the fp compare. If RHS is zero we prefer to keep it there so we
4524   // match CMPFPw0 instead of CMPFP, though we don't do this for f16 because we
4525   // must use VSEL (limited condition codes), due to not having conditional f16
4526   // moves.
4527   if (Subtarget->hasFPARMv8() &&
4528       !(isFloatingPointZero(RHS) && TrueVal.getValueType() != MVT::f16) &&
4529       (TrueVal.getValueType() == MVT::f16 ||
4530        TrueVal.getValueType() == MVT::f32 ||
4531        TrueVal.getValueType() == MVT::f64)) {
4532     bool swpCmpOps = false;
4533     bool swpVselOps = false;
4534     checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
4535 
4536     if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
4537         CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
4538       if (swpCmpOps)
4539         std::swap(LHS, RHS);
4540       if (swpVselOps)
4541         std::swap(TrueVal, FalseVal);
4542     }
4543   }
4544 
4545   SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
4546   SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN);
4547   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4548   SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
4549   if (CondCode2 != ARMCC::AL) {
4550     SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
4551     // FIXME: Needs another CMP because flag can have but one use.
4552     SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN);
4553     Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
4554   }
4555   return Result;
4556 }
4557 
4558 /// canChangeToInt - Given the fp compare operand, return true if it is suitable
4559 /// to morph to an integer compare sequence.
4560 static bool canChangeToInt(SDValue Op, bool &SeenZero,
4561                            const ARMSubtarget *Subtarget) {
4562   SDNode *N = Op.getNode();
4563   if (!N->hasOneUse())
4564     // Otherwise it requires moving the value from fp to integer registers.
4565     return false;
4566   if (!N->getNumValues())
4567     return false;
4568   EVT VT = Op.getValueType();
4569   if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
4570     // f32 case is generally profitable. f64 case only makes sense when vcmpe +
4571     // vmrs are very slow, e.g. cortex-a8.
4572     return false;
4573 
4574   if (isFloatingPointZero(Op)) {
4575     SeenZero = true;
4576     return true;
4577   }
4578   return ISD::isNormalLoad(N);
4579 }
4580 
4581 static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
4582   if (isFloatingPointZero(Op))
4583     return DAG.getConstant(0, SDLoc(Op), MVT::i32);
4584 
4585   if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
4586     return DAG.getLoad(MVT::i32, SDLoc(Op), Ld->getChain(), Ld->getBasePtr(),
4587                        Ld->getPointerInfo(), Ld->getAlignment(),
4588                        Ld->getMemOperand()->getFlags());
4589 
4590   llvm_unreachable("Unknown VFP cmp argument!");
4591 }
4592 
4593 static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
4594                            SDValue &RetVal1, SDValue &RetVal2) {
4595   SDLoc dl(Op);
4596 
4597   if (isFloatingPointZero(Op)) {
4598     RetVal1 = DAG.getConstant(0, dl, MVT::i32);
4599     RetVal2 = DAG.getConstant(0, dl, MVT::i32);
4600     return;
4601   }
4602 
4603   if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
4604     SDValue Ptr = Ld->getBasePtr();
4605     RetVal1 =
4606         DAG.getLoad(MVT::i32, dl, Ld->getChain(), Ptr, Ld->getPointerInfo(),
4607                     Ld->getAlignment(), Ld->getMemOperand()->getFlags());
4608 
4609     EVT PtrType = Ptr.getValueType();
4610     unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
4611     SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
4612                                  PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
4613     RetVal2 = DAG.getLoad(MVT::i32, dl, Ld->getChain(), NewPtr,
4614                           Ld->getPointerInfo().getWithOffset(4), NewAlign,
4615                           Ld->getMemOperand()->getFlags());
4616     return;
4617   }
4618 
4619   llvm_unreachable("Unknown VFP cmp argument!");
4620 }
4621 
4622 /// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
4623 /// f32 and even f64 comparisons to integer ones.
4624 SDValue
4625 ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
4626   SDValue Chain = Op.getOperand(0);
4627   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
4628   SDValue LHS = Op.getOperand(2);
4629   SDValue RHS = Op.getOperand(3);
4630   SDValue Dest = Op.getOperand(4);
4631   SDLoc dl(Op);
4632 
4633   bool LHSSeenZero = false;
4634   bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
4635   bool RHSSeenZero = false;
4636   bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
4637   if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
4638     // If unsafe fp math optimization is enabled and there are no other uses of
4639     // the CMP operands, and the condition code is EQ or NE, we can optimize it
4640     // to an integer comparison.
4641     if (CC == ISD::SETOEQ)
4642       CC = ISD::SETEQ;
4643     else if (CC == ISD::SETUNE)
4644       CC = ISD::SETNE;
4645 
4646     SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
4647     SDValue ARMcc;
4648     if (LHS.getValueType() == MVT::f32) {
4649       LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
4650                         bitcastf32Toi32(LHS, DAG), Mask);
4651       RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
4652                         bitcastf32Toi32(RHS, DAG), Mask);
4653       SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
4654       SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4655       return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
4656                          Chain, Dest, ARMcc, CCR, Cmp);
4657     }
4658 
4659     SDValue LHS1, LHS2;
4660     SDValue RHS1, RHS2;
4661     expandf64Toi32(LHS, DAG, LHS1, LHS2);
4662     expandf64Toi32(RHS, DAG, RHS1, RHS2);
4663     LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
4664     RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
4665     ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
4666     ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
4667     SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
4668     SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
4669     return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
4670   }
4671 
4672   return SDValue();
4673 }
4674 
4675 SDValue ARMTargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
4676   SDValue Chain = Op.getOperand(0);
4677   SDValue Cond = Op.getOperand(1);
4678   SDValue Dest = Op.getOperand(2);
4679   SDLoc dl(Op);
4680 
4681   // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch
4682   // instruction.
4683   unsigned Opc = Cond.getOpcode();
4684   bool OptimizeMul = (Opc == ISD::SMULO || Opc == ISD::UMULO) &&
4685                       !Subtarget->isThumb1Only();
4686   if (Cond.getResNo() == 1 &&
4687       (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
4688        Opc == ISD::USUBO || OptimizeMul)) {
4689     // Only lower legal XALUO ops.
4690     if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
4691       return SDValue();
4692 
4693     // The actual operation with overflow check.
4694     SDValue Value, OverflowCmp;
4695     SDValue ARMcc;
4696     std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
4697 
4698     // Reverse the condition code.
4699     ARMCC::CondCodes CondCode =
4700         (ARMCC::CondCodes)cast<const ConstantSDNode>(ARMcc)->getZExtValue();
4701     CondCode = ARMCC::getOppositeCondition(CondCode);
4702     ARMcc = DAG.getConstant(CondCode, SDLoc(ARMcc), MVT::i32);
4703     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4704 
4705     return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, Chain, Dest, ARMcc, CCR,
4706                        OverflowCmp);
4707   }
4708 
4709   return SDValue();
4710 }
4711 
4712 SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
4713   SDValue Chain = Op.getOperand(0);
4714   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
4715   SDValue LHS = Op.getOperand(2);
4716   SDValue RHS = Op.getOperand(3);
4717   SDValue Dest = Op.getOperand(4);
4718   SDLoc dl(Op);
4719 
4720   if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
4721     DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
4722                                                     dl);
4723 
4724     // If softenSetCCOperands only returned one value, we should compare it to
4725     // zero.
4726     if (!RHS.getNode()) {
4727       RHS = DAG.getConstant(0, dl, LHS.getValueType());
4728       CC = ISD::SETNE;
4729     }
4730   }
4731 
4732   // Optimize {s|u}{add|sub|mul}.with.overflow feeding into a branch
4733   // instruction.
4734   unsigned Opc = LHS.getOpcode();
4735   bool OptimizeMul = (Opc == ISD::SMULO || Opc == ISD::UMULO) &&
4736                       !Subtarget->isThumb1Only();
4737   if (LHS.getResNo() == 1 && (isOneConstant(RHS) || isNullConstant(RHS)) &&
4738       (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
4739        Opc == ISD::USUBO || OptimizeMul) &&
4740       (CC == ISD::SETEQ || CC == ISD::SETNE)) {
4741     // Only lower legal XALUO ops.
4742     if (!DAG.getTargetLoweringInfo().isTypeLegal(LHS->getValueType(0)))
4743       return SDValue();
4744 
4745     // The actual operation with overflow check.
4746     SDValue Value, OverflowCmp;
4747     SDValue ARMcc;
4748     std::tie(Value, OverflowCmp) = getARMXALUOOp(LHS.getValue(0), DAG, ARMcc);
4749 
4750     if ((CC == ISD::SETNE) != isOneConstant(RHS)) {
4751       // Reverse the condition code.
4752       ARMCC::CondCodes CondCode =
4753           (ARMCC::CondCodes)cast<const ConstantSDNode>(ARMcc)->getZExtValue();
4754       CondCode = ARMCC::getOppositeCondition(CondCode);
4755       ARMcc = DAG.getConstant(CondCode, SDLoc(ARMcc), MVT::i32);
4756     }
4757     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4758 
4759     return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other, Chain, Dest, ARMcc, CCR,
4760                        OverflowCmp);
4761   }
4762 
4763   if (LHS.getValueType() == MVT::i32) {
4764     SDValue ARMcc;
4765     SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
4766     SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4767     return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
4768                        Chain, Dest, ARMcc, CCR, Cmp);
4769   }
4770 
4771   if (getTargetMachine().Options.UnsafeFPMath &&
4772       (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
4773        CC == ISD::SETNE || CC == ISD::SETUNE)) {
4774     if (SDValue Result = OptimizeVFPBrcond(Op, DAG))
4775       return Result;
4776   }
4777 
4778   ARMCC::CondCodes CondCode, CondCode2;
4779   bool InvalidOnQNaN;
4780   FPCCToARMCC(CC, CondCode, CondCode2, InvalidOnQNaN);
4781 
4782   SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
4783   SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl, InvalidOnQNaN);
4784   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4785   SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
4786   SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
4787   SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
4788   if (CondCode2 != ARMCC::AL) {
4789     ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
4790     SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
4791     Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
4792   }
4793   return Res;
4794 }
4795 
4796 SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
4797   SDValue Chain = Op.getOperand(0);
4798   SDValue Table = Op.getOperand(1);
4799   SDValue Index = Op.getOperand(2);
4800   SDLoc dl(Op);
4801 
4802   EVT PTy = getPointerTy(DAG.getDataLayout());
4803   JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
4804   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
4805   Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
4806   Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
4807   SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Table, Index);
4808   if (Subtarget->isThumb2() || (Subtarget->hasV8MBaselineOps() && Subtarget->isThumb())) {
4809     // Thumb2 and ARMv8-M use a two-level jump. That is, it jumps into the jump table
4810     // which does another jump to the destination. This also makes it easier
4811     // to translate it to TBB / TBH later (Thumb2 only).
4812     // FIXME: This might not work if the function is extremely large.
4813     return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
4814                        Addr, Op.getOperand(2), JTI);
4815   }
4816   if (isPositionIndependent() || Subtarget->isROPI()) {
4817     Addr =
4818         DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
4819                     MachinePointerInfo::getJumpTable(DAG.getMachineFunction()));
4820     Chain = Addr.getValue(1);
4821     Addr = DAG.getNode(ISD::ADD, dl, PTy, Table, Addr);
4822     return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
4823   } else {
4824     Addr =
4825         DAG.getLoad(PTy, dl, Chain, Addr,
4826                     MachinePointerInfo::getJumpTable(DAG.getMachineFunction()));
4827     Chain = Addr.getValue(1);
4828     return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
4829   }
4830 }
4831 
4832 static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
4833   EVT VT = Op.getValueType();
4834   SDLoc dl(Op);
4835 
4836   if (Op.getValueType().getVectorElementType() == MVT::i32) {
4837     if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
4838       return Op;
4839     return DAG.UnrollVectorOp(Op.getNode());
4840   }
4841 
4842   const bool HasFullFP16 =
4843     static_cast<const ARMSubtarget&>(DAG.getSubtarget()).hasFullFP16();
4844 
4845   EVT NewTy;
4846   const EVT OpTy = Op.getOperand(0).getValueType();
4847   if (OpTy == MVT::v4f32)
4848     NewTy = MVT::v4i32;
4849   else if (OpTy == MVT::v4f16 && HasFullFP16)
4850     NewTy = MVT::v4i16;
4851   else if (OpTy == MVT::v8f16 && HasFullFP16)
4852     NewTy = MVT::v8i16;
4853   else
4854     llvm_unreachable("Invalid type for custom lowering!");
4855 
4856   if (VT != MVT::v4i16 && VT != MVT::v8i16)
4857     return DAG.UnrollVectorOp(Op.getNode());
4858 
4859   Op = DAG.getNode(Op.getOpcode(), dl, NewTy, Op.getOperand(0));
4860   return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
4861 }
4862 
4863 SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
4864   EVT VT = Op.getValueType();
4865   if (VT.isVector())
4866     return LowerVectorFP_TO_INT(Op, DAG);
4867   if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
4868     RTLIB::Libcall LC;
4869     if (Op.getOpcode() == ISD::FP_TO_SINT)
4870       LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
4871                               Op.getValueType());
4872     else
4873       LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
4874                               Op.getValueType());
4875     return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
4876                        /*isSigned*/ false, SDLoc(Op)).first;
4877   }
4878 
4879   return Op;
4880 }
4881 
4882 static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4883   EVT VT = Op.getValueType();
4884   SDLoc dl(Op);
4885 
4886   if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
4887     if (VT.getVectorElementType() == MVT::f32)
4888       return Op;
4889     return DAG.UnrollVectorOp(Op.getNode());
4890   }
4891 
4892   assert((Op.getOperand(0).getValueType() == MVT::v4i16 ||
4893           Op.getOperand(0).getValueType() == MVT::v8i16) &&
4894          "Invalid type for custom lowering!");
4895 
4896   const bool HasFullFP16 =
4897     static_cast<const ARMSubtarget&>(DAG.getSubtarget()).hasFullFP16();
4898 
4899   EVT DestVecType;
4900   if (VT == MVT::v4f32)
4901     DestVecType = MVT::v4i32;
4902   else if (VT == MVT::v4f16 && HasFullFP16)
4903     DestVecType = MVT::v4i16;
4904   else if (VT == MVT::v8f16 && HasFullFP16)
4905     DestVecType = MVT::v8i16;
4906   else
4907     return DAG.UnrollVectorOp(Op.getNode());
4908 
4909   unsigned CastOpc;
4910   unsigned Opc;
4911   switch (Op.getOpcode()) {
4912   default: llvm_unreachable("Invalid opcode!");
4913   case ISD::SINT_TO_FP:
4914     CastOpc = ISD::SIGN_EXTEND;
4915     Opc = ISD::SINT_TO_FP;
4916     break;
4917   case ISD::UINT_TO_FP:
4918     CastOpc = ISD::ZERO_EXTEND;
4919     Opc = ISD::UINT_TO_FP;
4920     break;
4921   }
4922 
4923   Op = DAG.getNode(CastOpc, dl, DestVecType, Op.getOperand(0));
4924   return DAG.getNode(Opc, dl, VT, Op);
4925 }
4926 
4927 SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
4928   EVT VT = Op.getValueType();
4929   if (VT.isVector())
4930     return LowerVectorINT_TO_FP(Op, DAG);
4931   if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
4932     RTLIB::Libcall LC;
4933     if (Op.getOpcode() == ISD::SINT_TO_FP)
4934       LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4935                               Op.getValueType());
4936     else
4937       LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4938                               Op.getValueType());
4939     return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
4940                        /*isSigned*/ false, SDLoc(Op)).first;
4941   }
4942 
4943   return Op;
4944 }
4945 
4946 SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
4947   // Implement fcopysign with a fabs and a conditional fneg.
4948   SDValue Tmp0 = Op.getOperand(0);
4949   SDValue Tmp1 = Op.getOperand(1);
4950   SDLoc dl(Op);
4951   EVT VT = Op.getValueType();
4952   EVT SrcVT = Tmp1.getValueType();
4953   bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4954     Tmp0.getOpcode() == ARMISD::VMOVDRR;
4955   bool UseNEON = !InGPR && Subtarget->hasNEON();
4956 
4957   if (UseNEON) {
4958     // Use VBSL to copy the sign bit.
4959     unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4960     SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
4961                                DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
4962     EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4963     if (VT == MVT::f64)
4964       Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4965                          DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
4966                          DAG.getConstant(32, dl, MVT::i32));
4967     else /*if (VT == MVT::f32)*/
4968       Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4969     if (SrcVT == MVT::f32) {
4970       Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4971       if (VT == MVT::f64)
4972         Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4973                            DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
4974                            DAG.getConstant(32, dl, MVT::i32));
4975     } else if (VT == MVT::f32)
4976       Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4977                          DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
4978                          DAG.getConstant(32, dl, MVT::i32));
4979     Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4980     Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4981 
4982     SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
4983                                             dl, MVT::i32);
4984     AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4985     SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4986                                   DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
4987 
4988     SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4989                               DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4990                               DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
4991     if (VT == MVT::f32) {
4992       Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4993       Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
4994                         DAG.getConstant(0, dl, MVT::i32));
4995     } else {
4996       Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4997     }
4998 
4999     return Res;
5000   }
5001 
5002   // Bitcast operand 1 to i32.
5003   if (SrcVT == MVT::f64)
5004     Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
5005                        Tmp1).getValue(1);
5006   Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
5007 
5008   // Or in the signbit with integer operations.
5009   SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
5010   SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
5011   Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
5012   if (VT == MVT::f32) {
5013     Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
5014                        DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
5015     return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5016                        DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
5017   }
5018 
5019   // f64: Or the high part with signbit and then combine two parts.
5020   Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
5021                      Tmp0);
5022   SDValue Lo = Tmp0.getValue(0);
5023   SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
5024   Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
5025   return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
5026 }
5027 
5028 SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
5029   MachineFunction &MF = DAG.getMachineFunction();
5030   MachineFrameInfo &MFI = MF.getFrameInfo();
5031   MFI.setReturnAddressIsTaken(true);
5032 
5033   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
5034     return SDValue();
5035 
5036   EVT VT = Op.getValueType();
5037   SDLoc dl(Op);
5038   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5039   if (Depth) {
5040     SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
5041     SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
5042     return DAG.getLoad(VT, dl, DAG.getEntryNode(),
5043                        DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
5044                        MachinePointerInfo());
5045   }
5046 
5047   // Return LR, which contains the return address. Mark it an implicit live-in.
5048   unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
5049   return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
5050 }
5051 
5052 SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
5053   const ARMBaseRegisterInfo &ARI =
5054     *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
5055   MachineFunction &MF = DAG.getMachineFunction();
5056   MachineFrameInfo &MFI = MF.getFrameInfo();
5057   MFI.setFrameAddressIsTaken(true);
5058 
5059   EVT VT = Op.getValueType();
5060   SDLoc dl(Op);  // FIXME probably not meaningful
5061   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5062   unsigned FrameReg = ARI.getFrameRegister(MF);
5063   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
5064   while (Depth--)
5065     FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
5066                             MachinePointerInfo());
5067   return FrameAddr;
5068 }
5069 
5070 // FIXME? Maybe this could be a TableGen attribute on some registers and
5071 // this table could be generated automatically from RegInfo.
5072 unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
5073                                               SelectionDAG &DAG) const {
5074   unsigned Reg = StringSwitch<unsigned>(RegName)
5075                        .Case("sp", ARM::SP)
5076                        .Default(0);
5077   if (Reg)
5078     return Reg;
5079   report_fatal_error(Twine("Invalid register name \""
5080                               + StringRef(RegName)  + "\"."));
5081 }
5082 
5083 // Result is 64 bit value so split into two 32 bit values and return as a
5084 // pair of values.
5085 static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
5086                                 SelectionDAG &DAG) {
5087   SDLoc DL(N);
5088 
5089   // This function is only supposed to be called for i64 type destination.
5090   assert(N->getValueType(0) == MVT::i64
5091           && "ExpandREAD_REGISTER called for non-i64 type result.");
5092 
5093   SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
5094                              DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
5095                              N->getOperand(0),
5096                              N->getOperand(1));
5097 
5098   Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
5099                     Read.getValue(1)));
5100   Results.push_back(Read.getOperand(0));
5101 }
5102 
5103 /// \p BC is a bitcast that is about to be turned into a VMOVDRR.
5104 /// When \p DstVT, the destination type of \p BC, is on the vector
5105 /// register bank and the source of bitcast, \p Op, operates on the same bank,
5106 /// it might be possible to combine them, such that everything stays on the
5107 /// vector register bank.
5108 /// \p return The node that would replace \p BT, if the combine
5109 /// is possible.
5110 static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC,
5111                                                 SelectionDAG &DAG) {
5112   SDValue Op = BC->getOperand(0);
5113   EVT DstVT = BC->getValueType(0);
5114 
5115   // The only vector instruction that can produce a scalar (remember,
5116   // since the bitcast was about to be turned into VMOVDRR, the source
5117   // type is i64) from a vector is EXTRACT_VECTOR_ELT.
5118   // Moreover, we can do this combine only if there is one use.
5119   // Finally, if the destination type is not a vector, there is not
5120   // much point on forcing everything on the vector bank.
5121   if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5122       !Op.hasOneUse())
5123     return SDValue();
5124 
5125   // If the index is not constant, we will introduce an additional
5126   // multiply that will stick.
5127   // Give up in that case.
5128   ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5129   if (!Index)
5130     return SDValue();
5131   unsigned DstNumElt = DstVT.getVectorNumElements();
5132 
5133   // Compute the new index.
5134   const APInt &APIntIndex = Index->getAPIntValue();
5135   APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt);
5136   NewIndex *= APIntIndex;
5137   // Check if the new constant index fits into i32.
5138   if (NewIndex.getBitWidth() > 32)
5139     return SDValue();
5140 
5141   // vMTy bitcast(i64 extractelt vNi64 src, i32 index) ->
5142   // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M)
5143   SDLoc dl(Op);
5144   SDValue ExtractSrc = Op.getOperand(0);
5145   EVT VecVT = EVT::getVectorVT(
5146       *DAG.getContext(), DstVT.getScalarType(),
5147       ExtractSrc.getValueType().getVectorNumElements() * DstNumElt);
5148   SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc);
5149   return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast,
5150                      DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32));
5151 }
5152 
5153 /// ExpandBITCAST - If the target supports VFP, this function is called to
5154 /// expand a bit convert where either the source or destination type is i64 to
5155 /// use a VMOVDRR or VMOVRRD node.  This should not be done when the non-i64
5156 /// operand type is illegal (e.g., v2f32 for a target that doesn't support
5157 /// vectors), since the legalizer won't know what to do with that.
5158 static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG,
5159                              const ARMSubtarget *Subtarget) {
5160   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5161   SDLoc dl(N);
5162   SDValue Op = N->getOperand(0);
5163 
5164   // This function is only supposed to be called for i64 types, either as the
5165   // source or destination of the bit convert.
5166   EVT SrcVT = Op.getValueType();
5167   EVT DstVT = N->getValueType(0);
5168   const bool HasFullFP16 = Subtarget->hasFullFP16();
5169 
5170   if (SrcVT == MVT::f32 && DstVT == MVT::i32) {
5171      // FullFP16: half values are passed in S-registers, and we don't
5172      // need any of the bitcast and moves:
5173      //
5174      // t2: f32,ch = CopyFromReg t0, Register:f32 %0
5175      //   t5: i32 = bitcast t2
5176      // t18: f16 = ARMISD::VMOVhr t5
5177      if (Op.getOpcode() != ISD::CopyFromReg ||
5178          Op.getValueType() != MVT::f32)
5179        return SDValue();
5180 
5181      auto Move = N->use_begin();
5182      if (Move->getOpcode() != ARMISD::VMOVhr)
5183        return SDValue();
5184 
5185      SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
5186      SDValue Copy = DAG.getNode(ISD::CopyFromReg, SDLoc(Op), MVT::f16, Ops);
5187      DAG.ReplaceAllUsesWith(*Move, &Copy);
5188      return Copy;
5189   }
5190 
5191   if (SrcVT == MVT::i16 && DstVT == MVT::f16) {
5192     if (!HasFullFP16)
5193       return SDValue();
5194     // SoftFP: read half-precision arguments:
5195     //
5196     // t2: i32,ch = ...
5197     //        t7: i16 = truncate t2 <~~~~ Op
5198     //      t8: f16 = bitcast t7    <~~~~ N
5199     //
5200     if (Op.getOperand(0).getValueType() == MVT::i32)
5201       return DAG.getNode(ARMISD::VMOVhr, SDLoc(Op),
5202                          MVT::f16, Op.getOperand(0));
5203 
5204     return SDValue();
5205   }
5206 
5207   // Half-precision return values
5208   if (SrcVT == MVT::f16 && DstVT == MVT::i16) {
5209     if (!HasFullFP16)
5210       return SDValue();
5211     //
5212     //          t11: f16 = fadd t8, t10
5213     //        t12: i16 = bitcast t11       <~~~ SDNode N
5214     //      t13: i32 = zero_extend t12
5215     //    t16: ch,glue = CopyToReg t0, Register:i32 %r0, t13
5216     //  t17: ch = ARMISD::RET_FLAG t16, Register:i32 %r0, t16:1
5217     //
5218     // transform this into:
5219     //
5220     //    t20: i32 = ARMISD::VMOVrh t11
5221     //  t16: ch,glue = CopyToReg t0, Register:i32 %r0, t20
5222     //
5223     auto ZeroExtend = N->use_begin();
5224     if (N->use_size() != 1 || ZeroExtend->getOpcode() != ISD::ZERO_EXTEND ||
5225         ZeroExtend->getValueType(0) != MVT::i32)
5226       return SDValue();
5227 
5228     auto Copy = ZeroExtend->use_begin();
5229     if (Copy->getOpcode() == ISD::CopyToReg &&
5230         Copy->use_begin()->getOpcode() == ARMISD::RET_FLAG) {
5231       SDValue Cvt = DAG.getNode(ARMISD::VMOVrh, SDLoc(Op), MVT::i32, Op);
5232       DAG.ReplaceAllUsesWith(*ZeroExtend, &Cvt);
5233       return Cvt;
5234     }
5235     return SDValue();
5236   }
5237 
5238   if (!(SrcVT == MVT::i64 || DstVT == MVT::i64))
5239     return SDValue();
5240 
5241   // Turn i64->f64 into VMOVDRR.
5242   if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
5243     // Do not force values to GPRs (this is what VMOVDRR does for the inputs)
5244     // if we can combine the bitcast with its source.
5245     if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG))
5246       return Val;
5247 
5248     SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
5249                              DAG.getConstant(0, dl, MVT::i32));
5250     SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
5251                              DAG.getConstant(1, dl, MVT::i32));
5252     return DAG.getNode(ISD::BITCAST, dl, DstVT,
5253                        DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
5254   }
5255 
5256   // Turn f64->i64 into VMOVRRD.
5257   if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
5258     SDValue Cvt;
5259     if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
5260         SrcVT.getVectorNumElements() > 1)
5261       Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
5262                         DAG.getVTList(MVT::i32, MVT::i32),
5263                         DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
5264     else
5265       Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
5266                         DAG.getVTList(MVT::i32, MVT::i32), Op);
5267     // Merge the pieces into a single i64 value.
5268     return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
5269   }
5270 
5271   return SDValue();
5272 }
5273 
5274 /// getZeroVector - Returns a vector of specified type with all zero elements.
5275 /// Zero vectors are used to represent vector negation and in those cases
5276 /// will be implemented with the NEON VNEG instruction.  However, VNEG does
5277 /// not support i64 elements, so sometimes the zero vectors will need to be
5278 /// explicitly constructed.  Regardless, use a canonical VMOV to create the
5279 /// zero vector.
5280 static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, const SDLoc &dl) {
5281   assert(VT.isVector() && "Expected a vector type");
5282   // The canonical modified immediate encoding of a zero vector is....0!
5283   SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
5284   EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
5285   SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
5286   return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
5287 }
5288 
5289 /// LowerShiftRightParts - Lower SRA_PARTS, which returns two
5290 /// i32 values and take a 2 x i32 value to shift plus a shift amount.
5291 SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
5292                                                 SelectionDAG &DAG) const {
5293   assert(Op.getNumOperands() == 3 && "Not a double-shift!");
5294   EVT VT = Op.getValueType();
5295   unsigned VTBits = VT.getSizeInBits();
5296   SDLoc dl(Op);
5297   SDValue ShOpLo = Op.getOperand(0);
5298   SDValue ShOpHi = Op.getOperand(1);
5299   SDValue ShAmt  = Op.getOperand(2);
5300   SDValue ARMcc;
5301   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
5302   unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
5303 
5304   assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
5305 
5306   SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
5307                                  DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
5308   SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
5309   SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
5310                                    DAG.getConstant(VTBits, dl, MVT::i32));
5311   SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
5312   SDValue LoSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
5313   SDValue LoBigShift = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
5314   SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
5315                             ISD::SETGE, ARMcc, DAG, dl);
5316   SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift, LoBigShift,
5317                            ARMcc, CCR, CmpLo);
5318 
5319   SDValue HiSmallShift = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
5320   SDValue HiBigShift = Opc == ISD::SRA
5321                            ? DAG.getNode(Opc, dl, VT, ShOpHi,
5322                                          DAG.getConstant(VTBits - 1, dl, VT))
5323                            : DAG.getConstant(0, dl, VT);
5324   SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
5325                             ISD::SETGE, ARMcc, DAG, dl);
5326   SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift,
5327                            ARMcc, CCR, CmpHi);
5328 
5329   SDValue Ops[2] = { Lo, Hi };
5330   return DAG.getMergeValues(Ops, dl);
5331 }
5332 
5333 /// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
5334 /// i32 values and take a 2 x i32 value to shift plus a shift amount.
5335 SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
5336                                                SelectionDAG &DAG) const {
5337   assert(Op.getNumOperands() == 3 && "Not a double-shift!");
5338   EVT VT = Op.getValueType();
5339   unsigned VTBits = VT.getSizeInBits();
5340   SDLoc dl(Op);
5341   SDValue ShOpLo = Op.getOperand(0);
5342   SDValue ShOpHi = Op.getOperand(1);
5343   SDValue ShAmt  = Op.getOperand(2);
5344   SDValue ARMcc;
5345   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
5346 
5347   assert(Op.getOpcode() == ISD::SHL_PARTS);
5348   SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
5349                                  DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
5350   SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
5351   SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
5352   SDValue HiSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
5353 
5354   SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
5355                                    DAG.getConstant(VTBits, dl, MVT::i32));
5356   SDValue HiBigShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
5357   SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
5358                             ISD::SETGE, ARMcc, DAG, dl);
5359   SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift,
5360                            ARMcc, CCR, CmpHi);
5361 
5362   SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
5363                           ISD::SETGE, ARMcc, DAG, dl);
5364   SDValue LoSmallShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
5365   SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift,
5366                            DAG.getConstant(0, dl, VT), ARMcc, CCR, CmpLo);
5367 
5368   SDValue Ops[2] = { Lo, Hi };
5369   return DAG.getMergeValues(Ops, dl);
5370 }
5371 
5372 SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
5373                                             SelectionDAG &DAG) const {
5374   // The rounding mode is in bits 23:22 of the FPSCR.
5375   // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
5376   // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
5377   // so that the shift + and get folded into a bitfield extract.
5378   SDLoc dl(Op);
5379   SDValue Ops[] = { DAG.getEntryNode(),
5380                     DAG.getConstant(Intrinsic::arm_get_fpscr, dl, MVT::i32) };
5381 
5382   SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_W_CHAIN, dl, MVT::i32, Ops);
5383   SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
5384                                   DAG.getConstant(1U << 22, dl, MVT::i32));
5385   SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
5386                               DAG.getConstant(22, dl, MVT::i32));
5387   return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
5388                      DAG.getConstant(3, dl, MVT::i32));
5389 }
5390 
5391 static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
5392                          const ARMSubtarget *ST) {
5393   SDLoc dl(N);
5394   EVT VT = N->getValueType(0);
5395   if (VT.isVector()) {
5396     assert(ST->hasNEON());
5397 
5398     // Compute the least significant set bit: LSB = X & -X
5399     SDValue X = N->getOperand(0);
5400     SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
5401     SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
5402 
5403     EVT ElemTy = VT.getVectorElementType();
5404 
5405     if (ElemTy == MVT::i8) {
5406       // Compute with: cttz(x) = ctpop(lsb - 1)
5407       SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
5408                                 DAG.getTargetConstant(1, dl, ElemTy));
5409       SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
5410       return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
5411     }
5412 
5413     if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
5414         (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
5415       // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
5416       unsigned NumBits = ElemTy.getSizeInBits();
5417       SDValue WidthMinus1 =
5418           DAG.getNode(ARMISD::VMOVIMM, dl, VT,
5419                       DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
5420       SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
5421       return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
5422     }
5423 
5424     // Compute with: cttz(x) = ctpop(lsb - 1)
5425 
5426     // Compute LSB - 1.
5427     SDValue Bits;
5428     if (ElemTy == MVT::i64) {
5429       // Load constant 0xffff'ffff'ffff'ffff to register.
5430       SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
5431                                DAG.getTargetConstant(0x1eff, dl, MVT::i32));
5432       Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
5433     } else {
5434       SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
5435                                 DAG.getTargetConstant(1, dl, ElemTy));
5436       Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
5437     }
5438     return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
5439   }
5440 
5441   if (!ST->hasV6T2Ops())
5442     return SDValue();
5443 
5444   SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
5445   return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
5446 }
5447 
5448 static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
5449                           const ARMSubtarget *ST) {
5450   EVT VT = N->getValueType(0);
5451   SDLoc DL(N);
5452 
5453   assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
5454   assert((VT == MVT::v1i64 || VT == MVT::v2i64 || VT == MVT::v2i32 ||
5455           VT == MVT::v4i32 || VT == MVT::v4i16 || VT == MVT::v8i16) &&
5456          "Unexpected type for custom ctpop lowering");
5457 
5458   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5459   EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
5460   SDValue Res = DAG.getBitcast(VT8Bit, N->getOperand(0));
5461   Res = DAG.getNode(ISD::CTPOP, DL, VT8Bit, Res);
5462 
5463   // Widen v8i8/v16i8 CTPOP result to VT by repeatedly widening pairwise adds.
5464   unsigned EltSize = 8;
5465   unsigned NumElts = VT.is64BitVector() ? 8 : 16;
5466   while (EltSize != VT.getScalarSizeInBits()) {
5467     SmallVector<SDValue, 8> Ops;
5468     Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddlu, DL,
5469                                   TLI.getPointerTy(DAG.getDataLayout())));
5470     Ops.push_back(Res);
5471 
5472     EltSize *= 2;
5473     NumElts /= 2;
5474     MVT WidenVT = MVT::getVectorVT(MVT::getIntegerVT(EltSize), NumElts);
5475     Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, WidenVT, Ops);
5476   }
5477 
5478   return Res;
5479 }
5480 
5481 static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
5482                           const ARMSubtarget *ST) {
5483   EVT VT = N->getValueType(0);
5484   SDLoc dl(N);
5485 
5486   if (!VT.isVector())
5487     return SDValue();
5488 
5489   // Lower vector shifts on NEON to use VSHL.
5490   assert(ST->hasNEON() && "unexpected vector shift");
5491 
5492   // Left shifts translate directly to the vshiftu intrinsic.
5493   if (N->getOpcode() == ISD::SHL)
5494     return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
5495                        DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
5496                                        MVT::i32),
5497                        N->getOperand(0), N->getOperand(1));
5498 
5499   assert((N->getOpcode() == ISD::SRA ||
5500           N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
5501 
5502   // NEON uses the same intrinsics for both left and right shifts.  For
5503   // right shifts, the shift amounts are negative, so negate the vector of
5504   // shift amounts.
5505   EVT ShiftVT = N->getOperand(1).getValueType();
5506   SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
5507                                      getZeroVector(ShiftVT, DAG, dl),
5508                                      N->getOperand(1));
5509   Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
5510                              Intrinsic::arm_neon_vshifts :
5511                              Intrinsic::arm_neon_vshiftu);
5512   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
5513                      DAG.getConstant(vshiftInt, dl, MVT::i32),
5514                      N->getOperand(0), NegatedCount);
5515 }
5516 
5517 static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
5518                                 const ARMSubtarget *ST) {
5519   EVT VT = N->getValueType(0);
5520   SDLoc dl(N);
5521 
5522   // We can get here for a node like i32 = ISD::SHL i32, i64
5523   if (VT != MVT::i64)
5524     return SDValue();
5525 
5526   assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
5527          "Unknown shift to lower!");
5528 
5529   // We only lower SRA, SRL of 1 here, all others use generic lowering.
5530   if (!isOneConstant(N->getOperand(1)))
5531     return SDValue();
5532 
5533   // If we are in thumb mode, we don't have RRX.
5534   if (ST->isThumb1Only()) return SDValue();
5535 
5536   // Okay, we have a 64-bit SRA or SRL of 1.  Lower this to an RRX expr.
5537   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
5538                            DAG.getConstant(0, dl, MVT::i32));
5539   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
5540                            DAG.getConstant(1, dl, MVT::i32));
5541 
5542   // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
5543   // captures the result into a carry flag.
5544   unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
5545   Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
5546 
5547   // The low part is an ARMISD::RRX operand, which shifts the carry in.
5548   Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
5549 
5550   // Merge the pieces into a single i64 value.
5551  return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
5552 }
5553 
5554 static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5555   SDValue TmpOp0, TmpOp1;
5556   bool Invert = false;
5557   bool Swap = false;
5558   unsigned Opc = 0;
5559 
5560   SDValue Op0 = Op.getOperand(0);
5561   SDValue Op1 = Op.getOperand(1);
5562   SDValue CC = Op.getOperand(2);
5563   EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
5564   EVT VT = Op.getValueType();
5565   ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5566   SDLoc dl(Op);
5567 
5568   if (Op0.getValueType().getVectorElementType() == MVT::i64 &&
5569       (SetCCOpcode == ISD::SETEQ || SetCCOpcode == ISD::SETNE)) {
5570     // Special-case integer 64-bit equality comparisons. They aren't legal,
5571     // but they can be lowered with a few vector instructions.
5572     unsigned CmpElements = CmpVT.getVectorNumElements() * 2;
5573     EVT SplitVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, CmpElements);
5574     SDValue CastOp0 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op0);
5575     SDValue CastOp1 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op1);
5576     SDValue Cmp = DAG.getNode(ISD::SETCC, dl, SplitVT, CastOp0, CastOp1,
5577                               DAG.getCondCode(ISD::SETEQ));
5578     SDValue Reversed = DAG.getNode(ARMISD::VREV64, dl, SplitVT, Cmp);
5579     SDValue Merged = DAG.getNode(ISD::AND, dl, SplitVT, Cmp, Reversed);
5580     Merged = DAG.getNode(ISD::BITCAST, dl, CmpVT, Merged);
5581     if (SetCCOpcode == ISD::SETNE)
5582       Merged = DAG.getNOT(dl, Merged, CmpVT);
5583     Merged = DAG.getSExtOrTrunc(Merged, dl, VT);
5584     return Merged;
5585   }
5586 
5587   if (CmpVT.getVectorElementType() == MVT::i64)
5588     // 64-bit comparisons are not legal in general.
5589     return SDValue();
5590 
5591   if (Op1.getValueType().isFloatingPoint()) {
5592     switch (SetCCOpcode) {
5593     default: llvm_unreachable("Illegal FP comparison");
5594     case ISD::SETUNE:
5595     case ISD::SETNE:  Invert = true; LLVM_FALLTHROUGH;
5596     case ISD::SETOEQ:
5597     case ISD::SETEQ:  Opc = ARMISD::VCEQ; break;
5598     case ISD::SETOLT:
5599     case ISD::SETLT: Swap = true; LLVM_FALLTHROUGH;
5600     case ISD::SETOGT:
5601     case ISD::SETGT:  Opc = ARMISD::VCGT; break;
5602     case ISD::SETOLE:
5603     case ISD::SETLE:  Swap = true; LLVM_FALLTHROUGH;
5604     case ISD::SETOGE:
5605     case ISD::SETGE: Opc = ARMISD::VCGE; break;
5606     case ISD::SETUGE: Swap = true; LLVM_FALLTHROUGH;
5607     case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
5608     case ISD::SETUGT: Swap = true; LLVM_FALLTHROUGH;
5609     case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
5610     case ISD::SETUEQ: Invert = true; LLVM_FALLTHROUGH;
5611     case ISD::SETONE:
5612       // Expand this to (OLT | OGT).
5613       TmpOp0 = Op0;
5614       TmpOp1 = Op1;
5615       Opc = ISD::OR;
5616       Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
5617       Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
5618       break;
5619     case ISD::SETUO:
5620       Invert = true;
5621       LLVM_FALLTHROUGH;
5622     case ISD::SETO:
5623       // Expand this to (OLT | OGE).
5624       TmpOp0 = Op0;
5625       TmpOp1 = Op1;
5626       Opc = ISD::OR;
5627       Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
5628       Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
5629       break;
5630     }
5631   } else {
5632     // Integer comparisons.
5633     switch (SetCCOpcode) {
5634     default: llvm_unreachable("Illegal integer comparison");
5635     case ISD::SETNE:  Invert = true; LLVM_FALLTHROUGH;
5636     case ISD::SETEQ:  Opc = ARMISD::VCEQ; break;
5637     case ISD::SETLT:  Swap = true; LLVM_FALLTHROUGH;
5638     case ISD::SETGT:  Opc = ARMISD::VCGT; break;
5639     case ISD::SETLE:  Swap = true; LLVM_FALLTHROUGH;
5640     case ISD::SETGE:  Opc = ARMISD::VCGE; break;
5641     case ISD::SETULT: Swap = true; LLVM_FALLTHROUGH;
5642     case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
5643     case ISD::SETULE: Swap = true; LLVM_FALLTHROUGH;
5644     case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
5645     }
5646 
5647     // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
5648     if (Opc == ARMISD::VCEQ) {
5649       SDValue AndOp;
5650       if (ISD::isBuildVectorAllZeros(Op1.getNode()))
5651         AndOp = Op0;
5652       else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
5653         AndOp = Op1;
5654 
5655       // Ignore bitconvert.
5656       if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
5657         AndOp = AndOp.getOperand(0);
5658 
5659       if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
5660         Opc = ARMISD::VTST;
5661         Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
5662         Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
5663         Invert = !Invert;
5664       }
5665     }
5666   }
5667 
5668   if (Swap)
5669     std::swap(Op0, Op1);
5670 
5671   // If one of the operands is a constant vector zero, attempt to fold the
5672   // comparison to a specialized compare-against-zero form.
5673   SDValue SingleOp;
5674   if (ISD::isBuildVectorAllZeros(Op1.getNode()))
5675     SingleOp = Op0;
5676   else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
5677     if (Opc == ARMISD::VCGE)
5678       Opc = ARMISD::VCLEZ;
5679     else if (Opc == ARMISD::VCGT)
5680       Opc = ARMISD::VCLTZ;
5681     SingleOp = Op1;
5682   }
5683 
5684   SDValue Result;
5685   if (SingleOp.getNode()) {
5686     switch (Opc) {
5687     case ARMISD::VCEQ:
5688       Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
5689     case ARMISD::VCGE:
5690       Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
5691     case ARMISD::VCLEZ:
5692       Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
5693     case ARMISD::VCGT:
5694       Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
5695     case ARMISD::VCLTZ:
5696       Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
5697     default:
5698       Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
5699     }
5700   } else {
5701      Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
5702   }
5703 
5704   Result = DAG.getSExtOrTrunc(Result, dl, VT);
5705 
5706   if (Invert)
5707     Result = DAG.getNOT(dl, Result, VT);
5708 
5709   return Result;
5710 }
5711 
5712 static SDValue LowerSETCCCARRY(SDValue Op, SelectionDAG &DAG) {
5713   SDValue LHS = Op.getOperand(0);
5714   SDValue RHS = Op.getOperand(1);
5715   SDValue Carry = Op.getOperand(2);
5716   SDValue Cond = Op.getOperand(3);
5717   SDLoc DL(Op);
5718 
5719   assert(LHS.getSimpleValueType().isInteger() && "SETCCCARRY is integer only.");
5720 
5721   // ARMISD::SUBE expects a carry not a borrow like ISD::SUBCARRY so we
5722   // have to invert the carry first.
5723   Carry = DAG.getNode(ISD::SUB, DL, MVT::i32,
5724                       DAG.getConstant(1, DL, MVT::i32), Carry);
5725   // This converts the boolean value carry into the carry flag.
5726   Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG);
5727 
5728   SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
5729   SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry);
5730 
5731   SDValue FVal = DAG.getConstant(0, DL, MVT::i32);
5732   SDValue TVal = DAG.getConstant(1, DL, MVT::i32);
5733   SDValue ARMcc = DAG.getConstant(
5734       IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32);
5735   SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
5736   SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR,
5737                                    Cmp.getValue(1), SDValue());
5738   return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc,
5739                      CCR, Chain.getValue(1));
5740 }
5741 
5742 /// isNEONModifiedImm - Check if the specified splat value corresponds to a
5743 /// valid vector constant for a NEON instruction with a "modified immediate"
5744 /// operand (e.g., VMOV).  If so, return the encoded value.
5745 static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
5746                                  unsigned SplatBitSize, SelectionDAG &DAG,
5747                                  const SDLoc &dl, EVT &VT, bool is128Bits,
5748                                  NEONModImmType type) {
5749   unsigned OpCmode, Imm;
5750 
5751   // SplatBitSize is set to the smallest size that splats the vector, so a
5752   // zero vector will always have SplatBitSize == 8.  However, NEON modified
5753   // immediate instructions others than VMOV do not support the 8-bit encoding
5754   // of a zero vector, and the default encoding of zero is supposed to be the
5755   // 32-bit version.
5756   if (SplatBits == 0)
5757     SplatBitSize = 32;
5758 
5759   switch (SplatBitSize) {
5760   case 8:
5761     if (type != VMOVModImm)
5762       return SDValue();
5763     // Any 1-byte value is OK.  Op=0, Cmode=1110.
5764     assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
5765     OpCmode = 0xe;
5766     Imm = SplatBits;
5767     VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
5768     break;
5769 
5770   case 16:
5771     // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
5772     VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
5773     if ((SplatBits & ~0xff) == 0) {
5774       // Value = 0x00nn: Op=x, Cmode=100x.
5775       OpCmode = 0x8;
5776       Imm = SplatBits;
5777       break;
5778     }
5779     if ((SplatBits & ~0xff00) == 0) {
5780       // Value = 0xnn00: Op=x, Cmode=101x.
5781       OpCmode = 0xa;
5782       Imm = SplatBits >> 8;
5783       break;
5784     }
5785     return SDValue();
5786 
5787   case 32:
5788     // NEON's 32-bit VMOV supports splat values where:
5789     // * only one byte is nonzero, or
5790     // * the least significant byte is 0xff and the second byte is nonzero, or
5791     // * the least significant 2 bytes are 0xff and the third is nonzero.
5792     VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
5793     if ((SplatBits & ~0xff) == 0) {
5794       // Value = 0x000000nn: Op=x, Cmode=000x.
5795       OpCmode = 0;
5796       Imm = SplatBits;
5797       break;
5798     }
5799     if ((SplatBits & ~0xff00) == 0) {
5800       // Value = 0x0000nn00: Op=x, Cmode=001x.
5801       OpCmode = 0x2;
5802       Imm = SplatBits >> 8;
5803       break;
5804     }
5805     if ((SplatBits & ~0xff0000) == 0) {
5806       // Value = 0x00nn0000: Op=x, Cmode=010x.
5807       OpCmode = 0x4;
5808       Imm = SplatBits >> 16;
5809       break;
5810     }
5811     if ((SplatBits & ~0xff000000) == 0) {
5812       // Value = 0xnn000000: Op=x, Cmode=011x.
5813       OpCmode = 0x6;
5814       Imm = SplatBits >> 24;
5815       break;
5816     }
5817 
5818     // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
5819     if (type == OtherModImm) return SDValue();
5820 
5821     if ((SplatBits & ~0xffff) == 0 &&
5822         ((SplatBits | SplatUndef) & 0xff) == 0xff) {
5823       // Value = 0x0000nnff: Op=x, Cmode=1100.
5824       OpCmode = 0xc;
5825       Imm = SplatBits >> 8;
5826       break;
5827     }
5828 
5829     if ((SplatBits & ~0xffffff) == 0 &&
5830         ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
5831       // Value = 0x00nnffff: Op=x, Cmode=1101.
5832       OpCmode = 0xd;
5833       Imm = SplatBits >> 16;
5834       break;
5835     }
5836 
5837     // Note: there are a few 32-bit splat values (specifically: 00ffff00,
5838     // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
5839     // VMOV.I32.  A (very) minor optimization would be to replicate the value
5840     // and fall through here to test for a valid 64-bit splat.  But, then the
5841     // caller would also need to check and handle the change in size.
5842     return SDValue();
5843 
5844   case 64: {
5845     if (type != VMOVModImm)
5846       return SDValue();
5847     // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
5848     uint64_t BitMask = 0xff;
5849     uint64_t Val = 0;
5850     unsigned ImmMask = 1;
5851     Imm = 0;
5852     for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
5853       if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
5854         Val |= BitMask;
5855         Imm |= ImmMask;
5856       } else if ((SplatBits & BitMask) != 0) {
5857         return SDValue();
5858       }
5859       BitMask <<= 8;
5860       ImmMask <<= 1;
5861     }
5862 
5863     if (DAG.getDataLayout().isBigEndian())
5864       // swap higher and lower 32 bit word
5865       Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
5866 
5867     // Op=1, Cmode=1110.
5868     OpCmode = 0x1e;
5869     VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
5870     break;
5871   }
5872 
5873   default:
5874     llvm_unreachable("unexpected size for isNEONModifiedImm");
5875   }
5876 
5877   unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
5878   return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
5879 }
5880 
5881 SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
5882                                            const ARMSubtarget *ST) const {
5883   EVT VT = Op.getValueType();
5884   bool IsDouble = (VT == MVT::f64);
5885   ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
5886   const APFloat &FPVal = CFP->getValueAPF();
5887 
5888   // Prevent floating-point constants from using literal loads
5889   // when execute-only is enabled.
5890   if (ST->genExecuteOnly()) {
5891     // If we can represent the constant as an immediate, don't lower it
5892     if (isFPImmLegal(FPVal, VT))
5893       return Op;
5894     // Otherwise, construct as integer, and move to float register
5895     APInt INTVal = FPVal.bitcastToAPInt();
5896     SDLoc DL(CFP);
5897     switch (VT.getSimpleVT().SimpleTy) {
5898       default:
5899         llvm_unreachable("Unknown floating point type!");
5900         break;
5901       case MVT::f64: {
5902         SDValue Lo = DAG.getConstant(INTVal.trunc(32), DL, MVT::i32);
5903         SDValue Hi = DAG.getConstant(INTVal.lshr(32).trunc(32), DL, MVT::i32);
5904         if (!ST->isLittle())
5905           std::swap(Lo, Hi);
5906         return DAG.getNode(ARMISD::VMOVDRR, DL, MVT::f64, Lo, Hi);
5907       }
5908       case MVT::f32:
5909           return DAG.getNode(ARMISD::VMOVSR, DL, VT,
5910               DAG.getConstant(INTVal, DL, MVT::i32));
5911     }
5912   }
5913 
5914   if (!ST->hasVFP3())
5915     return SDValue();
5916 
5917   // Use the default (constant pool) lowering for double constants when we have
5918   // an SP-only FPU
5919   if (IsDouble && Subtarget->isFPOnlySP())
5920     return SDValue();
5921 
5922   // Try splatting with a VMOV.f32...
5923   int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
5924 
5925   if (ImmVal != -1) {
5926     if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
5927       // We have code in place to select a valid ConstantFP already, no need to
5928       // do any mangling.
5929       return Op;
5930     }
5931 
5932     // It's a float and we are trying to use NEON operations where
5933     // possible. Lower it to a splat followed by an extract.
5934     SDLoc DL(Op);
5935     SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
5936     SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
5937                                       NewVal);
5938     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
5939                        DAG.getConstant(0, DL, MVT::i32));
5940   }
5941 
5942   // The rest of our options are NEON only, make sure that's allowed before
5943   // proceeding..
5944   if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
5945     return SDValue();
5946 
5947   EVT VMovVT;
5948   uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
5949 
5950   // It wouldn't really be worth bothering for doubles except for one very
5951   // important value, which does happen to match: 0.0. So make sure we don't do
5952   // anything stupid.
5953   if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
5954     return SDValue();
5955 
5956   // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
5957   SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
5958                                      VMovVT, false, VMOVModImm);
5959   if (NewVal != SDValue()) {
5960     SDLoc DL(Op);
5961     SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
5962                                       NewVal);
5963     if (IsDouble)
5964       return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5965 
5966     // It's a float: cast and extract a vector element.
5967     SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5968                                        VecConstant);
5969     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
5970                        DAG.getConstant(0, DL, MVT::i32));
5971   }
5972 
5973   // Finally, try a VMVN.i32
5974   NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
5975                              false, VMVNModImm);
5976   if (NewVal != SDValue()) {
5977     SDLoc DL(Op);
5978     SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
5979 
5980     if (IsDouble)
5981       return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5982 
5983     // It's a float: cast and extract a vector element.
5984     SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5985                                        VecConstant);
5986     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
5987                        DAG.getConstant(0, DL, MVT::i32));
5988   }
5989 
5990   return SDValue();
5991 }
5992 
5993 // check if an VEXT instruction can handle the shuffle mask when the
5994 // vector sources of the shuffle are the same.
5995 static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
5996   unsigned NumElts = VT.getVectorNumElements();
5997 
5998   // Assume that the first shuffle index is not UNDEF.  Fail if it is.
5999   if (M[0] < 0)
6000     return false;
6001 
6002   Imm = M[0];
6003 
6004   // If this is a VEXT shuffle, the immediate value is the index of the first
6005   // element.  The other shuffle indices must be the successive elements after
6006   // the first one.
6007   unsigned ExpectedElt = Imm;
6008   for (unsigned i = 1; i < NumElts; ++i) {
6009     // Increment the expected index.  If it wraps around, just follow it
6010     // back to index zero and keep going.
6011     ++ExpectedElt;
6012     if (ExpectedElt == NumElts)
6013       ExpectedElt = 0;
6014 
6015     if (M[i] < 0) continue; // ignore UNDEF indices
6016     if (ExpectedElt != static_cast<unsigned>(M[i]))
6017       return false;
6018   }
6019 
6020   return true;
6021 }
6022 
6023 static bool isVEXTMask(ArrayRef<int> M, EVT VT,
6024                        bool &ReverseVEXT, unsigned &Imm) {
6025   unsigned NumElts = VT.getVectorNumElements();
6026   ReverseVEXT = false;
6027 
6028   // Assume that the first shuffle index is not UNDEF.  Fail if it is.
6029   if (M[0] < 0)
6030     return false;
6031 
6032   Imm = M[0];
6033 
6034   // If this is a VEXT shuffle, the immediate value is the index of the first
6035   // element.  The other shuffle indices must be the successive elements after
6036   // the first one.
6037   unsigned ExpectedElt = Imm;
6038   for (unsigned i = 1; i < NumElts; ++i) {
6039     // Increment the expected index.  If it wraps around, it may still be
6040     // a VEXT but the source vectors must be swapped.
6041     ExpectedElt += 1;
6042     if (ExpectedElt == NumElts * 2) {
6043       ExpectedElt = 0;
6044       ReverseVEXT = true;
6045     }
6046 
6047     if (M[i] < 0) continue; // ignore UNDEF indices
6048     if (ExpectedElt != static_cast<unsigned>(M[i]))
6049       return false;
6050   }
6051 
6052   // Adjust the index value if the source operands will be swapped.
6053   if (ReverseVEXT)
6054     Imm -= NumElts;
6055 
6056   return true;
6057 }
6058 
6059 /// isVREVMask - Check if a vector shuffle corresponds to a VREV
6060 /// instruction with the specified blocksize.  (The order of the elements
6061 /// within each block of the vector is reversed.)
6062 static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
6063   assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
6064          "Only possible block sizes for VREV are: 16, 32, 64");
6065 
6066   unsigned EltSz = VT.getScalarSizeInBits();
6067   if (EltSz == 64)
6068     return false;
6069 
6070   unsigned NumElts = VT.getVectorNumElements();
6071   unsigned BlockElts = M[0] + 1;
6072   // If the first shuffle index is UNDEF, be optimistic.
6073   if (M[0] < 0)
6074     BlockElts = BlockSize / EltSz;
6075 
6076   if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
6077     return false;
6078 
6079   for (unsigned i = 0; i < NumElts; ++i) {
6080     if (M[i] < 0) continue; // ignore UNDEF indices
6081     if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
6082       return false;
6083   }
6084 
6085   return true;
6086 }
6087 
6088 static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
6089   // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
6090   // range, then 0 is placed into the resulting vector. So pretty much any mask
6091   // of 8 elements can work here.
6092   return VT == MVT::v8i8 && M.size() == 8;
6093 }
6094 
6095 static unsigned SelectPairHalf(unsigned Elements, ArrayRef<int> Mask,
6096                                unsigned Index) {
6097   if (Mask.size() == Elements * 2)
6098     return Index / Elements;
6099   return Mask[Index] == 0 ? 0 : 1;
6100 }
6101 
6102 // Checks whether the shuffle mask represents a vector transpose (VTRN) by
6103 // checking that pairs of elements in the shuffle mask represent the same index
6104 // in each vector, incrementing the expected index by 2 at each step.
6105 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
6106 //  v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
6107 //  v2={e,f,g,h}
6108 // WhichResult gives the offset for each element in the mask based on which
6109 // of the two results it belongs to.
6110 //
6111 // The transpose can be represented either as:
6112 // result1 = shufflevector v1, v2, result1_shuffle_mask
6113 // result2 = shufflevector v1, v2, result2_shuffle_mask
6114 // where v1/v2 and the shuffle masks have the same number of elements
6115 // (here WhichResult (see below) indicates which result is being checked)
6116 //
6117 // or as:
6118 // results = shufflevector v1, v2, shuffle_mask
6119 // where both results are returned in one vector and the shuffle mask has twice
6120 // as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
6121 // want to check the low half and high half of the shuffle mask as if it were
6122 // the other case
6123 static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
6124   unsigned EltSz = VT.getScalarSizeInBits();
6125   if (EltSz == 64)
6126     return false;
6127 
6128   unsigned NumElts = VT.getVectorNumElements();
6129   if (M.size() != NumElts && M.size() != NumElts*2)
6130     return false;
6131 
6132   // If the mask is twice as long as the input vector then we need to check the
6133   // upper and lower parts of the mask with a matching value for WhichResult
6134   // FIXME: A mask with only even values will be rejected in case the first
6135   // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
6136   // M[0] is used to determine WhichResult
6137   for (unsigned i = 0; i < M.size(); i += NumElts) {
6138     WhichResult = SelectPairHalf(NumElts, M, i);
6139     for (unsigned j = 0; j < NumElts; j += 2) {
6140       if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
6141           (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
6142         return false;
6143     }
6144   }
6145 
6146   if (M.size() == NumElts*2)
6147     WhichResult = 0;
6148 
6149   return true;
6150 }
6151 
6152 /// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
6153 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
6154 /// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
6155 static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
6156   unsigned EltSz = VT.getScalarSizeInBits();
6157   if (EltSz == 64)
6158     return false;
6159 
6160   unsigned NumElts = VT.getVectorNumElements();
6161   if (M.size() != NumElts && M.size() != NumElts*2)
6162     return false;
6163 
6164   for (unsigned i = 0; i < M.size(); i += NumElts) {
6165     WhichResult = SelectPairHalf(NumElts, M, i);
6166     for (unsigned j = 0; j < NumElts; j += 2) {
6167       if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
6168           (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
6169         return false;
6170     }
6171   }
6172 
6173   if (M.size() == NumElts*2)
6174     WhichResult = 0;
6175 
6176   return true;
6177 }
6178 
6179 // Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
6180 // that the mask elements are either all even and in steps of size 2 or all odd
6181 // and in steps of size 2.
6182 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
6183 //  v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
6184 //  v2={e,f,g,h}
6185 // Requires similar checks to that of isVTRNMask with
6186 // respect the how results are returned.
6187 static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
6188   unsigned EltSz = VT.getScalarSizeInBits();
6189   if (EltSz == 64)
6190     return false;
6191 
6192   unsigned NumElts = VT.getVectorNumElements();
6193   if (M.size() != NumElts && M.size() != NumElts*2)
6194     return false;
6195 
6196   for (unsigned i = 0; i < M.size(); i += NumElts) {
6197     WhichResult = SelectPairHalf(NumElts, M, i);
6198     for (unsigned j = 0; j < NumElts; ++j) {
6199       if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
6200         return false;
6201     }
6202   }
6203 
6204   if (M.size() == NumElts*2)
6205     WhichResult = 0;
6206 
6207   // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
6208   if (VT.is64BitVector() && EltSz == 32)
6209     return false;
6210 
6211   return true;
6212 }
6213 
6214 /// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
6215 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
6216 /// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
6217 static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
6218   unsigned EltSz = VT.getScalarSizeInBits();
6219   if (EltSz == 64)
6220     return false;
6221 
6222   unsigned NumElts = VT.getVectorNumElements();
6223   if (M.size() != NumElts && M.size() != NumElts*2)
6224     return false;
6225 
6226   unsigned Half = NumElts / 2;
6227   for (unsigned i = 0; i < M.size(); i += NumElts) {
6228     WhichResult = SelectPairHalf(NumElts, M, i);
6229     for (unsigned j = 0; j < NumElts; j += Half) {
6230       unsigned Idx = WhichResult;
6231       for (unsigned k = 0; k < Half; ++k) {
6232         int MIdx = M[i + j + k];
6233         if (MIdx >= 0 && (unsigned) MIdx != Idx)
6234           return false;
6235         Idx += 2;
6236       }
6237     }
6238   }
6239 
6240   if (M.size() == NumElts*2)
6241     WhichResult = 0;
6242 
6243   // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
6244   if (VT.is64BitVector() && EltSz == 32)
6245     return false;
6246 
6247   return true;
6248 }
6249 
6250 // Checks whether the shuffle mask represents a vector zip (VZIP) by checking
6251 // that pairs of elements of the shufflemask represent the same index in each
6252 // vector incrementing sequentially through the vectors.
6253 // e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
6254 //  v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
6255 //  v2={e,f,g,h}
6256 // Requires similar checks to that of isVTRNMask with respect the how results
6257 // are returned.
6258 static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
6259   unsigned EltSz = VT.getScalarSizeInBits();
6260   if (EltSz == 64)
6261     return false;
6262 
6263   unsigned NumElts = VT.getVectorNumElements();
6264   if (M.size() != NumElts && M.size() != NumElts*2)
6265     return false;
6266 
6267   for (unsigned i = 0; i < M.size(); i += NumElts) {
6268     WhichResult = SelectPairHalf(NumElts, M, i);
6269     unsigned Idx = WhichResult * NumElts / 2;
6270     for (unsigned j = 0; j < NumElts; j += 2) {
6271       if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
6272           (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
6273         return false;
6274       Idx += 1;
6275     }
6276   }
6277 
6278   if (M.size() == NumElts*2)
6279     WhichResult = 0;
6280 
6281   // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
6282   if (VT.is64BitVector() && EltSz == 32)
6283     return false;
6284 
6285   return true;
6286 }
6287 
6288 /// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
6289 /// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
6290 /// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
6291 static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
6292   unsigned EltSz = VT.getScalarSizeInBits();
6293   if (EltSz == 64)
6294     return false;
6295 
6296   unsigned NumElts = VT.getVectorNumElements();
6297   if (M.size() != NumElts && M.size() != NumElts*2)
6298     return false;
6299 
6300   for (unsigned i = 0; i < M.size(); i += NumElts) {
6301     WhichResult = SelectPairHalf(NumElts, M, i);
6302     unsigned Idx = WhichResult * NumElts / 2;
6303     for (unsigned j = 0; j < NumElts; j += 2) {
6304       if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
6305           (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
6306         return false;
6307       Idx += 1;
6308     }
6309   }
6310 
6311   if (M.size() == NumElts*2)
6312     WhichResult = 0;
6313 
6314   // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
6315   if (VT.is64BitVector() && EltSz == 32)
6316     return false;
6317 
6318   return true;
6319 }
6320 
6321 /// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
6322 /// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
6323 static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
6324                                            unsigned &WhichResult,
6325                                            bool &isV_UNDEF) {
6326   isV_UNDEF = false;
6327   if (isVTRNMask(ShuffleMask, VT, WhichResult))
6328     return ARMISD::VTRN;
6329   if (isVUZPMask(ShuffleMask, VT, WhichResult))
6330     return ARMISD::VUZP;
6331   if (isVZIPMask(ShuffleMask, VT, WhichResult))
6332     return ARMISD::VZIP;
6333 
6334   isV_UNDEF = true;
6335   if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
6336     return ARMISD::VTRN;
6337   if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
6338     return ARMISD::VUZP;
6339   if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
6340     return ARMISD::VZIP;
6341 
6342   return 0;
6343 }
6344 
6345 /// \return true if this is a reverse operation on an vector.
6346 static bool isReverseMask(ArrayRef<int> M, EVT VT) {
6347   unsigned NumElts = VT.getVectorNumElements();
6348   // Make sure the mask has the right size.
6349   if (NumElts != M.size())
6350       return false;
6351 
6352   // Look for <15, ..., 3, -1, 1, 0>.
6353   for (unsigned i = 0; i != NumElts; ++i)
6354     if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
6355       return false;
6356 
6357   return true;
6358 }
6359 
6360 // If N is an integer constant that can be moved into a register in one
6361 // instruction, return an SDValue of such a constant (will become a MOV
6362 // instruction).  Otherwise return null.
6363 static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
6364                                      const ARMSubtarget *ST, const SDLoc &dl) {
6365   uint64_t Val;
6366   if (!isa<ConstantSDNode>(N))
6367     return SDValue();
6368   Val = cast<ConstantSDNode>(N)->getZExtValue();
6369 
6370   if (ST->isThumb1Only()) {
6371     if (Val <= 255 || ~Val <= 255)
6372       return DAG.getConstant(Val, dl, MVT::i32);
6373   } else {
6374     if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
6375       return DAG.getConstant(Val, dl, MVT::i32);
6376   }
6377   return SDValue();
6378 }
6379 
6380 // If this is a case we can't handle, return null and let the default
6381 // expansion code take care of it.
6382 SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
6383                                              const ARMSubtarget *ST) const {
6384   BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
6385   SDLoc dl(Op);
6386   EVT VT = Op.getValueType();
6387 
6388   APInt SplatBits, SplatUndef;
6389   unsigned SplatBitSize;
6390   bool HasAnyUndefs;
6391   if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
6392     if (SplatUndef.isAllOnesValue())
6393       return DAG.getUNDEF(VT);
6394 
6395     if (SplatBitSize <= 64) {
6396       // Check if an immediate VMOV works.
6397       EVT VmovVT;
6398       SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
6399                                       SplatUndef.getZExtValue(), SplatBitSize,
6400                                       DAG, dl, VmovVT, VT.is128BitVector(),
6401                                       VMOVModImm);
6402       if (Val.getNode()) {
6403         SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
6404         return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
6405       }
6406 
6407       // Try an immediate VMVN.
6408       uint64_t NegatedImm = (~SplatBits).getZExtValue();
6409       Val = isNEONModifiedImm(NegatedImm,
6410                                       SplatUndef.getZExtValue(), SplatBitSize,
6411                                       DAG, dl, VmovVT, VT.is128BitVector(),
6412                                       VMVNModImm);
6413       if (Val.getNode()) {
6414         SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
6415         return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
6416       }
6417 
6418       // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
6419       if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
6420         int ImmVal = ARM_AM::getFP32Imm(SplatBits);
6421         if (ImmVal != -1) {
6422           SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
6423           return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
6424         }
6425       }
6426     }
6427   }
6428 
6429   // Scan through the operands to see if only one value is used.
6430   //
6431   // As an optimisation, even if more than one value is used it may be more
6432   // profitable to splat with one value then change some lanes.
6433   //
6434   // Heuristically we decide to do this if the vector has a "dominant" value,
6435   // defined as splatted to more than half of the lanes.
6436   unsigned NumElts = VT.getVectorNumElements();
6437   bool isOnlyLowElement = true;
6438   bool usesOnlyOneValue = true;
6439   bool hasDominantValue = false;
6440   bool isConstant = true;
6441 
6442   // Map of the number of times a particular SDValue appears in the
6443   // element list.
6444   DenseMap<SDValue, unsigned> ValueCounts;
6445   SDValue Value;
6446   for (unsigned i = 0; i < NumElts; ++i) {
6447     SDValue V = Op.getOperand(i);
6448     if (V.isUndef())
6449       continue;
6450     if (i > 0)
6451       isOnlyLowElement = false;
6452     if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
6453       isConstant = false;
6454 
6455     ValueCounts.insert(std::make_pair(V, 0));
6456     unsigned &Count = ValueCounts[V];
6457 
6458     // Is this value dominant? (takes up more than half of the lanes)
6459     if (++Count > (NumElts / 2)) {
6460       hasDominantValue = true;
6461       Value = V;
6462     }
6463   }
6464   if (ValueCounts.size() != 1)
6465     usesOnlyOneValue = false;
6466   if (!Value.getNode() && !ValueCounts.empty())
6467     Value = ValueCounts.begin()->first;
6468 
6469   if (ValueCounts.empty())
6470     return DAG.getUNDEF(VT);
6471 
6472   // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
6473   // Keep going if we are hitting this case.
6474   if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
6475     return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
6476 
6477   unsigned EltSize = VT.getScalarSizeInBits();
6478 
6479   // Use VDUP for non-constant splats.  For f32 constant splats, reduce to
6480   // i32 and try again.
6481   if (hasDominantValue && EltSize <= 32) {
6482     if (!isConstant) {
6483       SDValue N;
6484 
6485       // If we are VDUPing a value that comes directly from a vector, that will
6486       // cause an unnecessary move to and from a GPR, where instead we could
6487       // just use VDUPLANE. We can only do this if the lane being extracted
6488       // is at a constant index, as the VDUP from lane instructions only have
6489       // constant-index forms.
6490       ConstantSDNode *constIndex;
6491       if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6492           (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
6493         // We need to create a new undef vector to use for the VDUPLANE if the
6494         // size of the vector from which we get the value is different than the
6495         // size of the vector that we need to create. We will insert the element
6496         // such that the register coalescer will remove unnecessary copies.
6497         if (VT != Value->getOperand(0).getValueType()) {
6498           unsigned index = constIndex->getAPIntValue().getLimitedValue() %
6499                              VT.getVectorNumElements();
6500           N =  DAG.getNode(ARMISD::VDUPLANE, dl, VT,
6501                  DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
6502                         Value, DAG.getConstant(index, dl, MVT::i32)),
6503                            DAG.getConstant(index, dl, MVT::i32));
6504         } else
6505           N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
6506                         Value->getOperand(0), Value->getOperand(1));
6507       } else
6508         N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
6509 
6510       if (!usesOnlyOneValue) {
6511         // The dominant value was splatted as 'N', but we now have to insert
6512         // all differing elements.
6513         for (unsigned I = 0; I < NumElts; ++I) {
6514           if (Op.getOperand(I) == Value)
6515             continue;
6516           SmallVector<SDValue, 3> Ops;
6517           Ops.push_back(N);
6518           Ops.push_back(Op.getOperand(I));
6519           Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
6520           N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
6521         }
6522       }
6523       return N;
6524     }
6525     if (VT.getVectorElementType().isFloatingPoint()) {
6526       SmallVector<SDValue, 8> Ops;
6527       for (unsigned i = 0; i < NumElts; ++i)
6528         Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
6529                                   Op.getOperand(i)));
6530       EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
6531       SDValue Val = DAG.getBuildVector(VecVT, dl, Ops);
6532       Val = LowerBUILD_VECTOR(Val, DAG, ST);
6533       if (Val.getNode())
6534         return DAG.getNode(ISD::BITCAST, dl, VT, Val);
6535     }
6536     if (usesOnlyOneValue) {
6537       SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
6538       if (isConstant && Val.getNode())
6539         return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
6540     }
6541   }
6542 
6543   // If all elements are constants and the case above didn't get hit, fall back
6544   // to the default expansion, which will generate a load from the constant
6545   // pool.
6546   if (isConstant)
6547     return SDValue();
6548 
6549   // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
6550   if (NumElts >= 4) {
6551     SDValue shuffle = ReconstructShuffle(Op, DAG);
6552     if (shuffle != SDValue())
6553       return shuffle;
6554   }
6555 
6556   if (VT.is128BitVector() && VT != MVT::v2f64 && VT != MVT::v4f32) {
6557     // If we haven't found an efficient lowering, try splitting a 128-bit vector
6558     // into two 64-bit vectors; we might discover a better way to lower it.
6559     SmallVector<SDValue, 64> Ops(Op->op_begin(), Op->op_begin() + NumElts);
6560     EVT ExtVT = VT.getVectorElementType();
6561     EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElts / 2);
6562     SDValue Lower =
6563         DAG.getBuildVector(HVT, dl, makeArrayRef(&Ops[0], NumElts / 2));
6564     if (Lower.getOpcode() == ISD::BUILD_VECTOR)
6565       Lower = LowerBUILD_VECTOR(Lower, DAG, ST);
6566     SDValue Upper = DAG.getBuildVector(
6567         HVT, dl, makeArrayRef(&Ops[NumElts / 2], NumElts / 2));
6568     if (Upper.getOpcode() == ISD::BUILD_VECTOR)
6569       Upper = LowerBUILD_VECTOR(Upper, DAG, ST);
6570     if (Lower && Upper)
6571       return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lower, Upper);
6572   }
6573 
6574   // Vectors with 32- or 64-bit elements can be built by directly assigning
6575   // the subregisters.  Lower it to an ARMISD::BUILD_VECTOR so the operands
6576   // will be legalized.
6577   if (EltSize >= 32) {
6578     // Do the expansion with floating-point types, since that is what the VFP
6579     // registers are defined to use, and since i64 is not legal.
6580     EVT EltVT = EVT::getFloatingPointVT(EltSize);
6581     EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
6582     SmallVector<SDValue, 8> Ops;
6583     for (unsigned i = 0; i < NumElts; ++i)
6584       Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
6585     SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
6586     return DAG.getNode(ISD::BITCAST, dl, VT, Val);
6587   }
6588 
6589   // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
6590   // know the default expansion would otherwise fall back on something even
6591   // worse. For a vector with one or two non-undef values, that's
6592   // scalar_to_vector for the elements followed by a shuffle (provided the
6593   // shuffle is valid for the target) and materialization element by element
6594   // on the stack followed by a load for everything else.
6595   if (!isConstant && !usesOnlyOneValue) {
6596     SDValue Vec = DAG.getUNDEF(VT);
6597     for (unsigned i = 0 ; i < NumElts; ++i) {
6598       SDValue V = Op.getOperand(i);
6599       if (V.isUndef())
6600         continue;
6601       SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
6602       Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
6603     }
6604     return Vec;
6605   }
6606 
6607   return SDValue();
6608 }
6609 
6610 // Gather data to see if the operation can be modelled as a
6611 // shuffle in combination with VEXTs.
6612 SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
6613                                               SelectionDAG &DAG) const {
6614   assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
6615   SDLoc dl(Op);
6616   EVT VT = Op.getValueType();
6617   unsigned NumElts = VT.getVectorNumElements();
6618 
6619   struct ShuffleSourceInfo {
6620     SDValue Vec;
6621     unsigned MinElt = std::numeric_limits<unsigned>::max();
6622     unsigned MaxElt = 0;
6623 
6624     // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
6625     // be compatible with the shuffle we intend to construct. As a result
6626     // ShuffleVec will be some sliding window into the original Vec.
6627     SDValue ShuffleVec;
6628 
6629     // Code should guarantee that element i in Vec starts at element "WindowBase
6630     // + i * WindowScale in ShuffleVec".
6631     int WindowBase = 0;
6632     int WindowScale = 1;
6633 
6634     ShuffleSourceInfo(SDValue Vec) : Vec(Vec), ShuffleVec(Vec) {}
6635 
6636     bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
6637   };
6638 
6639   // First gather all vectors used as an immediate source for this BUILD_VECTOR
6640   // node.
6641   SmallVector<ShuffleSourceInfo, 2> Sources;
6642   for (unsigned i = 0; i < NumElts; ++i) {
6643     SDValue V = Op.getOperand(i);
6644     if (V.isUndef())
6645       continue;
6646     else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
6647       // A shuffle can only come from building a vector from various
6648       // elements of other vectors.
6649       return SDValue();
6650     } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
6651       // Furthermore, shuffles require a constant mask, whereas extractelts
6652       // accept variable indices.
6653       return SDValue();
6654     }
6655 
6656     // Add this element source to the list if it's not already there.
6657     SDValue SourceVec = V.getOperand(0);
6658     auto Source = llvm::find(Sources, SourceVec);
6659     if (Source == Sources.end())
6660       Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
6661 
6662     // Update the minimum and maximum lane number seen.
6663     unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
6664     Source->MinElt = std::min(Source->MinElt, EltNo);
6665     Source->MaxElt = std::max(Source->MaxElt, EltNo);
6666   }
6667 
6668   // Currently only do something sane when at most two source vectors
6669   // are involved.
6670   if (Sources.size() > 2)
6671     return SDValue();
6672 
6673   // Find out the smallest element size among result and two sources, and use
6674   // it as element size to build the shuffle_vector.
6675   EVT SmallestEltTy = VT.getVectorElementType();
6676   for (auto &Source : Sources) {
6677     EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
6678     if (SrcEltTy.bitsLT(SmallestEltTy))
6679       SmallestEltTy = SrcEltTy;
6680   }
6681   unsigned ResMultiplier =
6682       VT.getScalarSizeInBits() / SmallestEltTy.getSizeInBits();
6683   NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
6684   EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
6685 
6686   // If the source vector is too wide or too narrow, we may nevertheless be able
6687   // to construct a compatible shuffle either by concatenating it with UNDEF or
6688   // extracting a suitable range of elements.
6689   for (auto &Src : Sources) {
6690     EVT SrcVT = Src.ShuffleVec.getValueType();
6691 
6692     if (SrcVT.getSizeInBits() == VT.getSizeInBits())
6693       continue;
6694 
6695     // This stage of the search produces a source with the same element type as
6696     // the original, but with a total width matching the BUILD_VECTOR output.
6697     EVT EltVT = SrcVT.getVectorElementType();
6698     unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
6699     EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
6700 
6701     if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
6702       if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
6703         return SDValue();
6704       // We can pad out the smaller vector for free, so if it's part of a
6705       // shuffle...
6706       Src.ShuffleVec =
6707           DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
6708                       DAG.getUNDEF(Src.ShuffleVec.getValueType()));
6709       continue;
6710     }
6711 
6712     if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
6713       return SDValue();
6714 
6715     if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
6716       // Span too large for a VEXT to cope
6717       return SDValue();
6718     }
6719 
6720     if (Src.MinElt >= NumSrcElts) {
6721       // The extraction can just take the second half
6722       Src.ShuffleVec =
6723           DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6724                       DAG.getConstant(NumSrcElts, dl, MVT::i32));
6725       Src.WindowBase = -NumSrcElts;
6726     } else if (Src.MaxElt < NumSrcElts) {
6727       // The extraction can just take the first half
6728       Src.ShuffleVec =
6729           DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6730                       DAG.getConstant(0, dl, MVT::i32));
6731     } else {
6732       // An actual VEXT is needed
6733       SDValue VEXTSrc1 =
6734           DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6735                       DAG.getConstant(0, dl, MVT::i32));
6736       SDValue VEXTSrc2 =
6737           DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6738                       DAG.getConstant(NumSrcElts, dl, MVT::i32));
6739 
6740       Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
6741                                    VEXTSrc2,
6742                                    DAG.getConstant(Src.MinElt, dl, MVT::i32));
6743       Src.WindowBase = -Src.MinElt;
6744     }
6745   }
6746 
6747   // Another possible incompatibility occurs from the vector element types. We
6748   // can fix this by bitcasting the source vectors to the same type we intend
6749   // for the shuffle.
6750   for (auto &Src : Sources) {
6751     EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
6752     if (SrcEltTy == SmallestEltTy)
6753       continue;
6754     assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
6755     Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
6756     Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
6757     Src.WindowBase *= Src.WindowScale;
6758   }
6759 
6760   // Final sanity check before we try to actually produce a shuffle.
6761   LLVM_DEBUG(for (auto Src
6762                   : Sources)
6763                  assert(Src.ShuffleVec.getValueType() == ShuffleVT););
6764 
6765   // The stars all align, our next step is to produce the mask for the shuffle.
6766   SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
6767   int BitsPerShuffleLane = ShuffleVT.getScalarSizeInBits();
6768   for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
6769     SDValue Entry = Op.getOperand(i);
6770     if (Entry.isUndef())
6771       continue;
6772 
6773     auto Src = llvm::find(Sources, Entry.getOperand(0));
6774     int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
6775 
6776     // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
6777     // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
6778     // segment.
6779     EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
6780     int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
6781                                VT.getScalarSizeInBits());
6782     int LanesDefined = BitsDefined / BitsPerShuffleLane;
6783 
6784     // This source is expected to fill ResMultiplier lanes of the final shuffle,
6785     // starting at the appropriate offset.
6786     int *LaneMask = &Mask[i * ResMultiplier];
6787 
6788     int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
6789     ExtractBase += NumElts * (Src - Sources.begin());
6790     for (int j = 0; j < LanesDefined; ++j)
6791       LaneMask[j] = ExtractBase + j;
6792   }
6793 
6794   // Final check before we try to produce nonsense...
6795   if (!isShuffleMaskLegal(Mask, ShuffleVT))
6796     return SDValue();
6797 
6798   // We can't handle more than two sources. This should have already
6799   // been checked before this point.
6800   assert(Sources.size() <= 2 && "Too many sources!");
6801 
6802   SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
6803   for (unsigned i = 0; i < Sources.size(); ++i)
6804     ShuffleOps[i] = Sources[i].ShuffleVec;
6805 
6806   SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
6807                                          ShuffleOps[1], Mask);
6808   return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
6809 }
6810 
6811 /// isShuffleMaskLegal - Targets can use this to indicate that they only
6812 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6813 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6814 /// are assumed to be legal.
6815 bool ARMTargetLowering::isShuffleMaskLegal(ArrayRef<int> M, EVT VT) const {
6816   if (VT.getVectorNumElements() == 4 &&
6817       (VT.is128BitVector() || VT.is64BitVector())) {
6818     unsigned PFIndexes[4];
6819     for (unsigned i = 0; i != 4; ++i) {
6820       if (M[i] < 0)
6821         PFIndexes[i] = 8;
6822       else
6823         PFIndexes[i] = M[i];
6824     }
6825 
6826     // Compute the index in the perfect shuffle table.
6827     unsigned PFTableIndex =
6828       PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
6829     unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6830     unsigned Cost = (PFEntry >> 30);
6831 
6832     if (Cost <= 4)
6833       return true;
6834   }
6835 
6836   bool ReverseVEXT, isV_UNDEF;
6837   unsigned Imm, WhichResult;
6838 
6839   unsigned EltSize = VT.getScalarSizeInBits();
6840   return (EltSize >= 32 ||
6841           ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
6842           isVREVMask(M, VT, 64) ||
6843           isVREVMask(M, VT, 32) ||
6844           isVREVMask(M, VT, 16) ||
6845           isVEXTMask(M, VT, ReverseVEXT, Imm) ||
6846           isVTBLMask(M, VT) ||
6847           isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
6848           ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
6849 }
6850 
6851 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
6852 /// the specified operations to build the shuffle.
6853 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
6854                                       SDValue RHS, SelectionDAG &DAG,
6855                                       const SDLoc &dl) {
6856   unsigned OpNum = (PFEntry >> 26) & 0x0F;
6857   unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
6858   unsigned RHSID = (PFEntry >>  0) & ((1 << 13)-1);
6859 
6860   enum {
6861     OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
6862     OP_VREV,
6863     OP_VDUP0,
6864     OP_VDUP1,
6865     OP_VDUP2,
6866     OP_VDUP3,
6867     OP_VEXT1,
6868     OP_VEXT2,
6869     OP_VEXT3,
6870     OP_VUZPL, // VUZP, left result
6871     OP_VUZPR, // VUZP, right result
6872     OP_VZIPL, // VZIP, left result
6873     OP_VZIPR, // VZIP, right result
6874     OP_VTRNL, // VTRN, left result
6875     OP_VTRNR  // VTRN, right result
6876   };
6877 
6878   if (OpNum == OP_COPY) {
6879     if (LHSID == (1*9+2)*9+3) return LHS;
6880     assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
6881     return RHS;
6882   }
6883 
6884   SDValue OpLHS, OpRHS;
6885   OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6886   OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
6887   EVT VT = OpLHS.getValueType();
6888 
6889   switch (OpNum) {
6890   default: llvm_unreachable("Unknown shuffle opcode!");
6891   case OP_VREV:
6892     // VREV divides the vector in half and swaps within the half.
6893     if (VT.getVectorElementType() == MVT::i32 ||
6894         VT.getVectorElementType() == MVT::f32)
6895       return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
6896     // vrev <4 x i16> -> VREV32
6897     if (VT.getVectorElementType() == MVT::i16)
6898       return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
6899     // vrev <4 x i8> -> VREV16
6900     assert(VT.getVectorElementType() == MVT::i8);
6901     return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
6902   case OP_VDUP0:
6903   case OP_VDUP1:
6904   case OP_VDUP2:
6905   case OP_VDUP3:
6906     return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
6907                        OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
6908   case OP_VEXT1:
6909   case OP_VEXT2:
6910   case OP_VEXT3:
6911     return DAG.getNode(ARMISD::VEXT, dl, VT,
6912                        OpLHS, OpRHS,
6913                        DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
6914   case OP_VUZPL:
6915   case OP_VUZPR:
6916     return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
6917                        OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
6918   case OP_VZIPL:
6919   case OP_VZIPR:
6920     return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
6921                        OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
6922   case OP_VTRNL:
6923   case OP_VTRNR:
6924     return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
6925                        OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
6926   }
6927 }
6928 
6929 static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
6930                                        ArrayRef<int> ShuffleMask,
6931                                        SelectionDAG &DAG) {
6932   // Check to see if we can use the VTBL instruction.
6933   SDValue V1 = Op.getOperand(0);
6934   SDValue V2 = Op.getOperand(1);
6935   SDLoc DL(Op);
6936 
6937   SmallVector<SDValue, 8> VTBLMask;
6938   for (ArrayRef<int>::iterator
6939          I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
6940     VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
6941 
6942   if (V2.getNode()->isUndef())
6943     return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
6944                        DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
6945 
6946   return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
6947                      DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
6948 }
6949 
6950 static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
6951                                                       SelectionDAG &DAG) {
6952   SDLoc DL(Op);
6953   SDValue OpLHS = Op.getOperand(0);
6954   EVT VT = OpLHS.getValueType();
6955 
6956   assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
6957          "Expect an v8i16/v16i8 type");
6958   OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
6959   // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
6960   // extract the first 8 bytes into the top double word and the last 8 bytes
6961   // into the bottom double word. The v8i16 case is similar.
6962   unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
6963   return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
6964                      DAG.getConstant(ExtractNum, DL, MVT::i32));
6965 }
6966 
6967 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
6968   SDValue V1 = Op.getOperand(0);
6969   SDValue V2 = Op.getOperand(1);
6970   SDLoc dl(Op);
6971   EVT VT = Op.getValueType();
6972   ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
6973 
6974   // Convert shuffles that are directly supported on NEON to target-specific
6975   // DAG nodes, instead of keeping them as shuffles and matching them again
6976   // during code selection.  This is more efficient and avoids the possibility
6977   // of inconsistencies between legalization and selection.
6978   // FIXME: floating-point vectors should be canonicalized to integer vectors
6979   // of the same time so that they get CSEd properly.
6980   ArrayRef<int> ShuffleMask = SVN->getMask();
6981 
6982   unsigned EltSize = VT.getScalarSizeInBits();
6983   if (EltSize <= 32) {
6984     if (SVN->isSplat()) {
6985       int Lane = SVN->getSplatIndex();
6986       // If this is undef splat, generate it via "just" vdup, if possible.
6987       if (Lane == -1) Lane = 0;
6988 
6989       // Test if V1 is a SCALAR_TO_VECTOR.
6990       if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
6991         return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6992       }
6993       // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
6994       // (and probably will turn into a SCALAR_TO_VECTOR once legalization
6995       // reaches it).
6996       if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
6997           !isa<ConstantSDNode>(V1.getOperand(0))) {
6998         bool IsScalarToVector = true;
6999         for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
7000           if (!V1.getOperand(i).isUndef()) {
7001             IsScalarToVector = false;
7002             break;
7003           }
7004         if (IsScalarToVector)
7005           return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
7006       }
7007       return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
7008                          DAG.getConstant(Lane, dl, MVT::i32));
7009     }
7010 
7011     bool ReverseVEXT;
7012     unsigned Imm;
7013     if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
7014       if (ReverseVEXT)
7015         std::swap(V1, V2);
7016       return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
7017                          DAG.getConstant(Imm, dl, MVT::i32));
7018     }
7019 
7020     if (isVREVMask(ShuffleMask, VT, 64))
7021       return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
7022     if (isVREVMask(ShuffleMask, VT, 32))
7023       return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
7024     if (isVREVMask(ShuffleMask, VT, 16))
7025       return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
7026 
7027     if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
7028       return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
7029                          DAG.getConstant(Imm, dl, MVT::i32));
7030     }
7031 
7032     // Check for Neon shuffles that modify both input vectors in place.
7033     // If both results are used, i.e., if there are two shuffles with the same
7034     // source operands and with masks corresponding to both results of one of
7035     // these operations, DAG memoization will ensure that a single node is
7036     // used for both shuffles.
7037     unsigned WhichResult;
7038     bool isV_UNDEF;
7039     if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
7040             ShuffleMask, VT, WhichResult, isV_UNDEF)) {
7041       if (isV_UNDEF)
7042         V2 = V1;
7043       return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
7044           .getValue(WhichResult);
7045     }
7046 
7047     // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
7048     // shuffles that produce a result larger than their operands with:
7049     //   shuffle(concat(v1, undef), concat(v2, undef))
7050     // ->
7051     //   shuffle(concat(v1, v2), undef)
7052     // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
7053     //
7054     // This is useful in the general case, but there are special cases where
7055     // native shuffles produce larger results: the two-result ops.
7056     //
7057     // Look through the concat when lowering them:
7058     //   shuffle(concat(v1, v2), undef)
7059     // ->
7060     //   concat(VZIP(v1, v2):0, :1)
7061     //
7062     if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) {
7063       SDValue SubV1 = V1->getOperand(0);
7064       SDValue SubV2 = V1->getOperand(1);
7065       EVT SubVT = SubV1.getValueType();
7066 
7067       // We expect these to have been canonicalized to -1.
7068       assert(llvm::all_of(ShuffleMask, [&](int i) {
7069         return i < (int)VT.getVectorNumElements();
7070       }) && "Unexpected shuffle index into UNDEF operand!");
7071 
7072       if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
7073               ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
7074         if (isV_UNDEF)
7075           SubV2 = SubV1;
7076         assert((WhichResult == 0) &&
7077                "In-place shuffle of concat can only have one result!");
7078         SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
7079                                   SubV1, SubV2);
7080         return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
7081                            Res.getValue(1));
7082       }
7083     }
7084   }
7085 
7086   // If the shuffle is not directly supported and it has 4 elements, use
7087   // the PerfectShuffle-generated table to synthesize it from other shuffles.
7088   unsigned NumElts = VT.getVectorNumElements();
7089   if (NumElts == 4) {
7090     unsigned PFIndexes[4];
7091     for (unsigned i = 0; i != 4; ++i) {
7092       if (ShuffleMask[i] < 0)
7093         PFIndexes[i] = 8;
7094       else
7095         PFIndexes[i] = ShuffleMask[i];
7096     }
7097 
7098     // Compute the index in the perfect shuffle table.
7099     unsigned PFTableIndex =
7100       PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
7101     unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
7102     unsigned Cost = (PFEntry >> 30);
7103 
7104     if (Cost <= 4)
7105       return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
7106   }
7107 
7108   // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
7109   if (EltSize >= 32) {
7110     // Do the expansion with floating-point types, since that is what the VFP
7111     // registers are defined to use, and since i64 is not legal.
7112     EVT EltVT = EVT::getFloatingPointVT(EltSize);
7113     EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
7114     V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
7115     V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
7116     SmallVector<SDValue, 8> Ops;
7117     for (unsigned i = 0; i < NumElts; ++i) {
7118       if (ShuffleMask[i] < 0)
7119         Ops.push_back(DAG.getUNDEF(EltVT));
7120       else
7121         Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
7122                                   ShuffleMask[i] < (int)NumElts ? V1 : V2,
7123                                   DAG.getConstant(ShuffleMask[i] & (NumElts-1),
7124                                                   dl, MVT::i32)));
7125     }
7126     SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
7127     return DAG.getNode(ISD::BITCAST, dl, VT, Val);
7128   }
7129 
7130   if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
7131     return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
7132 
7133   if (VT == MVT::v8i8)
7134     if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG))
7135       return NewOp;
7136 
7137   return SDValue();
7138 }
7139 
7140 static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
7141   // INSERT_VECTOR_ELT is legal only for immediate indexes.
7142   SDValue Lane = Op.getOperand(2);
7143   if (!isa<ConstantSDNode>(Lane))
7144     return SDValue();
7145 
7146   return Op;
7147 }
7148 
7149 static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
7150   // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
7151   SDValue Lane = Op.getOperand(1);
7152   if (!isa<ConstantSDNode>(Lane))
7153     return SDValue();
7154 
7155   SDValue Vec = Op.getOperand(0);
7156   if (Op.getValueType() == MVT::i32 && Vec.getScalarValueSizeInBits() < 32) {
7157     SDLoc dl(Op);
7158     return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
7159   }
7160 
7161   return Op;
7162 }
7163 
7164 static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
7165   // The only time a CONCAT_VECTORS operation can have legal types is when
7166   // two 64-bit vectors are concatenated to a 128-bit vector.
7167   assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
7168          "unexpected CONCAT_VECTORS");
7169   SDLoc dl(Op);
7170   SDValue Val = DAG.getUNDEF(MVT::v2f64);
7171   SDValue Op0 = Op.getOperand(0);
7172   SDValue Op1 = Op.getOperand(1);
7173   if (!Op0.isUndef())
7174     Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
7175                       DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
7176                       DAG.getIntPtrConstant(0, dl));
7177   if (!Op1.isUndef())
7178     Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
7179                       DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
7180                       DAG.getIntPtrConstant(1, dl));
7181   return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
7182 }
7183 
7184 /// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
7185 /// element has been zero/sign-extended, depending on the isSigned parameter,
7186 /// from an integer type half its size.
7187 static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
7188                                    bool isSigned) {
7189   // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
7190   EVT VT = N->getValueType(0);
7191   if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
7192     SDNode *BVN = N->getOperand(0).getNode();
7193     if (BVN->getValueType(0) != MVT::v4i32 ||
7194         BVN->getOpcode() != ISD::BUILD_VECTOR)
7195       return false;
7196     unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
7197     unsigned HiElt = 1 - LoElt;
7198     ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
7199     ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
7200     ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
7201     ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
7202     if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
7203       return false;
7204     if (isSigned) {
7205       if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
7206           Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
7207         return true;
7208     } else {
7209       if (Hi0->isNullValue() && Hi1->isNullValue())
7210         return true;
7211     }
7212     return false;
7213   }
7214 
7215   if (N->getOpcode() != ISD::BUILD_VECTOR)
7216     return false;
7217 
7218   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
7219     SDNode *Elt = N->getOperand(i).getNode();
7220     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
7221       unsigned EltSize = VT.getScalarSizeInBits();
7222       unsigned HalfSize = EltSize / 2;
7223       if (isSigned) {
7224         if (!isIntN(HalfSize, C->getSExtValue()))
7225           return false;
7226       } else {
7227         if (!isUIntN(HalfSize, C->getZExtValue()))
7228           return false;
7229       }
7230       continue;
7231     }
7232     return false;
7233   }
7234 
7235   return true;
7236 }
7237 
7238 /// isSignExtended - Check if a node is a vector value that is sign-extended
7239 /// or a constant BUILD_VECTOR with sign-extended elements.
7240 static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
7241   if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
7242     return true;
7243   if (isExtendedBUILD_VECTOR(N, DAG, true))
7244     return true;
7245   return false;
7246 }
7247 
7248 /// isZeroExtended - Check if a node is a vector value that is zero-extended
7249 /// or a constant BUILD_VECTOR with zero-extended elements.
7250 static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
7251   if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
7252     return true;
7253   if (isExtendedBUILD_VECTOR(N, DAG, false))
7254     return true;
7255   return false;
7256 }
7257 
7258 static EVT getExtensionTo64Bits(const EVT &OrigVT) {
7259   if (OrigVT.getSizeInBits() >= 64)
7260     return OrigVT;
7261 
7262   assert(OrigVT.isSimple() && "Expecting a simple value type");
7263 
7264   MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
7265   switch (OrigSimpleTy) {
7266   default: llvm_unreachable("Unexpected Vector Type");
7267   case MVT::v2i8:
7268   case MVT::v2i16:
7269      return MVT::v2i32;
7270   case MVT::v4i8:
7271     return  MVT::v4i16;
7272   }
7273 }
7274 
7275 /// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
7276 /// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
7277 /// We insert the required extension here to get the vector to fill a D register.
7278 static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
7279                                             const EVT &OrigTy,
7280                                             const EVT &ExtTy,
7281                                             unsigned ExtOpcode) {
7282   // The vector originally had a size of OrigTy. It was then extended to ExtTy.
7283   // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
7284   // 64-bits we need to insert a new extension so that it will be 64-bits.
7285   assert(ExtTy.is128BitVector() && "Unexpected extension size");
7286   if (OrigTy.getSizeInBits() >= 64)
7287     return N;
7288 
7289   // Must extend size to at least 64 bits to be used as an operand for VMULL.
7290   EVT NewVT = getExtensionTo64Bits(OrigTy);
7291 
7292   return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
7293 }
7294 
7295 /// SkipLoadExtensionForVMULL - return a load of the original vector size that
7296 /// does not do any sign/zero extension. If the original vector is less
7297 /// than 64 bits, an appropriate extension will be added after the load to
7298 /// reach a total size of 64 bits. We have to add the extension separately
7299 /// because ARM does not have a sign/zero extending load for vectors.
7300 static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
7301   EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
7302 
7303   // The load already has the right type.
7304   if (ExtendedTy == LD->getMemoryVT())
7305     return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
7306                        LD->getBasePtr(), LD->getPointerInfo(),
7307                        LD->getAlignment(), LD->getMemOperand()->getFlags());
7308 
7309   // We need to create a zextload/sextload. We cannot just create a load
7310   // followed by a zext/zext node because LowerMUL is also run during normal
7311   // operation legalization where we can't create illegal types.
7312   return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
7313                         LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
7314                         LD->getMemoryVT(), LD->getAlignment(),
7315                         LD->getMemOperand()->getFlags());
7316 }
7317 
7318 /// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
7319 /// extending load, or BUILD_VECTOR with extended elements, return the
7320 /// unextended value. The unextended vector should be 64 bits so that it can
7321 /// be used as an operand to a VMULL instruction. If the original vector size
7322 /// before extension is less than 64 bits we add a an extension to resize
7323 /// the vector to 64 bits.
7324 static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
7325   if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
7326     return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
7327                                         N->getOperand(0)->getValueType(0),
7328                                         N->getValueType(0),
7329                                         N->getOpcode());
7330 
7331   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
7332     assert((ISD::isSEXTLoad(LD) || ISD::isZEXTLoad(LD)) &&
7333            "Expected extending load");
7334 
7335     SDValue newLoad = SkipLoadExtensionForVMULL(LD, DAG);
7336     DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), newLoad.getValue(1));
7337     unsigned Opcode = ISD::isSEXTLoad(LD) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
7338     SDValue extLoad =
7339         DAG.getNode(Opcode, SDLoc(newLoad), LD->getValueType(0), newLoad);
7340     DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 0), extLoad);
7341 
7342     return newLoad;
7343   }
7344 
7345   // Otherwise, the value must be a BUILD_VECTOR.  For v2i64, it will
7346   // have been legalized as a BITCAST from v4i32.
7347   if (N->getOpcode() == ISD::BITCAST) {
7348     SDNode *BVN = N->getOperand(0).getNode();
7349     assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
7350            BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
7351     unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
7352     return DAG.getBuildVector(
7353         MVT::v2i32, SDLoc(N),
7354         {BVN->getOperand(LowElt), BVN->getOperand(LowElt + 2)});
7355   }
7356   // Construct a new BUILD_VECTOR with elements truncated to half the size.
7357   assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
7358   EVT VT = N->getValueType(0);
7359   unsigned EltSize = VT.getScalarSizeInBits() / 2;
7360   unsigned NumElts = VT.getVectorNumElements();
7361   MVT TruncVT = MVT::getIntegerVT(EltSize);
7362   SmallVector<SDValue, 8> Ops;
7363   SDLoc dl(N);
7364   for (unsigned i = 0; i != NumElts; ++i) {
7365     ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
7366     const APInt &CInt = C->getAPIntValue();
7367     // Element types smaller than 32 bits are not legal, so use i32 elements.
7368     // The values are implicitly truncated so sext vs. zext doesn't matter.
7369     Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
7370   }
7371   return DAG.getBuildVector(MVT::getVectorVT(TruncVT, NumElts), dl, Ops);
7372 }
7373 
7374 static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
7375   unsigned Opcode = N->getOpcode();
7376   if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
7377     SDNode *N0 = N->getOperand(0).getNode();
7378     SDNode *N1 = N->getOperand(1).getNode();
7379     return N0->hasOneUse() && N1->hasOneUse() &&
7380       isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
7381   }
7382   return false;
7383 }
7384 
7385 static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
7386   unsigned Opcode = N->getOpcode();
7387   if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
7388     SDNode *N0 = N->getOperand(0).getNode();
7389     SDNode *N1 = N->getOperand(1).getNode();
7390     return N0->hasOneUse() && N1->hasOneUse() &&
7391       isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
7392   }
7393   return false;
7394 }
7395 
7396 static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
7397   // Multiplications are only custom-lowered for 128-bit vectors so that
7398   // VMULL can be detected.  Otherwise v2i64 multiplications are not legal.
7399   EVT VT = Op.getValueType();
7400   assert(VT.is128BitVector() && VT.isInteger() &&
7401          "unexpected type for custom-lowering ISD::MUL");
7402   SDNode *N0 = Op.getOperand(0).getNode();
7403   SDNode *N1 = Op.getOperand(1).getNode();
7404   unsigned NewOpc = 0;
7405   bool isMLA = false;
7406   bool isN0SExt = isSignExtended(N0, DAG);
7407   bool isN1SExt = isSignExtended(N1, DAG);
7408   if (isN0SExt && isN1SExt)
7409     NewOpc = ARMISD::VMULLs;
7410   else {
7411     bool isN0ZExt = isZeroExtended(N0, DAG);
7412     bool isN1ZExt = isZeroExtended(N1, DAG);
7413     if (isN0ZExt && isN1ZExt)
7414       NewOpc = ARMISD::VMULLu;
7415     else if (isN1SExt || isN1ZExt) {
7416       // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
7417       // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
7418       if (isN1SExt && isAddSubSExt(N0, DAG)) {
7419         NewOpc = ARMISD::VMULLs;
7420         isMLA = true;
7421       } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
7422         NewOpc = ARMISD::VMULLu;
7423         isMLA = true;
7424       } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
7425         std::swap(N0, N1);
7426         NewOpc = ARMISD::VMULLu;
7427         isMLA = true;
7428       }
7429     }
7430 
7431     if (!NewOpc) {
7432       if (VT == MVT::v2i64)
7433         // Fall through to expand this.  It is not legal.
7434         return SDValue();
7435       else
7436         // Other vector multiplications are legal.
7437         return Op;
7438     }
7439   }
7440 
7441   // Legalize to a VMULL instruction.
7442   SDLoc DL(Op);
7443   SDValue Op0;
7444   SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
7445   if (!isMLA) {
7446     Op0 = SkipExtensionForVMULL(N0, DAG);
7447     assert(Op0.getValueType().is64BitVector() &&
7448            Op1.getValueType().is64BitVector() &&
7449            "unexpected types for extended operands to VMULL");
7450     return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
7451   }
7452 
7453   // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
7454   // isel lowering to take advantage of no-stall back to back vmul + vmla.
7455   //   vmull q0, d4, d6
7456   //   vmlal q0, d5, d6
7457   // is faster than
7458   //   vaddl q0, d4, d5
7459   //   vmovl q1, d6
7460   //   vmul  q0, q0, q1
7461   SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
7462   SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
7463   EVT Op1VT = Op1.getValueType();
7464   return DAG.getNode(N0->getOpcode(), DL, VT,
7465                      DAG.getNode(NewOpc, DL, VT,
7466                                DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
7467                      DAG.getNode(NewOpc, DL, VT,
7468                                DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
7469 }
7470 
7471 static SDValue LowerSDIV_v4i8(SDValue X, SDValue Y, const SDLoc &dl,
7472                               SelectionDAG &DAG) {
7473   // TODO: Should this propagate fast-math-flags?
7474 
7475   // Convert to float
7476   // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
7477   // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
7478   X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
7479   Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
7480   X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
7481   Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
7482   // Get reciprocal estimate.
7483   // float4 recip = vrecpeq_f32(yf);
7484   Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
7485                    DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
7486                    Y);
7487   // Because char has a smaller range than uchar, we can actually get away
7488   // without any newton steps.  This requires that we use a weird bias
7489   // of 0xb000, however (again, this has been exhaustively tested).
7490   // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
7491   X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
7492   X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
7493   Y = DAG.getConstant(0xb000, dl, MVT::v4i32);
7494   X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
7495   X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
7496   // Convert back to short.
7497   X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
7498   X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
7499   return X;
7500 }
7501 
7502 static SDValue LowerSDIV_v4i16(SDValue N0, SDValue N1, const SDLoc &dl,
7503                                SelectionDAG &DAG) {
7504   // TODO: Should this propagate fast-math-flags?
7505 
7506   SDValue N2;
7507   // Convert to float.
7508   // float4 yf = vcvt_f32_s32(vmovl_s16(y));
7509   // float4 xf = vcvt_f32_s32(vmovl_s16(x));
7510   N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
7511   N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
7512   N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
7513   N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
7514 
7515   // Use reciprocal estimate and one refinement step.
7516   // float4 recip = vrecpeq_f32(yf);
7517   // recip *= vrecpsq_f32(yf, recip);
7518   N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
7519                    DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
7520                    N1);
7521   N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
7522                    DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
7523                    N1, N2);
7524   N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
7525   // Because short has a smaller range than ushort, we can actually get away
7526   // with only a single newton step.  This requires that we use a weird bias
7527   // of 89, however (again, this has been exhaustively tested).
7528   // float4 result = as_float4(as_int4(xf*recip) + 0x89);
7529   N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
7530   N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
7531   N1 = DAG.getConstant(0x89, dl, MVT::v4i32);
7532   N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
7533   N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
7534   // Convert back to integer and return.
7535   // return vmovn_s32(vcvt_s32_f32(result));
7536   N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
7537   N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
7538   return N0;
7539 }
7540 
7541 static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
7542   EVT VT = Op.getValueType();
7543   assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
7544          "unexpected type for custom-lowering ISD::SDIV");
7545 
7546   SDLoc dl(Op);
7547   SDValue N0 = Op.getOperand(0);
7548   SDValue N1 = Op.getOperand(1);
7549   SDValue N2, N3;
7550 
7551   if (VT == MVT::v8i8) {
7552     N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
7553     N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
7554 
7555     N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
7556                      DAG.getIntPtrConstant(4, dl));
7557     N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
7558                      DAG.getIntPtrConstant(4, dl));
7559     N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
7560                      DAG.getIntPtrConstant(0, dl));
7561     N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
7562                      DAG.getIntPtrConstant(0, dl));
7563 
7564     N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
7565     N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
7566 
7567     N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
7568     N0 = LowerCONCAT_VECTORS(N0, DAG);
7569 
7570     N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
7571     return N0;
7572   }
7573   return LowerSDIV_v4i16(N0, N1, dl, DAG);
7574 }
7575 
7576 static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
7577   // TODO: Should this propagate fast-math-flags?
7578   EVT VT = Op.getValueType();
7579   assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
7580          "unexpected type for custom-lowering ISD::UDIV");
7581 
7582   SDLoc dl(Op);
7583   SDValue N0 = Op.getOperand(0);
7584   SDValue N1 = Op.getOperand(1);
7585   SDValue N2, N3;
7586 
7587   if (VT == MVT::v8i8) {
7588     N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
7589     N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
7590 
7591     N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
7592                      DAG.getIntPtrConstant(4, dl));
7593     N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
7594                      DAG.getIntPtrConstant(4, dl));
7595     N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
7596                      DAG.getIntPtrConstant(0, dl));
7597     N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
7598                      DAG.getIntPtrConstant(0, dl));
7599 
7600     N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
7601     N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
7602 
7603     N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
7604     N0 = LowerCONCAT_VECTORS(N0, DAG);
7605 
7606     N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
7607                      DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
7608                                      MVT::i32),
7609                      N0);
7610     return N0;
7611   }
7612 
7613   // v4i16 sdiv ... Convert to float.
7614   // float4 yf = vcvt_f32_s32(vmovl_u16(y));
7615   // float4 xf = vcvt_f32_s32(vmovl_u16(x));
7616   N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
7617   N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
7618   N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
7619   SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
7620 
7621   // Use reciprocal estimate and two refinement steps.
7622   // float4 recip = vrecpeq_f32(yf);
7623   // recip *= vrecpsq_f32(yf, recip);
7624   // recip *= vrecpsq_f32(yf, recip);
7625   N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
7626                    DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
7627                    BN1);
7628   N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
7629                    DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
7630                    BN1, N2);
7631   N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
7632   N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
7633                    DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
7634                    BN1, N2);
7635   N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
7636   // Simply multiplying by the reciprocal estimate can leave us a few ulps
7637   // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
7638   // and that it will never cause us to return an answer too large).
7639   // float4 result = as_float4(as_int4(xf*recip) + 2);
7640   N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
7641   N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
7642   N1 = DAG.getConstant(2, dl, MVT::v4i32);
7643   N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
7644   N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
7645   // Convert back to integer and return.
7646   // return vmovn_u32(vcvt_s32_f32(result));
7647   N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
7648   N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
7649   return N0;
7650 }
7651 
7652 static SDValue LowerADDSUBCARRY(SDValue Op, SelectionDAG &DAG) {
7653   SDNode *N = Op.getNode();
7654   EVT VT = N->getValueType(0);
7655   SDVTList VTs = DAG.getVTList(VT, MVT::i32);
7656 
7657   SDValue Carry = Op.getOperand(2);
7658 
7659   SDLoc DL(Op);
7660 
7661   SDValue Result;
7662   if (Op.getOpcode() == ISD::ADDCARRY) {
7663     // This converts the boolean value carry into the carry flag.
7664     Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG);
7665 
7666     // Do the addition proper using the carry flag we wanted.
7667     Result = DAG.getNode(ARMISD::ADDE, DL, VTs, Op.getOperand(0),
7668                          Op.getOperand(1), Carry);
7669 
7670     // Now convert the carry flag into a boolean value.
7671     Carry = ConvertCarryFlagToBooleanCarry(Result.getValue(1), VT, DAG);
7672   } else {
7673     // ARMISD::SUBE expects a carry not a borrow like ISD::SUBCARRY so we
7674     // have to invert the carry first.
7675     Carry = DAG.getNode(ISD::SUB, DL, MVT::i32,
7676                         DAG.getConstant(1, DL, MVT::i32), Carry);
7677     // This converts the boolean value carry into the carry flag.
7678     Carry = ConvertBooleanCarryToCarryFlag(Carry, DAG);
7679 
7680     // Do the subtraction proper using the carry flag we wanted.
7681     Result = DAG.getNode(ARMISD::SUBE, DL, VTs, Op.getOperand(0),
7682                          Op.getOperand(1), Carry);
7683 
7684     // Now convert the carry flag into a boolean value.
7685     Carry = ConvertCarryFlagToBooleanCarry(Result.getValue(1), VT, DAG);
7686     // But the carry returned by ARMISD::SUBE is not a borrow as expected
7687     // by ISD::SUBCARRY, so compute 1 - C.
7688     Carry = DAG.getNode(ISD::SUB, DL, MVT::i32,
7689                         DAG.getConstant(1, DL, MVT::i32), Carry);
7690   }
7691 
7692   // Return both values.
7693   return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, Carry);
7694 }
7695 
7696 SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
7697   assert(Subtarget->isTargetDarwin());
7698 
7699   // For iOS, we want to call an alternative entry point: __sincos_stret,
7700   // return values are passed via sret.
7701   SDLoc dl(Op);
7702   SDValue Arg = Op.getOperand(0);
7703   EVT ArgVT = Arg.getValueType();
7704   Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7705   auto PtrVT = getPointerTy(DAG.getDataLayout());
7706 
7707   MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
7708   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7709 
7710   // Pair of floats / doubles used to pass the result.
7711   Type *RetTy = StructType::get(ArgTy, ArgTy);
7712   auto &DL = DAG.getDataLayout();
7713 
7714   ArgListTy Args;
7715   bool ShouldUseSRet = Subtarget->isAPCS_ABI();
7716   SDValue SRet;
7717   if (ShouldUseSRet) {
7718     // Create stack object for sret.
7719     const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
7720     const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
7721     int FrameIdx = MFI.CreateStackObject(ByteSize, StackAlign, false);
7722     SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
7723 
7724     ArgListEntry Entry;
7725     Entry.Node = SRet;
7726     Entry.Ty = RetTy->getPointerTo();
7727     Entry.IsSExt = false;
7728     Entry.IsZExt = false;
7729     Entry.IsSRet = true;
7730     Args.push_back(Entry);
7731     RetTy = Type::getVoidTy(*DAG.getContext());
7732   }
7733 
7734   ArgListEntry Entry;
7735   Entry.Node = Arg;
7736   Entry.Ty = ArgTy;
7737   Entry.IsSExt = false;
7738   Entry.IsZExt = false;
7739   Args.push_back(Entry);
7740 
7741   RTLIB::Libcall LC =
7742       (ArgVT == MVT::f64) ? RTLIB::SINCOS_STRET_F64 : RTLIB::SINCOS_STRET_F32;
7743   const char *LibcallName = getLibcallName(LC);
7744   CallingConv::ID CC = getLibcallCallingConv(LC);
7745   SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
7746 
7747   TargetLowering::CallLoweringInfo CLI(DAG);
7748   CLI.setDebugLoc(dl)
7749       .setChain(DAG.getEntryNode())
7750       .setCallee(CC, RetTy, Callee, std::move(Args))
7751       .setDiscardResult(ShouldUseSRet);
7752   std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
7753 
7754   if (!ShouldUseSRet)
7755     return CallResult.first;
7756 
7757   SDValue LoadSin =
7758       DAG.getLoad(ArgVT, dl, CallResult.second, SRet, MachinePointerInfo());
7759 
7760   // Address of cos field.
7761   SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
7762                             DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
7763   SDValue LoadCos =
7764       DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, MachinePointerInfo());
7765 
7766   SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
7767   return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
7768                      LoadSin.getValue(0), LoadCos.getValue(0));
7769 }
7770 
7771 SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
7772                                                   bool Signed,
7773                                                   SDValue &Chain) const {
7774   EVT VT = Op.getValueType();
7775   assert((VT == MVT::i32 || VT == MVT::i64) &&
7776          "unexpected type for custom lowering DIV");
7777   SDLoc dl(Op);
7778 
7779   const auto &DL = DAG.getDataLayout();
7780   const auto &TLI = DAG.getTargetLoweringInfo();
7781 
7782   const char *Name = nullptr;
7783   if (Signed)
7784     Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
7785   else
7786     Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
7787 
7788   SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
7789 
7790   ARMTargetLowering::ArgListTy Args;
7791 
7792   for (auto AI : {1, 0}) {
7793     ArgListEntry Arg;
7794     Arg.Node = Op.getOperand(AI);
7795     Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
7796     Args.push_back(Arg);
7797   }
7798 
7799   CallLoweringInfo CLI(DAG);
7800   CLI.setDebugLoc(dl)
7801     .setChain(Chain)
7802     .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
7803                ES, std::move(Args));
7804 
7805   return LowerCallTo(CLI).first;
7806 }
7807 
7808 // This is a code size optimisation: return the original SDIV node to
7809 // DAGCombiner when we don't want to expand SDIV into a sequence of
7810 // instructions, and an empty node otherwise which will cause the
7811 // SDIV to be expanded in DAGCombine.
7812 SDValue
7813 ARMTargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
7814                                  SelectionDAG &DAG,
7815                                  SmallVectorImpl<SDNode *> &Created) const {
7816   // TODO: Support SREM
7817   if (N->getOpcode() != ISD::SDIV)
7818     return SDValue();
7819 
7820   const auto &ST = static_cast<const ARMSubtarget&>(DAG.getSubtarget());
7821   const bool MinSize = ST.optForMinSize();
7822   const bool HasDivide = ST.isThumb() ? ST.hasDivideInThumbMode()
7823                                       : ST.hasDivideInARMMode();
7824 
7825   // Don't touch vector types; rewriting this may lead to scalarizing
7826   // the int divs.
7827   if (N->getOperand(0).getValueType().isVector())
7828     return SDValue();
7829 
7830   // Bail if MinSize is not set, and also for both ARM and Thumb mode we need
7831   // hwdiv support for this to be really profitable.
7832   if (!(MinSize && HasDivide))
7833     return SDValue();
7834 
7835   // ARM mode is a bit simpler than Thumb: we can handle large power
7836   // of 2 immediates with 1 mov instruction; no further checks required,
7837   // just return the sdiv node.
7838   if (!ST.isThumb())
7839     return SDValue(N, 0);
7840 
7841   // In Thumb mode, immediates larger than 128 need a wide 4-byte MOV,
7842   // and thus lose the code size benefits of a MOVS that requires only 2.
7843   // TargetTransformInfo and 'getIntImmCodeSizeCost' could be helpful here,
7844   // but as it's doing exactly this, it's not worth the trouble to get TTI.
7845   if (Divisor.sgt(128))
7846     return SDValue();
7847 
7848   return SDValue(N, 0);
7849 }
7850 
7851 SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
7852                                             bool Signed) const {
7853   assert(Op.getValueType() == MVT::i32 &&
7854          "unexpected type for custom lowering DIV");
7855   SDLoc dl(Op);
7856 
7857   SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
7858                                DAG.getEntryNode(), Op.getOperand(1));
7859 
7860   return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
7861 }
7862 
7863 static SDValue WinDBZCheckDenominator(SelectionDAG &DAG, SDNode *N, SDValue InChain) {
7864   SDLoc DL(N);
7865   SDValue Op = N->getOperand(1);
7866   if (N->getValueType(0) == MVT::i32)
7867     return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain, Op);
7868   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op,
7869                            DAG.getConstant(0, DL, MVT::i32));
7870   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op,
7871                            DAG.getConstant(1, DL, MVT::i32));
7872   return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain,
7873                      DAG.getNode(ISD::OR, DL, MVT::i32, Lo, Hi));
7874 }
7875 
7876 void ARMTargetLowering::ExpandDIV_Windows(
7877     SDValue Op, SelectionDAG &DAG, bool Signed,
7878     SmallVectorImpl<SDValue> &Results) const {
7879   const auto &DL = DAG.getDataLayout();
7880   const auto &TLI = DAG.getTargetLoweringInfo();
7881 
7882   assert(Op.getValueType() == MVT::i64 &&
7883          "unexpected type for custom lowering DIV");
7884   SDLoc dl(Op);
7885 
7886   SDValue DBZCHK = WinDBZCheckDenominator(DAG, Op.getNode(), DAG.getEntryNode());
7887 
7888   SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
7889 
7890   SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
7891   SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
7892                               DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
7893   Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
7894 
7895   Results.push_back(Lower);
7896   Results.push_back(Upper);
7897 }
7898 
7899 static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
7900   if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
7901     // Acquire/Release load/store is not legal for targets without a dmb or
7902     // equivalent available.
7903     return SDValue();
7904 
7905   // Monotonic load/store is legal for all targets.
7906   return Op;
7907 }
7908 
7909 static void ReplaceREADCYCLECOUNTER(SDNode *N,
7910                                     SmallVectorImpl<SDValue> &Results,
7911                                     SelectionDAG &DAG,
7912                                     const ARMSubtarget *Subtarget) {
7913   SDLoc DL(N);
7914   // Under Power Management extensions, the cycle-count is:
7915   //    mrc p15, #0, <Rt>, c9, c13, #0
7916   SDValue Ops[] = { N->getOperand(0), // Chain
7917                     DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
7918                     DAG.getConstant(15, DL, MVT::i32),
7919                     DAG.getConstant(0, DL, MVT::i32),
7920                     DAG.getConstant(9, DL, MVT::i32),
7921                     DAG.getConstant(13, DL, MVT::i32),
7922                     DAG.getConstant(0, DL, MVT::i32)
7923   };
7924 
7925   SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
7926                                  DAG.getVTList(MVT::i32, MVT::Other), Ops);
7927   Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
7928                                 DAG.getConstant(0, DL, MVT::i32)));
7929   Results.push_back(Cycles32.getValue(1));
7930 }
7931 
7932 static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V) {
7933   SDLoc dl(V.getNode());
7934   SDValue VLo = DAG.getAnyExtOrTrunc(V, dl, MVT::i32);
7935   SDValue VHi = DAG.getAnyExtOrTrunc(
7936       DAG.getNode(ISD::SRL, dl, MVT::i64, V, DAG.getConstant(32, dl, MVT::i32)),
7937       dl, MVT::i32);
7938   bool isBigEndian = DAG.getDataLayout().isBigEndian();
7939   if (isBigEndian)
7940     std::swap (VLo, VHi);
7941   SDValue RegClass =
7942       DAG.getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
7943   SDValue SubReg0 = DAG.getTargetConstant(ARM::gsub_0, dl, MVT::i32);
7944   SDValue SubReg1 = DAG.getTargetConstant(ARM::gsub_1, dl, MVT::i32);
7945   const SDValue Ops[] = { RegClass, VLo, SubReg0, VHi, SubReg1 };
7946   return SDValue(
7947       DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0);
7948 }
7949 
7950 static void ReplaceCMP_SWAP_64Results(SDNode *N,
7951                                        SmallVectorImpl<SDValue> & Results,
7952                                        SelectionDAG &DAG) {
7953   assert(N->getValueType(0) == MVT::i64 &&
7954          "AtomicCmpSwap on types less than 64 should be legal");
7955   SDValue Ops[] = {N->getOperand(1),
7956                    createGPRPairNode(DAG, N->getOperand(2)),
7957                    createGPRPairNode(DAG, N->getOperand(3)),
7958                    N->getOperand(0)};
7959   SDNode *CmpSwap = DAG.getMachineNode(
7960       ARM::CMP_SWAP_64, SDLoc(N),
7961       DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other), Ops);
7962 
7963   MachineMemOperand *MemOp = cast<MemSDNode>(N)->getMemOperand();
7964   DAG.setNodeMemRefs(cast<MachineSDNode>(CmpSwap), {MemOp});
7965 
7966   bool isBigEndian = DAG.getDataLayout().isBigEndian();
7967 
7968   Results.push_back(
7969       DAG.getTargetExtractSubreg(isBigEndian ? ARM::gsub_1 : ARM::gsub_0,
7970                                  SDLoc(N), MVT::i32, SDValue(CmpSwap, 0)));
7971   Results.push_back(
7972       DAG.getTargetExtractSubreg(isBigEndian ? ARM::gsub_0 : ARM::gsub_1,
7973                                  SDLoc(N), MVT::i32, SDValue(CmpSwap, 0)));
7974   Results.push_back(SDValue(CmpSwap, 2));
7975 }
7976 
7977 static SDValue LowerFPOWI(SDValue Op, const ARMSubtarget &Subtarget,
7978                           SelectionDAG &DAG) {
7979   const auto &TLI = DAG.getTargetLoweringInfo();
7980 
7981   assert(Subtarget.getTargetTriple().isOSMSVCRT() &&
7982          "Custom lowering is MSVCRT specific!");
7983 
7984   SDLoc dl(Op);
7985   SDValue Val = Op.getOperand(0);
7986   MVT Ty = Val->getSimpleValueType(0);
7987   SDValue Exponent = DAG.getNode(ISD::SINT_TO_FP, dl, Ty, Op.getOperand(1));
7988   SDValue Callee = DAG.getExternalSymbol(Ty == MVT::f32 ? "powf" : "pow",
7989                                          TLI.getPointerTy(DAG.getDataLayout()));
7990 
7991   TargetLowering::ArgListTy Args;
7992   TargetLowering::ArgListEntry Entry;
7993 
7994   Entry.Node = Val;
7995   Entry.Ty = Val.getValueType().getTypeForEVT(*DAG.getContext());
7996   Entry.IsZExt = true;
7997   Args.push_back(Entry);
7998 
7999   Entry.Node = Exponent;
8000   Entry.Ty = Exponent.getValueType().getTypeForEVT(*DAG.getContext());
8001   Entry.IsZExt = true;
8002   Args.push_back(Entry);
8003 
8004   Type *LCRTy = Val.getValueType().getTypeForEVT(*DAG.getContext());
8005 
8006   // In the in-chain to the call is the entry node  If we are emitting a
8007   // tailcall, the chain will be mutated if the node has a non-entry input
8008   // chain.
8009   SDValue InChain = DAG.getEntryNode();
8010   SDValue TCChain = InChain;
8011 
8012   const Function &F = DAG.getMachineFunction().getFunction();
8013   bool IsTC = TLI.isInTailCallPosition(DAG, Op.getNode(), TCChain) &&
8014               F.getReturnType() == LCRTy;
8015   if (IsTC)
8016     InChain = TCChain;
8017 
8018   TargetLowering::CallLoweringInfo CLI(DAG);
8019   CLI.setDebugLoc(dl)
8020       .setChain(InChain)
8021       .setCallee(CallingConv::ARM_AAPCS_VFP, LCRTy, Callee, std::move(Args))
8022       .setTailCall(IsTC);
8023   std::pair<SDValue, SDValue> CI = TLI.LowerCallTo(CLI);
8024 
8025   // Return the chain (the DAG root) if it is a tail call
8026   return !CI.second.getNode() ? DAG.getRoot() : CI.first;
8027 }
8028 
8029 SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
8030   LLVM_DEBUG(dbgs() << "Lowering node: "; Op.dump());
8031   switch (Op.getOpcode()) {
8032   default: llvm_unreachable("Don't know how to custom lower this!");
8033   case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
8034   case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8035   case ISD::BlockAddress:  return LowerBlockAddress(Op, DAG);
8036   case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
8037   case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
8038   case ISD::SELECT:        return LowerSELECT(Op, DAG);
8039   case ISD::SELECT_CC:     return LowerSELECT_CC(Op, DAG);
8040   case ISD::BRCOND:        return LowerBRCOND(Op, DAG);
8041   case ISD::BR_CC:         return LowerBR_CC(Op, DAG);
8042   case ISD::BR_JT:         return LowerBR_JT(Op, DAG);
8043   case ISD::VASTART:       return LowerVASTART(Op, DAG);
8044   case ISD::ATOMIC_FENCE:  return LowerATOMIC_FENCE(Op, DAG, Subtarget);
8045   case ISD::PREFETCH:      return LowerPREFETCH(Op, DAG, Subtarget);
8046   case ISD::SINT_TO_FP:
8047   case ISD::UINT_TO_FP:    return LowerINT_TO_FP(Op, DAG);
8048   case ISD::FP_TO_SINT:
8049   case ISD::FP_TO_UINT:    return LowerFP_TO_INT(Op, DAG);
8050   case ISD::FCOPYSIGN:     return LowerFCOPYSIGN(Op, DAG);
8051   case ISD::RETURNADDR:    return LowerRETURNADDR(Op, DAG);
8052   case ISD::FRAMEADDR:     return LowerFRAMEADDR(Op, DAG);
8053   case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
8054   case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
8055   case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
8056   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
8057                                                                Subtarget);
8058   case ISD::BITCAST:       return ExpandBITCAST(Op.getNode(), DAG, Subtarget);
8059   case ISD::SHL:
8060   case ISD::SRL:
8061   case ISD::SRA:           return LowerShift(Op.getNode(), DAG, Subtarget);
8062   case ISD::SREM:          return LowerREM(Op.getNode(), DAG);
8063   case ISD::UREM:          return LowerREM(Op.getNode(), DAG);
8064   case ISD::SHL_PARTS:     return LowerShiftLeftParts(Op, DAG);
8065   case ISD::SRL_PARTS:
8066   case ISD::SRA_PARTS:     return LowerShiftRightParts(Op, DAG);
8067   case ISD::CTTZ:
8068   case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
8069   case ISD::CTPOP:         return LowerCTPOP(Op.getNode(), DAG, Subtarget);
8070   case ISD::SETCC:         return LowerVSETCC(Op, DAG);
8071   case ISD::SETCCCARRY:    return LowerSETCCCARRY(Op, DAG);
8072   case ISD::ConstantFP:    return LowerConstantFP(Op, DAG, Subtarget);
8073   case ISD::BUILD_VECTOR:  return LowerBUILD_VECTOR(Op, DAG, Subtarget);
8074   case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8075   case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
8076   case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8077   case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
8078   case ISD::FLT_ROUNDS_:   return LowerFLT_ROUNDS_(Op, DAG);
8079   case ISD::MUL:           return LowerMUL(Op, DAG);
8080   case ISD::SDIV:
8081     if (Subtarget->isTargetWindows() && !Op.getValueType().isVector())
8082       return LowerDIV_Windows(Op, DAG, /* Signed */ true);
8083     return LowerSDIV(Op, DAG);
8084   case ISD::UDIV:
8085     if (Subtarget->isTargetWindows() && !Op.getValueType().isVector())
8086       return LowerDIV_Windows(Op, DAG, /* Signed */ false);
8087     return LowerUDIV(Op, DAG);
8088   case ISD::ADDCARRY:
8089   case ISD::SUBCARRY:      return LowerADDSUBCARRY(Op, DAG);
8090   case ISD::SADDO:
8091   case ISD::SSUBO:
8092     return LowerSignedALUO(Op, DAG);
8093   case ISD::UADDO:
8094   case ISD::USUBO:
8095     return LowerUnsignedALUO(Op, DAG);
8096   case ISD::ATOMIC_LOAD:
8097   case ISD::ATOMIC_STORE:  return LowerAtomicLoadStore(Op, DAG);
8098   case ISD::FSINCOS:       return LowerFSINCOS(Op, DAG);
8099   case ISD::SDIVREM:
8100   case ISD::UDIVREM:       return LowerDivRem(Op, DAG);
8101   case ISD::DYNAMIC_STACKALLOC:
8102     if (Subtarget->isTargetWindows())
8103       return LowerDYNAMIC_STACKALLOC(Op, DAG);
8104     llvm_unreachable("Don't know how to custom lower this!");
8105   case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
8106   case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
8107   case ISD::FPOWI: return LowerFPOWI(Op, *Subtarget, DAG);
8108   case ARMISD::WIN__DBZCHK: return SDValue();
8109   }
8110 }
8111 
8112 static void ReplaceLongIntrinsic(SDNode *N, SmallVectorImpl<SDValue> &Results,
8113                                  SelectionDAG &DAG) {
8114   unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
8115   unsigned Opc = 0;
8116   if (IntNo == Intrinsic::arm_smlald)
8117     Opc = ARMISD::SMLALD;
8118   else if (IntNo == Intrinsic::arm_smlaldx)
8119     Opc = ARMISD::SMLALDX;
8120   else if (IntNo == Intrinsic::arm_smlsld)
8121     Opc = ARMISD::SMLSLD;
8122   else if (IntNo == Intrinsic::arm_smlsldx)
8123     Opc = ARMISD::SMLSLDX;
8124   else
8125     return;
8126 
8127   SDLoc dl(N);
8128   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
8129                            N->getOperand(3),
8130                            DAG.getConstant(0, dl, MVT::i32));
8131   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
8132                            N->getOperand(3),
8133                            DAG.getConstant(1, dl, MVT::i32));
8134 
8135   SDValue LongMul = DAG.getNode(Opc, dl,
8136                                 DAG.getVTList(MVT::i32, MVT::i32),
8137                                 N->getOperand(1), N->getOperand(2),
8138                                 Lo, Hi);
8139   Results.push_back(LongMul.getValue(0));
8140   Results.push_back(LongMul.getValue(1));
8141 }
8142 
8143 /// ReplaceNodeResults - Replace the results of node with an illegal result
8144 /// type with new values built out of custom code.
8145 void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
8146                                            SmallVectorImpl<SDValue> &Results,
8147                                            SelectionDAG &DAG) const {
8148   SDValue Res;
8149   switch (N->getOpcode()) {
8150   default:
8151     llvm_unreachable("Don't know how to custom expand this!");
8152   case ISD::READ_REGISTER:
8153     ExpandREAD_REGISTER(N, Results, DAG);
8154     break;
8155   case ISD::BITCAST:
8156     Res = ExpandBITCAST(N, DAG, Subtarget);
8157     break;
8158   case ISD::SRL:
8159   case ISD::SRA:
8160     Res = Expand64BitShift(N, DAG, Subtarget);
8161     break;
8162   case ISD::SREM:
8163   case ISD::UREM:
8164     Res = LowerREM(N, DAG);
8165     break;
8166   case ISD::SDIVREM:
8167   case ISD::UDIVREM:
8168     Res = LowerDivRem(SDValue(N, 0), DAG);
8169     assert(Res.getNumOperands() == 2 && "DivRem needs two values");
8170     Results.push_back(Res.getValue(0));
8171     Results.push_back(Res.getValue(1));
8172     return;
8173   case ISD::READCYCLECOUNTER:
8174     ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
8175     return;
8176   case ISD::UDIV:
8177   case ISD::SDIV:
8178     assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
8179     return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
8180                              Results);
8181   case ISD::ATOMIC_CMP_SWAP:
8182     ReplaceCMP_SWAP_64Results(N, Results, DAG);
8183     return;
8184   case ISD::INTRINSIC_WO_CHAIN:
8185     return ReplaceLongIntrinsic(N, Results, DAG);
8186   case ISD::ABS:
8187      lowerABS(N, Results, DAG);
8188      return ;
8189 
8190   }
8191   if (Res.getNode())
8192     Results.push_back(Res);
8193 }
8194 
8195 //===----------------------------------------------------------------------===//
8196 //                           ARM Scheduler Hooks
8197 //===----------------------------------------------------------------------===//
8198 
8199 /// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
8200 /// registers the function context.
8201 void ARMTargetLowering::SetupEntryBlockForSjLj(MachineInstr &MI,
8202                                                MachineBasicBlock *MBB,
8203                                                MachineBasicBlock *DispatchBB,
8204                                                int FI) const {
8205   assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
8206          "ROPI/RWPI not currently supported with SjLj");
8207   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
8208   DebugLoc dl = MI.getDebugLoc();
8209   MachineFunction *MF = MBB->getParent();
8210   MachineRegisterInfo *MRI = &MF->getRegInfo();
8211   MachineConstantPool *MCP = MF->getConstantPool();
8212   ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
8213   const Function &F = MF->getFunction();
8214 
8215   bool isThumb = Subtarget->isThumb();
8216   bool isThumb2 = Subtarget->isThumb2();
8217 
8218   unsigned PCLabelId = AFI->createPICLabelUId();
8219   unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
8220   ARMConstantPoolValue *CPV =
8221     ARMConstantPoolMBB::Create(F.getContext(), DispatchBB, PCLabelId, PCAdj);
8222   unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
8223 
8224   const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
8225                                            : &ARM::GPRRegClass;
8226 
8227   // Grab constant pool and fixed stack memory operands.
8228   MachineMemOperand *CPMMO =
8229       MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
8230                                MachineMemOperand::MOLoad, 4, 4);
8231 
8232   MachineMemOperand *FIMMOSt =
8233       MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
8234                                MachineMemOperand::MOStore, 4, 4);
8235 
8236   // Load the address of the dispatch MBB into the jump buffer.
8237   if (isThumb2) {
8238     // Incoming value: jbuf
8239     //   ldr.n  r5, LCPI1_1
8240     //   orr    r5, r5, #1
8241     //   add    r5, pc
8242     //   str    r5, [$jbuf, #+4] ; &jbuf[1]
8243     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
8244     BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
8245         .addConstantPoolIndex(CPI)
8246         .addMemOperand(CPMMO)
8247         .add(predOps(ARMCC::AL));
8248     // Set the low bit because of thumb mode.
8249     unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
8250     BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
8251         .addReg(NewVReg1, RegState::Kill)
8252         .addImm(0x01)
8253         .add(predOps(ARMCC::AL))
8254         .add(condCodeOp());
8255     unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
8256     BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
8257       .addReg(NewVReg2, RegState::Kill)
8258       .addImm(PCLabelId);
8259     BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
8260         .addReg(NewVReg3, RegState::Kill)
8261         .addFrameIndex(FI)
8262         .addImm(36) // &jbuf[1] :: pc
8263         .addMemOperand(FIMMOSt)
8264         .add(predOps(ARMCC::AL));
8265   } else if (isThumb) {
8266     // Incoming value: jbuf
8267     //   ldr.n  r1, LCPI1_4
8268     //   add    r1, pc
8269     //   mov    r2, #1
8270     //   orrs   r1, r2
8271     //   add    r2, $jbuf, #+4 ; &jbuf[1]
8272     //   str    r1, [r2]
8273     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
8274     BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
8275         .addConstantPoolIndex(CPI)
8276         .addMemOperand(CPMMO)
8277         .add(predOps(ARMCC::AL));
8278     unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
8279     BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
8280       .addReg(NewVReg1, RegState::Kill)
8281       .addImm(PCLabelId);
8282     // Set the low bit because of thumb mode.
8283     unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
8284     BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
8285         .addReg(ARM::CPSR, RegState::Define)
8286         .addImm(1)
8287         .add(predOps(ARMCC::AL));
8288     unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
8289     BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
8290         .addReg(ARM::CPSR, RegState::Define)
8291         .addReg(NewVReg2, RegState::Kill)
8292         .addReg(NewVReg3, RegState::Kill)
8293         .add(predOps(ARMCC::AL));
8294     unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
8295     BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
8296             .addFrameIndex(FI)
8297             .addImm(36); // &jbuf[1] :: pc
8298     BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
8299         .addReg(NewVReg4, RegState::Kill)
8300         .addReg(NewVReg5, RegState::Kill)
8301         .addImm(0)
8302         .addMemOperand(FIMMOSt)
8303         .add(predOps(ARMCC::AL));
8304   } else {
8305     // Incoming value: jbuf
8306     //   ldr  r1, LCPI1_1
8307     //   add  r1, pc, r1
8308     //   str  r1, [$jbuf, #+4] ; &jbuf[1]
8309     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
8310     BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
8311         .addConstantPoolIndex(CPI)
8312         .addImm(0)
8313         .addMemOperand(CPMMO)
8314         .add(predOps(ARMCC::AL));
8315     unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
8316     BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
8317         .addReg(NewVReg1, RegState::Kill)
8318         .addImm(PCLabelId)
8319         .add(predOps(ARMCC::AL));
8320     BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
8321         .addReg(NewVReg2, RegState::Kill)
8322         .addFrameIndex(FI)
8323         .addImm(36) // &jbuf[1] :: pc
8324         .addMemOperand(FIMMOSt)
8325         .add(predOps(ARMCC::AL));
8326   }
8327 }
8328 
8329 void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr &MI,
8330                                               MachineBasicBlock *MBB) const {
8331   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
8332   DebugLoc dl = MI.getDebugLoc();
8333   MachineFunction *MF = MBB->getParent();
8334   MachineRegisterInfo *MRI = &MF->getRegInfo();
8335   MachineFrameInfo &MFI = MF->getFrameInfo();
8336   int FI = MFI.getFunctionContextIndex();
8337 
8338   const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
8339                                                         : &ARM::GPRnopcRegClass;
8340 
8341   // Get a mapping of the call site numbers to all of the landing pads they're
8342   // associated with.
8343   DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2>> CallSiteNumToLPad;
8344   unsigned MaxCSNum = 0;
8345   for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
8346        ++BB) {
8347     if (!BB->isEHPad()) continue;
8348 
8349     // FIXME: We should assert that the EH_LABEL is the first MI in the landing
8350     // pad.
8351     for (MachineBasicBlock::iterator
8352            II = BB->begin(), IE = BB->end(); II != IE; ++II) {
8353       if (!II->isEHLabel()) continue;
8354 
8355       MCSymbol *Sym = II->getOperand(0).getMCSymbol();
8356       if (!MF->hasCallSiteLandingPad(Sym)) continue;
8357 
8358       SmallVectorImpl<unsigned> &CallSiteIdxs = MF->getCallSiteLandingPad(Sym);
8359       for (SmallVectorImpl<unsigned>::iterator
8360              CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
8361            CSI != CSE; ++CSI) {
8362         CallSiteNumToLPad[*CSI].push_back(&*BB);
8363         MaxCSNum = std::max(MaxCSNum, *CSI);
8364       }
8365       break;
8366     }
8367   }
8368 
8369   // Get an ordered list of the machine basic blocks for the jump table.
8370   std::vector<MachineBasicBlock*> LPadList;
8371   SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs;
8372   LPadList.reserve(CallSiteNumToLPad.size());
8373   for (unsigned I = 1; I <= MaxCSNum; ++I) {
8374     SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
8375     for (SmallVectorImpl<MachineBasicBlock*>::iterator
8376            II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
8377       LPadList.push_back(*II);
8378       InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
8379     }
8380   }
8381 
8382   assert(!LPadList.empty() &&
8383          "No landing pad destinations for the dispatch jump table!");
8384 
8385   // Create the jump table and associated information.
8386   MachineJumpTableInfo *JTI =
8387     MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
8388   unsigned MJTI = JTI->createJumpTableIndex(LPadList);
8389 
8390   // Create the MBBs for the dispatch code.
8391 
8392   // Shove the dispatch's address into the return slot in the function context.
8393   MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
8394   DispatchBB->setIsEHPad();
8395 
8396   MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
8397   unsigned trap_opcode;
8398   if (Subtarget->isThumb())
8399     trap_opcode = ARM::tTRAP;
8400   else
8401     trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
8402 
8403   BuildMI(TrapBB, dl, TII->get(trap_opcode));
8404   DispatchBB->addSuccessor(TrapBB);
8405 
8406   MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
8407   DispatchBB->addSuccessor(DispContBB);
8408 
8409   // Insert and MBBs.
8410   MF->insert(MF->end(), DispatchBB);
8411   MF->insert(MF->end(), DispContBB);
8412   MF->insert(MF->end(), TrapBB);
8413 
8414   // Insert code into the entry block that creates and registers the function
8415   // context.
8416   SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
8417 
8418   MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
8419       MachinePointerInfo::getFixedStack(*MF, FI),
8420       MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
8421 
8422   MachineInstrBuilder MIB;
8423   MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
8424 
8425   const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
8426   const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
8427 
8428   // Add a register mask with no preserved registers.  This results in all
8429   // registers being marked as clobbered. This can't work if the dispatch block
8430   // is in a Thumb1 function and is linked with ARM code which uses the FP
8431   // registers, as there is no way to preserve the FP registers in Thumb1 mode.
8432   MIB.addRegMask(RI.getSjLjDispatchPreservedMask(*MF));
8433 
8434   bool IsPositionIndependent = isPositionIndependent();
8435   unsigned NumLPads = LPadList.size();
8436   if (Subtarget->isThumb2()) {
8437     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
8438     BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
8439         .addFrameIndex(FI)
8440         .addImm(4)
8441         .addMemOperand(FIMMOLd)
8442         .add(predOps(ARMCC::AL));
8443 
8444     if (NumLPads < 256) {
8445       BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
8446           .addReg(NewVReg1)
8447           .addImm(LPadList.size())
8448           .add(predOps(ARMCC::AL));
8449     } else {
8450       unsigned VReg1 = MRI->createVirtualRegister(TRC);
8451       BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
8452           .addImm(NumLPads & 0xFFFF)
8453           .add(predOps(ARMCC::AL));
8454 
8455       unsigned VReg2 = VReg1;
8456       if ((NumLPads & 0xFFFF0000) != 0) {
8457         VReg2 = MRI->createVirtualRegister(TRC);
8458         BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
8459             .addReg(VReg1)
8460             .addImm(NumLPads >> 16)
8461             .add(predOps(ARMCC::AL));
8462       }
8463 
8464       BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
8465           .addReg(NewVReg1)
8466           .addReg(VReg2)
8467           .add(predOps(ARMCC::AL));
8468     }
8469 
8470     BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
8471       .addMBB(TrapBB)
8472       .addImm(ARMCC::HI)
8473       .addReg(ARM::CPSR);
8474 
8475     unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
8476     BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT), NewVReg3)
8477         .addJumpTableIndex(MJTI)
8478         .add(predOps(ARMCC::AL));
8479 
8480     unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
8481     BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
8482         .addReg(NewVReg3, RegState::Kill)
8483         .addReg(NewVReg1)
8484         .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))
8485         .add(predOps(ARMCC::AL))
8486         .add(condCodeOp());
8487 
8488     BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
8489       .addReg(NewVReg4, RegState::Kill)
8490       .addReg(NewVReg1)
8491       .addJumpTableIndex(MJTI);
8492   } else if (Subtarget->isThumb()) {
8493     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
8494     BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
8495         .addFrameIndex(FI)
8496         .addImm(1)
8497         .addMemOperand(FIMMOLd)
8498         .add(predOps(ARMCC::AL));
8499 
8500     if (NumLPads < 256) {
8501       BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
8502           .addReg(NewVReg1)
8503           .addImm(NumLPads)
8504           .add(predOps(ARMCC::AL));
8505     } else {
8506       MachineConstantPool *ConstantPool = MF->getConstantPool();
8507       Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext());
8508       const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
8509 
8510       // MachineConstantPool wants an explicit alignment.
8511       unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
8512       if (Align == 0)
8513         Align = MF->getDataLayout().getTypeAllocSize(C->getType());
8514       unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
8515 
8516       unsigned VReg1 = MRI->createVirtualRegister(TRC);
8517       BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
8518           .addReg(VReg1, RegState::Define)
8519           .addConstantPoolIndex(Idx)
8520           .add(predOps(ARMCC::AL));
8521       BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
8522           .addReg(NewVReg1)
8523           .addReg(VReg1)
8524           .add(predOps(ARMCC::AL));
8525     }
8526 
8527     BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
8528       .addMBB(TrapBB)
8529       .addImm(ARMCC::HI)
8530       .addReg(ARM::CPSR);
8531 
8532     unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
8533     BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
8534         .addReg(ARM::CPSR, RegState::Define)
8535         .addReg(NewVReg1)
8536         .addImm(2)
8537         .add(predOps(ARMCC::AL));
8538 
8539     unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
8540     BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
8541         .addJumpTableIndex(MJTI)
8542         .add(predOps(ARMCC::AL));
8543 
8544     unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
8545     BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
8546         .addReg(ARM::CPSR, RegState::Define)
8547         .addReg(NewVReg2, RegState::Kill)
8548         .addReg(NewVReg3)
8549         .add(predOps(ARMCC::AL));
8550 
8551     MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
8552         MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
8553 
8554     unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
8555     BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
8556         .addReg(NewVReg4, RegState::Kill)
8557         .addImm(0)
8558         .addMemOperand(JTMMOLd)
8559         .add(predOps(ARMCC::AL));
8560 
8561     unsigned NewVReg6 = NewVReg5;
8562     if (IsPositionIndependent) {
8563       NewVReg6 = MRI->createVirtualRegister(TRC);
8564       BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
8565           .addReg(ARM::CPSR, RegState::Define)
8566           .addReg(NewVReg5, RegState::Kill)
8567           .addReg(NewVReg3)
8568           .add(predOps(ARMCC::AL));
8569     }
8570 
8571     BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
8572       .addReg(NewVReg6, RegState::Kill)
8573       .addJumpTableIndex(MJTI);
8574   } else {
8575     unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
8576     BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
8577         .addFrameIndex(FI)
8578         .addImm(4)
8579         .addMemOperand(FIMMOLd)
8580         .add(predOps(ARMCC::AL));
8581 
8582     if (NumLPads < 256) {
8583       BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
8584           .addReg(NewVReg1)
8585           .addImm(NumLPads)
8586           .add(predOps(ARMCC::AL));
8587     } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
8588       unsigned VReg1 = MRI->createVirtualRegister(TRC);
8589       BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
8590           .addImm(NumLPads & 0xFFFF)
8591           .add(predOps(ARMCC::AL));
8592 
8593       unsigned VReg2 = VReg1;
8594       if ((NumLPads & 0xFFFF0000) != 0) {
8595         VReg2 = MRI->createVirtualRegister(TRC);
8596         BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
8597             .addReg(VReg1)
8598             .addImm(NumLPads >> 16)
8599             .add(predOps(ARMCC::AL));
8600       }
8601 
8602       BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
8603           .addReg(NewVReg1)
8604           .addReg(VReg2)
8605           .add(predOps(ARMCC::AL));
8606     } else {
8607       MachineConstantPool *ConstantPool = MF->getConstantPool();
8608       Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext());
8609       const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
8610 
8611       // MachineConstantPool wants an explicit alignment.
8612       unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
8613       if (Align == 0)
8614         Align = MF->getDataLayout().getTypeAllocSize(C->getType());
8615       unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
8616 
8617       unsigned VReg1 = MRI->createVirtualRegister(TRC);
8618       BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
8619           .addReg(VReg1, RegState::Define)
8620           .addConstantPoolIndex(Idx)
8621           .addImm(0)
8622           .add(predOps(ARMCC::AL));
8623       BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
8624           .addReg(NewVReg1)
8625           .addReg(VReg1, RegState::Kill)
8626           .add(predOps(ARMCC::AL));
8627     }
8628 
8629     BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
8630       .addMBB(TrapBB)
8631       .addImm(ARMCC::HI)
8632       .addReg(ARM::CPSR);
8633 
8634     unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
8635     BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
8636         .addReg(NewVReg1)
8637         .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))
8638         .add(predOps(ARMCC::AL))
8639         .add(condCodeOp());
8640     unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
8641     BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
8642         .addJumpTableIndex(MJTI)
8643         .add(predOps(ARMCC::AL));
8644 
8645     MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
8646         MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
8647     unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
8648     BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
8649         .addReg(NewVReg3, RegState::Kill)
8650         .addReg(NewVReg4)
8651         .addImm(0)
8652         .addMemOperand(JTMMOLd)
8653         .add(predOps(ARMCC::AL));
8654 
8655     if (IsPositionIndependent) {
8656       BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
8657         .addReg(NewVReg5, RegState::Kill)
8658         .addReg(NewVReg4)
8659         .addJumpTableIndex(MJTI);
8660     } else {
8661       BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
8662         .addReg(NewVReg5, RegState::Kill)
8663         .addJumpTableIndex(MJTI);
8664     }
8665   }
8666 
8667   // Add the jump table entries as successors to the MBB.
8668   SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
8669   for (std::vector<MachineBasicBlock*>::iterator
8670          I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
8671     MachineBasicBlock *CurMBB = *I;
8672     if (SeenMBBs.insert(CurMBB).second)
8673       DispContBB->addSuccessor(CurMBB);
8674   }
8675 
8676   // N.B. the order the invoke BBs are processed in doesn't matter here.
8677   const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
8678   SmallVector<MachineBasicBlock*, 64> MBBLPads;
8679   for (MachineBasicBlock *BB : InvokeBBs) {
8680 
8681     // Remove the landing pad successor from the invoke block and replace it
8682     // with the new dispatch block.
8683     SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
8684                                                   BB->succ_end());
8685     while (!Successors.empty()) {
8686       MachineBasicBlock *SMBB = Successors.pop_back_val();
8687       if (SMBB->isEHPad()) {
8688         BB->removeSuccessor(SMBB);
8689         MBBLPads.push_back(SMBB);
8690       }
8691     }
8692 
8693     BB->addSuccessor(DispatchBB, BranchProbability::getZero());
8694     BB->normalizeSuccProbs();
8695 
8696     // Find the invoke call and mark all of the callee-saved registers as
8697     // 'implicit defined' so that they're spilled. This prevents code from
8698     // moving instructions to before the EH block, where they will never be
8699     // executed.
8700     for (MachineBasicBlock::reverse_iterator
8701            II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
8702       if (!II->isCall()) continue;
8703 
8704       DenseMap<unsigned, bool> DefRegs;
8705       for (MachineInstr::mop_iterator
8706              OI = II->operands_begin(), OE = II->operands_end();
8707            OI != OE; ++OI) {
8708         if (!OI->isReg()) continue;
8709         DefRegs[OI->getReg()] = true;
8710       }
8711 
8712       MachineInstrBuilder MIB(*MF, &*II);
8713 
8714       for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
8715         unsigned Reg = SavedRegs[i];
8716         if (Subtarget->isThumb2() &&
8717             !ARM::tGPRRegClass.contains(Reg) &&
8718             !ARM::hGPRRegClass.contains(Reg))
8719           continue;
8720         if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
8721           continue;
8722         if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
8723           continue;
8724         if (!DefRegs[Reg])
8725           MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
8726       }
8727 
8728       break;
8729     }
8730   }
8731 
8732   // Mark all former landing pads as non-landing pads. The dispatch is the only
8733   // landing pad now.
8734   for (SmallVectorImpl<MachineBasicBlock*>::iterator
8735          I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
8736     (*I)->setIsEHPad(false);
8737 
8738   // The instruction is gone now.
8739   MI.eraseFromParent();
8740 }
8741 
8742 static
8743 MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
8744   for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
8745        E = MBB->succ_end(); I != E; ++I)
8746     if (*I != Succ)
8747       return *I;
8748   llvm_unreachable("Expecting a BB with two successors!");
8749 }
8750 
8751 /// Return the load opcode for a given load size. If load size >= 8,
8752 /// neon opcode will be returned.
8753 static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
8754   if (LdSize >= 8)
8755     return LdSize == 16 ? ARM::VLD1q32wb_fixed
8756                         : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
8757   if (IsThumb1)
8758     return LdSize == 4 ? ARM::tLDRi
8759                        : LdSize == 2 ? ARM::tLDRHi
8760                                      : LdSize == 1 ? ARM::tLDRBi : 0;
8761   if (IsThumb2)
8762     return LdSize == 4 ? ARM::t2LDR_POST
8763                        : LdSize == 2 ? ARM::t2LDRH_POST
8764                                      : LdSize == 1 ? ARM::t2LDRB_POST : 0;
8765   return LdSize == 4 ? ARM::LDR_POST_IMM
8766                      : LdSize == 2 ? ARM::LDRH_POST
8767                                    : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
8768 }
8769 
8770 /// Return the store opcode for a given store size. If store size >= 8,
8771 /// neon opcode will be returned.
8772 static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
8773   if (StSize >= 8)
8774     return StSize == 16 ? ARM::VST1q32wb_fixed
8775                         : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
8776   if (IsThumb1)
8777     return StSize == 4 ? ARM::tSTRi
8778                        : StSize == 2 ? ARM::tSTRHi
8779                                      : StSize == 1 ? ARM::tSTRBi : 0;
8780   if (IsThumb2)
8781     return StSize == 4 ? ARM::t2STR_POST
8782                        : StSize == 2 ? ARM::t2STRH_POST
8783                                      : StSize == 1 ? ARM::t2STRB_POST : 0;
8784   return StSize == 4 ? ARM::STR_POST_IMM
8785                      : StSize == 2 ? ARM::STRH_POST
8786                                    : StSize == 1 ? ARM::STRB_POST_IMM : 0;
8787 }
8788 
8789 /// Emit a post-increment load operation with given size. The instructions
8790 /// will be added to BB at Pos.
8791 static void emitPostLd(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
8792                        const TargetInstrInfo *TII, const DebugLoc &dl,
8793                        unsigned LdSize, unsigned Data, unsigned AddrIn,
8794                        unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
8795   unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
8796   assert(LdOpc != 0 && "Should have a load opcode");
8797   if (LdSize >= 8) {
8798     BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8799         .addReg(AddrOut, RegState::Define)
8800         .addReg(AddrIn)
8801         .addImm(0)
8802         .add(predOps(ARMCC::AL));
8803   } else if (IsThumb1) {
8804     // load + update AddrIn
8805     BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8806         .addReg(AddrIn)
8807         .addImm(0)
8808         .add(predOps(ARMCC::AL));
8809     BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut)
8810         .add(t1CondCodeOp())
8811         .addReg(AddrIn)
8812         .addImm(LdSize)
8813         .add(predOps(ARMCC::AL));
8814   } else if (IsThumb2) {
8815     BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8816         .addReg(AddrOut, RegState::Define)
8817         .addReg(AddrIn)
8818         .addImm(LdSize)
8819         .add(predOps(ARMCC::AL));
8820   } else { // arm
8821     BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8822         .addReg(AddrOut, RegState::Define)
8823         .addReg(AddrIn)
8824         .addReg(0)
8825         .addImm(LdSize)
8826         .add(predOps(ARMCC::AL));
8827   }
8828 }
8829 
8830 /// Emit a post-increment store operation with given size. The instructions
8831 /// will be added to BB at Pos.
8832 static void emitPostSt(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
8833                        const TargetInstrInfo *TII, const DebugLoc &dl,
8834                        unsigned StSize, unsigned Data, unsigned AddrIn,
8835                        unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
8836   unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
8837   assert(StOpc != 0 && "Should have a store opcode");
8838   if (StSize >= 8) {
8839     BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
8840         .addReg(AddrIn)
8841         .addImm(0)
8842         .addReg(Data)
8843         .add(predOps(ARMCC::AL));
8844   } else if (IsThumb1) {
8845     // store + update AddrIn
8846     BuildMI(*BB, Pos, dl, TII->get(StOpc))
8847         .addReg(Data)
8848         .addReg(AddrIn)
8849         .addImm(0)
8850         .add(predOps(ARMCC::AL));
8851     BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut)
8852         .add(t1CondCodeOp())
8853         .addReg(AddrIn)
8854         .addImm(StSize)
8855         .add(predOps(ARMCC::AL));
8856   } else if (IsThumb2) {
8857     BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
8858         .addReg(Data)
8859         .addReg(AddrIn)
8860         .addImm(StSize)
8861         .add(predOps(ARMCC::AL));
8862   } else { // arm
8863     BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
8864         .addReg(Data)
8865         .addReg(AddrIn)
8866         .addReg(0)
8867         .addImm(StSize)
8868         .add(predOps(ARMCC::AL));
8869   }
8870 }
8871 
8872 MachineBasicBlock *
8873 ARMTargetLowering::EmitStructByval(MachineInstr &MI,
8874                                    MachineBasicBlock *BB) const {
8875   // This pseudo instruction has 3 operands: dst, src, size
8876   // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
8877   // Otherwise, we will generate unrolled scalar copies.
8878   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
8879   const BasicBlock *LLVM_BB = BB->getBasicBlock();
8880   MachineFunction::iterator It = ++BB->getIterator();
8881 
8882   unsigned dest = MI.getOperand(0).getReg();
8883   unsigned src = MI.getOperand(1).getReg();
8884   unsigned SizeVal = MI.getOperand(2).getImm();
8885   unsigned Align = MI.getOperand(3).getImm();
8886   DebugLoc dl = MI.getDebugLoc();
8887 
8888   MachineFunction *MF = BB->getParent();
8889   MachineRegisterInfo &MRI = MF->getRegInfo();
8890   unsigned UnitSize = 0;
8891   const TargetRegisterClass *TRC = nullptr;
8892   const TargetRegisterClass *VecTRC = nullptr;
8893 
8894   bool IsThumb1 = Subtarget->isThumb1Only();
8895   bool IsThumb2 = Subtarget->isThumb2();
8896   bool IsThumb = Subtarget->isThumb();
8897 
8898   if (Align & 1) {
8899     UnitSize = 1;
8900   } else if (Align & 2) {
8901     UnitSize = 2;
8902   } else {
8903     // Check whether we can use NEON instructions.
8904     if (!MF->getFunction().hasFnAttribute(Attribute::NoImplicitFloat) &&
8905         Subtarget->hasNEON()) {
8906       if ((Align % 16 == 0) && SizeVal >= 16)
8907         UnitSize = 16;
8908       else if ((Align % 8 == 0) && SizeVal >= 8)
8909         UnitSize = 8;
8910     }
8911     // Can't use NEON instructions.
8912     if (UnitSize == 0)
8913       UnitSize = 4;
8914   }
8915 
8916   // Select the correct opcode and register class for unit size load/store
8917   bool IsNeon = UnitSize >= 8;
8918   TRC = IsThumb ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
8919   if (IsNeon)
8920     VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
8921                             : UnitSize == 8 ? &ARM::DPRRegClass
8922                                             : nullptr;
8923 
8924   unsigned BytesLeft = SizeVal % UnitSize;
8925   unsigned LoopSize = SizeVal - BytesLeft;
8926 
8927   if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
8928     // Use LDR and STR to copy.
8929     // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
8930     // [destOut] = STR_POST(scratch, destIn, UnitSize)
8931     unsigned srcIn = src;
8932     unsigned destIn = dest;
8933     for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
8934       unsigned srcOut = MRI.createVirtualRegister(TRC);
8935       unsigned destOut = MRI.createVirtualRegister(TRC);
8936       unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
8937       emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
8938                  IsThumb1, IsThumb2);
8939       emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
8940                  IsThumb1, IsThumb2);
8941       srcIn = srcOut;
8942       destIn = destOut;
8943     }
8944 
8945     // Handle the leftover bytes with LDRB and STRB.
8946     // [scratch, srcOut] = LDRB_POST(srcIn, 1)
8947     // [destOut] = STRB_POST(scratch, destIn, 1)
8948     for (unsigned i = 0; i < BytesLeft; i++) {
8949       unsigned srcOut = MRI.createVirtualRegister(TRC);
8950       unsigned destOut = MRI.createVirtualRegister(TRC);
8951       unsigned scratch = MRI.createVirtualRegister(TRC);
8952       emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
8953                  IsThumb1, IsThumb2);
8954       emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
8955                  IsThumb1, IsThumb2);
8956       srcIn = srcOut;
8957       destIn = destOut;
8958     }
8959     MI.eraseFromParent(); // The instruction is gone now.
8960     return BB;
8961   }
8962 
8963   // Expand the pseudo op to a loop.
8964   // thisMBB:
8965   //   ...
8966   //   movw varEnd, # --> with thumb2
8967   //   movt varEnd, #
8968   //   ldrcp varEnd, idx --> without thumb2
8969   //   fallthrough --> loopMBB
8970   // loopMBB:
8971   //   PHI varPhi, varEnd, varLoop
8972   //   PHI srcPhi, src, srcLoop
8973   //   PHI destPhi, dst, destLoop
8974   //   [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
8975   //   [destLoop] = STR_POST(scratch, destPhi, UnitSize)
8976   //   subs varLoop, varPhi, #UnitSize
8977   //   bne loopMBB
8978   //   fallthrough --> exitMBB
8979   // exitMBB:
8980   //   epilogue to handle left-over bytes
8981   //   [scratch, srcOut] = LDRB_POST(srcLoop, 1)
8982   //   [destOut] = STRB_POST(scratch, destLoop, 1)
8983   MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
8984   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
8985   MF->insert(It, loopMBB);
8986   MF->insert(It, exitMBB);
8987 
8988   // Transfer the remainder of BB and its successor edges to exitMBB.
8989   exitMBB->splice(exitMBB->begin(), BB,
8990                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
8991   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
8992 
8993   // Load an immediate to varEnd.
8994   unsigned varEnd = MRI.createVirtualRegister(TRC);
8995   if (Subtarget->useMovt()) {
8996     unsigned Vtmp = varEnd;
8997     if ((LoopSize & 0xFFFF0000) != 0)
8998       Vtmp = MRI.createVirtualRegister(TRC);
8999     BuildMI(BB, dl, TII->get(IsThumb ? ARM::t2MOVi16 : ARM::MOVi16), Vtmp)
9000         .addImm(LoopSize & 0xFFFF)
9001         .add(predOps(ARMCC::AL));
9002 
9003     if ((LoopSize & 0xFFFF0000) != 0)
9004       BuildMI(BB, dl, TII->get(IsThumb ? ARM::t2MOVTi16 : ARM::MOVTi16), varEnd)
9005           .addReg(Vtmp)
9006           .addImm(LoopSize >> 16)
9007           .add(predOps(ARMCC::AL));
9008   } else {
9009     MachineConstantPool *ConstantPool = MF->getConstantPool();
9010     Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext());
9011     const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
9012 
9013     // MachineConstantPool wants an explicit alignment.
9014     unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
9015     if (Align == 0)
9016       Align = MF->getDataLayout().getTypeAllocSize(C->getType());
9017     unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
9018 
9019     if (IsThumb)
9020       BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci))
9021           .addReg(varEnd, RegState::Define)
9022           .addConstantPoolIndex(Idx)
9023           .add(predOps(ARMCC::AL));
9024     else
9025       BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp))
9026           .addReg(varEnd, RegState::Define)
9027           .addConstantPoolIndex(Idx)
9028           .addImm(0)
9029           .add(predOps(ARMCC::AL));
9030   }
9031   BB->addSuccessor(loopMBB);
9032 
9033   // Generate the loop body:
9034   //   varPhi = PHI(varLoop, varEnd)
9035   //   srcPhi = PHI(srcLoop, src)
9036   //   destPhi = PHI(destLoop, dst)
9037   MachineBasicBlock *entryBB = BB;
9038   BB = loopMBB;
9039   unsigned varLoop = MRI.createVirtualRegister(TRC);
9040   unsigned varPhi = MRI.createVirtualRegister(TRC);
9041   unsigned srcLoop = MRI.createVirtualRegister(TRC);
9042   unsigned srcPhi = MRI.createVirtualRegister(TRC);
9043   unsigned destLoop = MRI.createVirtualRegister(TRC);
9044   unsigned destPhi = MRI.createVirtualRegister(TRC);
9045 
9046   BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
9047     .addReg(varLoop).addMBB(loopMBB)
9048     .addReg(varEnd).addMBB(entryBB);
9049   BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
9050     .addReg(srcLoop).addMBB(loopMBB)
9051     .addReg(src).addMBB(entryBB);
9052   BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
9053     .addReg(destLoop).addMBB(loopMBB)
9054     .addReg(dest).addMBB(entryBB);
9055 
9056   //   [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
9057   //   [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
9058   unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
9059   emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
9060              IsThumb1, IsThumb2);
9061   emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
9062              IsThumb1, IsThumb2);
9063 
9064   // Decrement loop variable by UnitSize.
9065   if (IsThumb1) {
9066     BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop)
9067         .add(t1CondCodeOp())
9068         .addReg(varPhi)
9069         .addImm(UnitSize)
9070         .add(predOps(ARMCC::AL));
9071   } else {
9072     MachineInstrBuilder MIB =
9073         BuildMI(*BB, BB->end(), dl,
9074                 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
9075     MIB.addReg(varPhi)
9076         .addImm(UnitSize)
9077         .add(predOps(ARMCC::AL))
9078         .add(condCodeOp());
9079     MIB->getOperand(5).setReg(ARM::CPSR);
9080     MIB->getOperand(5).setIsDef(true);
9081   }
9082   BuildMI(*BB, BB->end(), dl,
9083           TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
9084       .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
9085 
9086   // loopMBB can loop back to loopMBB or fall through to exitMBB.
9087   BB->addSuccessor(loopMBB);
9088   BB->addSuccessor(exitMBB);
9089 
9090   // Add epilogue to handle BytesLeft.
9091   BB = exitMBB;
9092   auto StartOfExit = exitMBB->begin();
9093 
9094   //   [scratch, srcOut] = LDRB_POST(srcLoop, 1)
9095   //   [destOut] = STRB_POST(scratch, destLoop, 1)
9096   unsigned srcIn = srcLoop;
9097   unsigned destIn = destLoop;
9098   for (unsigned i = 0; i < BytesLeft; i++) {
9099     unsigned srcOut = MRI.createVirtualRegister(TRC);
9100     unsigned destOut = MRI.createVirtualRegister(TRC);
9101     unsigned scratch = MRI.createVirtualRegister(TRC);
9102     emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
9103                IsThumb1, IsThumb2);
9104     emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
9105                IsThumb1, IsThumb2);
9106     srcIn = srcOut;
9107     destIn = destOut;
9108   }
9109 
9110   MI.eraseFromParent(); // The instruction is gone now.
9111   return BB;
9112 }
9113 
9114 MachineBasicBlock *
9115 ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
9116                                        MachineBasicBlock *MBB) const {
9117   const TargetMachine &TM = getTargetMachine();
9118   const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
9119   DebugLoc DL = MI.getDebugLoc();
9120 
9121   assert(Subtarget->isTargetWindows() &&
9122          "__chkstk is only supported on Windows");
9123   assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
9124 
9125   // __chkstk takes the number of words to allocate on the stack in R4, and
9126   // returns the stack adjustment in number of bytes in R4.  This will not
9127   // clober any other registers (other than the obvious lr).
9128   //
9129   // Although, technically, IP should be considered a register which may be
9130   // clobbered, the call itself will not touch it.  Windows on ARM is a pure
9131   // thumb-2 environment, so there is no interworking required.  As a result, we
9132   // do not expect a veneer to be emitted by the linker, clobbering IP.
9133   //
9134   // Each module receives its own copy of __chkstk, so no import thunk is
9135   // required, again, ensuring that IP is not clobbered.
9136   //
9137   // Finally, although some linkers may theoretically provide a trampoline for
9138   // out of range calls (which is quite common due to a 32M range limitation of
9139   // branches for Thumb), we can generate the long-call version via
9140   // -mcmodel=large, alleviating the need for the trampoline which may clobber
9141   // IP.
9142 
9143   switch (TM.getCodeModel()) {
9144   case CodeModel::Tiny:
9145     llvm_unreachable("Tiny code model not available on ARM.");
9146   case CodeModel::Small:
9147   case CodeModel::Medium:
9148   case CodeModel::Kernel:
9149     BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
9150         .add(predOps(ARMCC::AL))
9151         .addExternalSymbol("__chkstk")
9152         .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
9153         .addReg(ARM::R4, RegState::Implicit | RegState::Define)
9154         .addReg(ARM::R12,
9155                 RegState::Implicit | RegState::Define | RegState::Dead)
9156         .addReg(ARM::CPSR,
9157                 RegState::Implicit | RegState::Define | RegState::Dead);
9158     break;
9159   case CodeModel::Large: {
9160     MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9161     unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
9162 
9163     BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
9164       .addExternalSymbol("__chkstk");
9165     BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
9166         .add(predOps(ARMCC::AL))
9167         .addReg(Reg, RegState::Kill)
9168         .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
9169         .addReg(ARM::R4, RegState::Implicit | RegState::Define)
9170         .addReg(ARM::R12,
9171                 RegState::Implicit | RegState::Define | RegState::Dead)
9172         .addReg(ARM::CPSR,
9173                 RegState::Implicit | RegState::Define | RegState::Dead);
9174     break;
9175   }
9176   }
9177 
9178   BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr), ARM::SP)
9179       .addReg(ARM::SP, RegState::Kill)
9180       .addReg(ARM::R4, RegState::Kill)
9181       .setMIFlags(MachineInstr::FrameSetup)
9182       .add(predOps(ARMCC::AL))
9183       .add(condCodeOp());
9184 
9185   MI.eraseFromParent();
9186   return MBB;
9187 }
9188 
9189 MachineBasicBlock *
9190 ARMTargetLowering::EmitLowered__dbzchk(MachineInstr &MI,
9191                                        MachineBasicBlock *MBB) const {
9192   DebugLoc DL = MI.getDebugLoc();
9193   MachineFunction *MF = MBB->getParent();
9194   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
9195 
9196   MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
9197   MF->insert(++MBB->getIterator(), ContBB);
9198   ContBB->splice(ContBB->begin(), MBB,
9199                  std::next(MachineBasicBlock::iterator(MI)), MBB->end());
9200   ContBB->transferSuccessorsAndUpdatePHIs(MBB);
9201   MBB->addSuccessor(ContBB);
9202 
9203   MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
9204   BuildMI(TrapBB, DL, TII->get(ARM::t__brkdiv0));
9205   MF->push_back(TrapBB);
9206   MBB->addSuccessor(TrapBB);
9207 
9208   BuildMI(*MBB, MI, DL, TII->get(ARM::tCMPi8))
9209       .addReg(MI.getOperand(0).getReg())
9210       .addImm(0)
9211       .add(predOps(ARMCC::AL));
9212   BuildMI(*MBB, MI, DL, TII->get(ARM::t2Bcc))
9213       .addMBB(TrapBB)
9214       .addImm(ARMCC::EQ)
9215       .addReg(ARM::CPSR);
9216 
9217   MI.eraseFromParent();
9218   return ContBB;
9219 }
9220 
9221 // The CPSR operand of SelectItr might be missing a kill marker
9222 // because there were multiple uses of CPSR, and ISel didn't know
9223 // which to mark. Figure out whether SelectItr should have had a
9224 // kill marker, and set it if it should. Returns the correct kill
9225 // marker value.
9226 static bool checkAndUpdateCPSRKill(MachineBasicBlock::iterator SelectItr,
9227                                    MachineBasicBlock* BB,
9228                                    const TargetRegisterInfo* TRI) {
9229   // Scan forward through BB for a use/def of CPSR.
9230   MachineBasicBlock::iterator miI(std::next(SelectItr));
9231   for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
9232     const MachineInstr& mi = *miI;
9233     if (mi.readsRegister(ARM::CPSR))
9234       return false;
9235     if (mi.definesRegister(ARM::CPSR))
9236       break; // Should have kill-flag - update below.
9237   }
9238 
9239   // If we hit the end of the block, check whether CPSR is live into a
9240   // successor.
9241   if (miI == BB->end()) {
9242     for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
9243                                           sEnd = BB->succ_end();
9244          sItr != sEnd; ++sItr) {
9245       MachineBasicBlock* succ = *sItr;
9246       if (succ->isLiveIn(ARM::CPSR))
9247         return false;
9248     }
9249   }
9250 
9251   // We found a def, or hit the end of the basic block and CPSR wasn't live
9252   // out. SelectMI should have a kill flag on CPSR.
9253   SelectItr->addRegisterKilled(ARM::CPSR, TRI);
9254   return true;
9255 }
9256 
9257 MachineBasicBlock *
9258 ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
9259                                                MachineBasicBlock *BB) const {
9260   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
9261   DebugLoc dl = MI.getDebugLoc();
9262   bool isThumb2 = Subtarget->isThumb2();
9263   switch (MI.getOpcode()) {
9264   default: {
9265     MI.print(errs());
9266     llvm_unreachable("Unexpected instr type to insert");
9267   }
9268 
9269   // Thumb1 post-indexed loads are really just single-register LDMs.
9270   case ARM::tLDR_postidx: {
9271     MachineOperand Def(MI.getOperand(1));
9272     BuildMI(*BB, MI, dl, TII->get(ARM::tLDMIA_UPD))
9273         .add(Def)  // Rn_wb
9274         .add(MI.getOperand(2))  // Rn
9275         .add(MI.getOperand(3))  // PredImm
9276         .add(MI.getOperand(4))  // PredReg
9277         .add(MI.getOperand(0)); // Rt
9278     MI.eraseFromParent();
9279     return BB;
9280   }
9281 
9282   // The Thumb2 pre-indexed stores have the same MI operands, they just
9283   // define them differently in the .td files from the isel patterns, so
9284   // they need pseudos.
9285   case ARM::t2STR_preidx:
9286     MI.setDesc(TII->get(ARM::t2STR_PRE));
9287     return BB;
9288   case ARM::t2STRB_preidx:
9289     MI.setDesc(TII->get(ARM::t2STRB_PRE));
9290     return BB;
9291   case ARM::t2STRH_preidx:
9292     MI.setDesc(TII->get(ARM::t2STRH_PRE));
9293     return BB;
9294 
9295   case ARM::STRi_preidx:
9296   case ARM::STRBi_preidx: {
9297     unsigned NewOpc = MI.getOpcode() == ARM::STRi_preidx ? ARM::STR_PRE_IMM
9298                                                          : ARM::STRB_PRE_IMM;
9299     // Decode the offset.
9300     unsigned Offset = MI.getOperand(4).getImm();
9301     bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
9302     Offset = ARM_AM::getAM2Offset(Offset);
9303     if (isSub)
9304       Offset = -Offset;
9305 
9306     MachineMemOperand *MMO = *MI.memoperands_begin();
9307     BuildMI(*BB, MI, dl, TII->get(NewOpc))
9308         .add(MI.getOperand(0)) // Rn_wb
9309         .add(MI.getOperand(1)) // Rt
9310         .add(MI.getOperand(2)) // Rn
9311         .addImm(Offset)        // offset (skip GPR==zero_reg)
9312         .add(MI.getOperand(5)) // pred
9313         .add(MI.getOperand(6))
9314         .addMemOperand(MMO);
9315     MI.eraseFromParent();
9316     return BB;
9317   }
9318   case ARM::STRr_preidx:
9319   case ARM::STRBr_preidx:
9320   case ARM::STRH_preidx: {
9321     unsigned NewOpc;
9322     switch (MI.getOpcode()) {
9323     default: llvm_unreachable("unexpected opcode!");
9324     case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
9325     case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
9326     case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
9327     }
9328     MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
9329     for (unsigned i = 0; i < MI.getNumOperands(); ++i)
9330       MIB.add(MI.getOperand(i));
9331     MI.eraseFromParent();
9332     return BB;
9333   }
9334 
9335   case ARM::tMOVCCr_pseudo: {
9336     // To "insert" a SELECT_CC instruction, we actually have to insert the
9337     // diamond control-flow pattern.  The incoming instruction knows the
9338     // destination vreg to set, the condition code register to branch on, the
9339     // true/false values to select between, and a branch opcode to use.
9340     const BasicBlock *LLVM_BB = BB->getBasicBlock();
9341     MachineFunction::iterator It = ++BB->getIterator();
9342 
9343     //  thisMBB:
9344     //  ...
9345     //   TrueVal = ...
9346     //   cmpTY ccX, r1, r2
9347     //   bCC copy1MBB
9348     //   fallthrough --> copy0MBB
9349     MachineBasicBlock *thisMBB  = BB;
9350     MachineFunction *F = BB->getParent();
9351     MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9352     MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
9353     F->insert(It, copy0MBB);
9354     F->insert(It, sinkMBB);
9355 
9356     // Check whether CPSR is live past the tMOVCCr_pseudo.
9357     const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
9358     if (!MI.killsRegister(ARM::CPSR) &&
9359         !checkAndUpdateCPSRKill(MI, thisMBB, TRI)) {
9360       copy0MBB->addLiveIn(ARM::CPSR);
9361       sinkMBB->addLiveIn(ARM::CPSR);
9362     }
9363 
9364     // Transfer the remainder of BB and its successor edges to sinkMBB.
9365     sinkMBB->splice(sinkMBB->begin(), BB,
9366                     std::next(MachineBasicBlock::iterator(MI)), BB->end());
9367     sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
9368 
9369     BB->addSuccessor(copy0MBB);
9370     BB->addSuccessor(sinkMBB);
9371 
9372     BuildMI(BB, dl, TII->get(ARM::tBcc))
9373         .addMBB(sinkMBB)
9374         .addImm(MI.getOperand(3).getImm())
9375         .addReg(MI.getOperand(4).getReg());
9376 
9377     //  copy0MBB:
9378     //   %FalseValue = ...
9379     //   # fallthrough to sinkMBB
9380     BB = copy0MBB;
9381 
9382     // Update machine-CFG edges
9383     BB->addSuccessor(sinkMBB);
9384 
9385     //  sinkMBB:
9386     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
9387     //  ...
9388     BB = sinkMBB;
9389     BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), MI.getOperand(0).getReg())
9390         .addReg(MI.getOperand(1).getReg())
9391         .addMBB(copy0MBB)
9392         .addReg(MI.getOperand(2).getReg())
9393         .addMBB(thisMBB);
9394 
9395     MI.eraseFromParent(); // The pseudo instruction is gone now.
9396     return BB;
9397   }
9398 
9399   case ARM::BCCi64:
9400   case ARM::BCCZi64: {
9401     // If there is an unconditional branch to the other successor, remove it.
9402     BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
9403 
9404     // Compare both parts that make up the double comparison separately for
9405     // equality.
9406     bool RHSisZero = MI.getOpcode() == ARM::BCCZi64;
9407 
9408     unsigned LHS1 = MI.getOperand(1).getReg();
9409     unsigned LHS2 = MI.getOperand(2).getReg();
9410     if (RHSisZero) {
9411       BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
9412           .addReg(LHS1)
9413           .addImm(0)
9414           .add(predOps(ARMCC::AL));
9415       BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
9416         .addReg(LHS2).addImm(0)
9417         .addImm(ARMCC::EQ).addReg(ARM::CPSR);
9418     } else {
9419       unsigned RHS1 = MI.getOperand(3).getReg();
9420       unsigned RHS2 = MI.getOperand(4).getReg();
9421       BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
9422           .addReg(LHS1)
9423           .addReg(RHS1)
9424           .add(predOps(ARMCC::AL));
9425       BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
9426         .addReg(LHS2).addReg(RHS2)
9427         .addImm(ARMCC::EQ).addReg(ARM::CPSR);
9428     }
9429 
9430     MachineBasicBlock *destMBB = MI.getOperand(RHSisZero ? 3 : 5).getMBB();
9431     MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
9432     if (MI.getOperand(0).getImm() == ARMCC::NE)
9433       std::swap(destMBB, exitMBB);
9434 
9435     BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
9436       .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
9437     if (isThumb2)
9438       BuildMI(BB, dl, TII->get(ARM::t2B))
9439           .addMBB(exitMBB)
9440           .add(predOps(ARMCC::AL));
9441     else
9442       BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
9443 
9444     MI.eraseFromParent(); // The pseudo instruction is gone now.
9445     return BB;
9446   }
9447 
9448   case ARM::Int_eh_sjlj_setjmp:
9449   case ARM::Int_eh_sjlj_setjmp_nofp:
9450   case ARM::tInt_eh_sjlj_setjmp:
9451   case ARM::t2Int_eh_sjlj_setjmp:
9452   case ARM::t2Int_eh_sjlj_setjmp_nofp:
9453     return BB;
9454 
9455   case ARM::Int_eh_sjlj_setup_dispatch:
9456     EmitSjLjDispatchBlock(MI, BB);
9457     return BB;
9458 
9459   case ARM::ABS:
9460   case ARM::t2ABS: {
9461     // To insert an ABS instruction, we have to insert the
9462     // diamond control-flow pattern.  The incoming instruction knows the
9463     // source vreg to test against 0, the destination vreg to set,
9464     // the condition code register to branch on, the
9465     // true/false values to select between, and a branch opcode to use.
9466     // It transforms
9467     //     V1 = ABS V0
9468     // into
9469     //     V2 = MOVS V0
9470     //     BCC                      (branch to SinkBB if V0 >= 0)
9471     //     RSBBB: V3 = RSBri V2, 0  (compute ABS if V2 < 0)
9472     //     SinkBB: V1 = PHI(V2, V3)
9473     const BasicBlock *LLVM_BB = BB->getBasicBlock();
9474     MachineFunction::iterator BBI = ++BB->getIterator();
9475     MachineFunction *Fn = BB->getParent();
9476     MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
9477     MachineBasicBlock *SinkBB  = Fn->CreateMachineBasicBlock(LLVM_BB);
9478     Fn->insert(BBI, RSBBB);
9479     Fn->insert(BBI, SinkBB);
9480 
9481     unsigned int ABSSrcReg = MI.getOperand(1).getReg();
9482     unsigned int ABSDstReg = MI.getOperand(0).getReg();
9483     bool ABSSrcKIll = MI.getOperand(1).isKill();
9484     bool isThumb2 = Subtarget->isThumb2();
9485     MachineRegisterInfo &MRI = Fn->getRegInfo();
9486     // In Thumb mode S must not be specified if source register is the SP or
9487     // PC and if destination register is the SP, so restrict register class
9488     unsigned NewRsbDstReg =
9489       MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
9490 
9491     // Transfer the remainder of BB and its successor edges to sinkMBB.
9492     SinkBB->splice(SinkBB->begin(), BB,
9493                    std::next(MachineBasicBlock::iterator(MI)), BB->end());
9494     SinkBB->transferSuccessorsAndUpdatePHIs(BB);
9495 
9496     BB->addSuccessor(RSBBB);
9497     BB->addSuccessor(SinkBB);
9498 
9499     // fall through to SinkMBB
9500     RSBBB->addSuccessor(SinkBB);
9501 
9502     // insert a cmp at the end of BB
9503     BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
9504         .addReg(ABSSrcReg)
9505         .addImm(0)
9506         .add(predOps(ARMCC::AL));
9507 
9508     // insert a bcc with opposite CC to ARMCC::MI at the end of BB
9509     BuildMI(BB, dl,
9510       TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
9511       .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
9512 
9513     // insert rsbri in RSBBB
9514     // Note: BCC and rsbri will be converted into predicated rsbmi
9515     // by if-conversion pass
9516     BuildMI(*RSBBB, RSBBB->begin(), dl,
9517             TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
9518         .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
9519         .addImm(0)
9520         .add(predOps(ARMCC::AL))
9521         .add(condCodeOp());
9522 
9523     // insert PHI in SinkBB,
9524     // reuse ABSDstReg to not change uses of ABS instruction
9525     BuildMI(*SinkBB, SinkBB->begin(), dl,
9526       TII->get(ARM::PHI), ABSDstReg)
9527       .addReg(NewRsbDstReg).addMBB(RSBBB)
9528       .addReg(ABSSrcReg).addMBB(BB);
9529 
9530     // remove ABS instruction
9531     MI.eraseFromParent();
9532 
9533     // return last added BB
9534     return SinkBB;
9535   }
9536   case ARM::COPY_STRUCT_BYVAL_I32:
9537     ++NumLoopByVals;
9538     return EmitStructByval(MI, BB);
9539   case ARM::WIN__CHKSTK:
9540     return EmitLowered__chkstk(MI, BB);
9541   case ARM::WIN__DBZCHK:
9542     return EmitLowered__dbzchk(MI, BB);
9543   }
9544 }
9545 
9546 /// Attaches vregs to MEMCPY that it will use as scratch registers
9547 /// when it is expanded into LDM/STM. This is done as a post-isel lowering
9548 /// instead of as a custom inserter because we need the use list from the SDNode.
9549 static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
9550                                     MachineInstr &MI, const SDNode *Node) {
9551   bool isThumb1 = Subtarget->isThumb1Only();
9552 
9553   DebugLoc DL = MI.getDebugLoc();
9554   MachineFunction *MF = MI.getParent()->getParent();
9555   MachineRegisterInfo &MRI = MF->getRegInfo();
9556   MachineInstrBuilder MIB(*MF, MI);
9557 
9558   // If the new dst/src is unused mark it as dead.
9559   if (!Node->hasAnyUseOfValue(0)) {
9560     MI.getOperand(0).setIsDead(true);
9561   }
9562   if (!Node->hasAnyUseOfValue(1)) {
9563     MI.getOperand(1).setIsDead(true);
9564   }
9565 
9566   // The MEMCPY both defines and kills the scratch registers.
9567   for (unsigned I = 0; I != MI.getOperand(4).getImm(); ++I) {
9568     unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
9569                                                          : &ARM::GPRRegClass);
9570     MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
9571   }
9572 }
9573 
9574 void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
9575                                                       SDNode *Node) const {
9576   if (MI.getOpcode() == ARM::MEMCPY) {
9577     attachMEMCPYScratchRegs(Subtarget, MI, Node);
9578     return;
9579   }
9580 
9581   const MCInstrDesc *MCID = &MI.getDesc();
9582   // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
9583   // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
9584   // operand is still set to noreg. If needed, set the optional operand's
9585   // register to CPSR, and remove the redundant implicit def.
9586   //
9587   // e.g. ADCS (..., implicit-def CPSR) -> ADC (... opt:def CPSR).
9588 
9589   // Rename pseudo opcodes.
9590   unsigned NewOpc = convertAddSubFlagsOpcode(MI.getOpcode());
9591   unsigned ccOutIdx;
9592   if (NewOpc) {
9593     const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
9594     MCID = &TII->get(NewOpc);
9595 
9596     assert(MCID->getNumOperands() ==
9597            MI.getDesc().getNumOperands() + 5 - MI.getDesc().getSize()
9598         && "converted opcode should be the same except for cc_out"
9599            " (and, on Thumb1, pred)");
9600 
9601     MI.setDesc(*MCID);
9602 
9603     // Add the optional cc_out operand
9604     MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
9605 
9606     // On Thumb1, move all input operands to the end, then add the predicate
9607     if (Subtarget->isThumb1Only()) {
9608       for (unsigned c = MCID->getNumOperands() - 4; c--;) {
9609         MI.addOperand(MI.getOperand(1));
9610         MI.RemoveOperand(1);
9611       }
9612 
9613       // Restore the ties
9614       for (unsigned i = MI.getNumOperands(); i--;) {
9615         const MachineOperand& op = MI.getOperand(i);
9616         if (op.isReg() && op.isUse()) {
9617           int DefIdx = MCID->getOperandConstraint(i, MCOI::TIED_TO);
9618           if (DefIdx != -1)
9619             MI.tieOperands(DefIdx, i);
9620         }
9621       }
9622 
9623       MI.addOperand(MachineOperand::CreateImm(ARMCC::AL));
9624       MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/false));
9625       ccOutIdx = 1;
9626     } else
9627       ccOutIdx = MCID->getNumOperands() - 1;
9628   } else
9629     ccOutIdx = MCID->getNumOperands() - 1;
9630 
9631   // Any ARM instruction that sets the 's' bit should specify an optional
9632   // "cc_out" operand in the last operand position.
9633   if (!MI.hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
9634     assert(!NewOpc && "Optional cc_out operand required");
9635     return;
9636   }
9637   // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
9638   // since we already have an optional CPSR def.
9639   bool definesCPSR = false;
9640   bool deadCPSR = false;
9641   for (unsigned i = MCID->getNumOperands(), e = MI.getNumOperands(); i != e;
9642        ++i) {
9643     const MachineOperand &MO = MI.getOperand(i);
9644     if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
9645       definesCPSR = true;
9646       if (MO.isDead())
9647         deadCPSR = true;
9648       MI.RemoveOperand(i);
9649       break;
9650     }
9651   }
9652   if (!definesCPSR) {
9653     assert(!NewOpc && "Optional cc_out operand required");
9654     return;
9655   }
9656   assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
9657   if (deadCPSR) {
9658     assert(!MI.getOperand(ccOutIdx).getReg() &&
9659            "expect uninitialized optional cc_out operand");
9660     // Thumb1 instructions must have the S bit even if the CPSR is dead.
9661     if (!Subtarget->isThumb1Only())
9662       return;
9663   }
9664 
9665   // If this instruction was defined with an optional CPSR def and its dag node
9666   // had a live implicit CPSR def, then activate the optional CPSR def.
9667   MachineOperand &MO = MI.getOperand(ccOutIdx);
9668   MO.setReg(ARM::CPSR);
9669   MO.setIsDef(true);
9670 }
9671 
9672 //===----------------------------------------------------------------------===//
9673 //                           ARM Optimization Hooks
9674 //===----------------------------------------------------------------------===//
9675 
9676 // Helper function that checks if N is a null or all ones constant.
9677 static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
9678   return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
9679 }
9680 
9681 // Return true if N is conditionally 0 or all ones.
9682 // Detects these expressions where cc is an i1 value:
9683 //
9684 //   (select cc 0, y)   [AllOnes=0]
9685 //   (select cc y, 0)   [AllOnes=0]
9686 //   (zext cc)          [AllOnes=0]
9687 //   (sext cc)          [AllOnes=0/1]
9688 //   (select cc -1, y)  [AllOnes=1]
9689 //   (select cc y, -1)  [AllOnes=1]
9690 //
9691 // Invert is set when N is the null/all ones constant when CC is false.
9692 // OtherOp is set to the alternative value of N.
9693 static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
9694                                        SDValue &CC, bool &Invert,
9695                                        SDValue &OtherOp,
9696                                        SelectionDAG &DAG) {
9697   switch (N->getOpcode()) {
9698   default: return false;
9699   case ISD::SELECT: {
9700     CC = N->getOperand(0);
9701     SDValue N1 = N->getOperand(1);
9702     SDValue N2 = N->getOperand(2);
9703     if (isZeroOrAllOnes(N1, AllOnes)) {
9704       Invert = false;
9705       OtherOp = N2;
9706       return true;
9707     }
9708     if (isZeroOrAllOnes(N2, AllOnes)) {
9709       Invert = true;
9710       OtherOp = N1;
9711       return true;
9712     }
9713     return false;
9714   }
9715   case ISD::ZERO_EXTEND:
9716     // (zext cc) can never be the all ones value.
9717     if (AllOnes)
9718       return false;
9719     LLVM_FALLTHROUGH;
9720   case ISD::SIGN_EXTEND: {
9721     SDLoc dl(N);
9722     EVT VT = N->getValueType(0);
9723     CC = N->getOperand(0);
9724     if (CC.getValueType() != MVT::i1 || CC.getOpcode() != ISD::SETCC)
9725       return false;
9726     Invert = !AllOnes;
9727     if (AllOnes)
9728       // When looking for an AllOnes constant, N is an sext, and the 'other'
9729       // value is 0.
9730       OtherOp = DAG.getConstant(0, dl, VT);
9731     else if (N->getOpcode() == ISD::ZERO_EXTEND)
9732       // When looking for a 0 constant, N can be zext or sext.
9733       OtherOp = DAG.getConstant(1, dl, VT);
9734     else
9735       OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
9736                                 VT);
9737     return true;
9738   }
9739   }
9740 }
9741 
9742 // Combine a constant select operand into its use:
9743 //
9744 //   (add (select cc, 0, c), x)  -> (select cc, x, (add, x, c))
9745 //   (sub x, (select cc, 0, c))  -> (select cc, x, (sub, x, c))
9746 //   (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))  [AllOnes=1]
9747 //   (or  (select cc, 0, c), x)  -> (select cc, x, (or, x, c))
9748 //   (xor (select cc, 0, c), x)  -> (select cc, x, (xor, x, c))
9749 //
9750 // The transform is rejected if the select doesn't have a constant operand that
9751 // is null, or all ones when AllOnes is set.
9752 //
9753 // Also recognize sext/zext from i1:
9754 //
9755 //   (add (zext cc), x) -> (select cc (add x, 1), x)
9756 //   (add (sext cc), x) -> (select cc (add x, -1), x)
9757 //
9758 // These transformations eventually create predicated instructions.
9759 //
9760 // @param N       The node to transform.
9761 // @param Slct    The N operand that is a select.
9762 // @param OtherOp The other N operand (x above).
9763 // @param DCI     Context.
9764 // @param AllOnes Require the select constant to be all ones instead of null.
9765 // @returns The new node, or SDValue() on failure.
9766 static
9767 SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
9768                             TargetLowering::DAGCombinerInfo &DCI,
9769                             bool AllOnes = false) {
9770   SelectionDAG &DAG = DCI.DAG;
9771   EVT VT = N->getValueType(0);
9772   SDValue NonConstantVal;
9773   SDValue CCOp;
9774   bool SwapSelectOps;
9775   if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
9776                                   NonConstantVal, DAG))
9777     return SDValue();
9778 
9779   // Slct is now know to be the desired identity constant when CC is true.
9780   SDValue TrueVal = OtherOp;
9781   SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
9782                                  OtherOp, NonConstantVal);
9783   // Unless SwapSelectOps says CC should be false.
9784   if (SwapSelectOps)
9785     std::swap(TrueVal, FalseVal);
9786 
9787   return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
9788                      CCOp, TrueVal, FalseVal);
9789 }
9790 
9791 // Attempt combineSelectAndUse on each operand of a commutative operator N.
9792 static
9793 SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
9794                                        TargetLowering::DAGCombinerInfo &DCI) {
9795   SDValue N0 = N->getOperand(0);
9796   SDValue N1 = N->getOperand(1);
9797   if (N0.getNode()->hasOneUse())
9798     if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
9799       return Result;
9800   if (N1.getNode()->hasOneUse())
9801     if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
9802       return Result;
9803   return SDValue();
9804 }
9805 
9806 static bool IsVUZPShuffleNode(SDNode *N) {
9807   // VUZP shuffle node.
9808   if (N->getOpcode() == ARMISD::VUZP)
9809     return true;
9810 
9811   // "VUZP" on i32 is an alias for VTRN.
9812   if (N->getOpcode() == ARMISD::VTRN && N->getValueType(0) == MVT::v2i32)
9813     return true;
9814 
9815   return false;
9816 }
9817 
9818 static SDValue AddCombineToVPADD(SDNode *N, SDValue N0, SDValue N1,
9819                                  TargetLowering::DAGCombinerInfo &DCI,
9820                                  const ARMSubtarget *Subtarget) {
9821   // Look for ADD(VUZP.0, VUZP.1).
9822   if (!IsVUZPShuffleNode(N0.getNode()) || N0.getNode() != N1.getNode() ||
9823       N0 == N1)
9824    return SDValue();
9825 
9826   // Make sure the ADD is a 64-bit add; there is no 128-bit VPADD.
9827   if (!N->getValueType(0).is64BitVector())
9828     return SDValue();
9829 
9830   // Generate vpadd.
9831   SelectionDAG &DAG = DCI.DAG;
9832   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9833   SDLoc dl(N);
9834   SDNode *Unzip = N0.getNode();
9835   EVT VT = N->getValueType(0);
9836 
9837   SmallVector<SDValue, 8> Ops;
9838   Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpadd, dl,
9839                                 TLI.getPointerTy(DAG.getDataLayout())));
9840   Ops.push_back(Unzip->getOperand(0));
9841   Ops.push_back(Unzip->getOperand(1));
9842 
9843   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, Ops);
9844 }
9845 
9846 static SDValue AddCombineVUZPToVPADDL(SDNode *N, SDValue N0, SDValue N1,
9847                                       TargetLowering::DAGCombinerInfo &DCI,
9848                                       const ARMSubtarget *Subtarget) {
9849   // Check for two extended operands.
9850   if (!(N0.getOpcode() == ISD::SIGN_EXTEND &&
9851         N1.getOpcode() == ISD::SIGN_EXTEND) &&
9852       !(N0.getOpcode() == ISD::ZERO_EXTEND &&
9853         N1.getOpcode() == ISD::ZERO_EXTEND))
9854     return SDValue();
9855 
9856   SDValue N00 = N0.getOperand(0);
9857   SDValue N10 = N1.getOperand(0);
9858 
9859   // Look for ADD(SEXT(VUZP.0), SEXT(VUZP.1))
9860   if (!IsVUZPShuffleNode(N00.getNode()) || N00.getNode() != N10.getNode() ||
9861       N00 == N10)
9862     return SDValue();
9863 
9864   // We only recognize Q register paddl here; this can't be reached until
9865   // after type legalization.
9866   if (!N00.getValueType().is64BitVector() ||
9867       !N0.getValueType().is128BitVector())
9868     return SDValue();
9869 
9870   // Generate vpaddl.
9871   SelectionDAG &DAG = DCI.DAG;
9872   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9873   SDLoc dl(N);
9874   EVT VT = N->getValueType(0);
9875 
9876   SmallVector<SDValue, 8> Ops;
9877   // Form vpaddl.sN or vpaddl.uN depending on the kind of extension.
9878   unsigned Opcode;
9879   if (N0.getOpcode() == ISD::SIGN_EXTEND)
9880     Opcode = Intrinsic::arm_neon_vpaddls;
9881   else
9882     Opcode = Intrinsic::arm_neon_vpaddlu;
9883   Ops.push_back(DAG.getConstant(Opcode, dl,
9884                                 TLI.getPointerTy(DAG.getDataLayout())));
9885   EVT ElemTy = N00.getValueType().getVectorElementType();
9886   unsigned NumElts = VT.getVectorNumElements();
9887   EVT ConcatVT = EVT::getVectorVT(*DAG.getContext(), ElemTy, NumElts * 2);
9888   SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), ConcatVT,
9889                                N00.getOperand(0), N00.getOperand(1));
9890   Ops.push_back(Concat);
9891 
9892   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT, Ops);
9893 }
9894 
9895 // FIXME: This function shouldn't be necessary; if we lower BUILD_VECTOR in
9896 // an appropriate manner, we end up with ADD(VUZP(ZEXT(N))), which is
9897 // much easier to match.
9898 static SDValue
9899 AddCombineBUILD_VECTORToVPADDL(SDNode *N, SDValue N0, SDValue N1,
9900                                TargetLowering::DAGCombinerInfo &DCI,
9901                                const ARMSubtarget *Subtarget) {
9902   // Only perform optimization if after legalize, and if NEON is available. We
9903   // also expected both operands to be BUILD_VECTORs.
9904   if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
9905       || N0.getOpcode() != ISD::BUILD_VECTOR
9906       || N1.getOpcode() != ISD::BUILD_VECTOR)
9907     return SDValue();
9908 
9909   // Check output type since VPADDL operand elements can only be 8, 16, or 32.
9910   EVT VT = N->getValueType(0);
9911   if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
9912     return SDValue();
9913 
9914   // Check that the vector operands are of the right form.
9915   // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
9916   // operands, where N is the size of the formed vector.
9917   // Each EXTRACT_VECTOR should have the same input vector and odd or even
9918   // index such that we have a pair wise add pattern.
9919 
9920   // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
9921   if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
9922     return SDValue();
9923   SDValue Vec = N0->getOperand(0)->getOperand(0);
9924   SDNode *V = Vec.getNode();
9925   unsigned nextIndex = 0;
9926 
9927   // For each operands to the ADD which are BUILD_VECTORs,
9928   // check to see if each of their operands are an EXTRACT_VECTOR with
9929   // the same vector and appropriate index.
9930   for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
9931     if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
9932         && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9933 
9934       SDValue ExtVec0 = N0->getOperand(i);
9935       SDValue ExtVec1 = N1->getOperand(i);
9936 
9937       // First operand is the vector, verify its the same.
9938       if (V != ExtVec0->getOperand(0).getNode() ||
9939           V != ExtVec1->getOperand(0).getNode())
9940         return SDValue();
9941 
9942       // Second is the constant, verify its correct.
9943       ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
9944       ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
9945 
9946       // For the constant, we want to see all the even or all the odd.
9947       if (!C0 || !C1 || C0->getZExtValue() != nextIndex
9948           || C1->getZExtValue() != nextIndex+1)
9949         return SDValue();
9950 
9951       // Increment index.
9952       nextIndex+=2;
9953     } else
9954       return SDValue();
9955   }
9956 
9957   // Don't generate vpaddl+vmovn; we'll match it to vpadd later. Also make sure
9958   // we're using the entire input vector, otherwise there's a size/legality
9959   // mismatch somewhere.
9960   if (nextIndex != Vec.getValueType().getVectorNumElements() ||
9961       Vec.getValueType().getVectorElementType() == VT.getVectorElementType())
9962     return SDValue();
9963 
9964   // Create VPADDL node.
9965   SelectionDAG &DAG = DCI.DAG;
9966   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9967 
9968   SDLoc dl(N);
9969 
9970   // Build operand list.
9971   SmallVector<SDValue, 8> Ops;
9972   Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
9973                                 TLI.getPointerTy(DAG.getDataLayout())));
9974 
9975   // Input is the vector.
9976   Ops.push_back(Vec);
9977 
9978   // Get widened type and narrowed type.
9979   MVT widenType;
9980   unsigned numElem = VT.getVectorNumElements();
9981 
9982   EVT inputLaneType = Vec.getValueType().getVectorElementType();
9983   switch (inputLaneType.getSimpleVT().SimpleTy) {
9984     case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
9985     case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
9986     case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
9987     default:
9988       llvm_unreachable("Invalid vector element type for padd optimization.");
9989   }
9990 
9991   SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
9992   unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
9993   return DAG.getNode(ExtOp, dl, VT, tmp);
9994 }
9995 
9996 static SDValue findMUL_LOHI(SDValue V) {
9997   if (V->getOpcode() == ISD::UMUL_LOHI ||
9998       V->getOpcode() == ISD::SMUL_LOHI)
9999     return V;
10000   return SDValue();
10001 }
10002 
10003 static SDValue AddCombineTo64BitSMLAL16(SDNode *AddcNode, SDNode *AddeNode,
10004                                         TargetLowering::DAGCombinerInfo &DCI,
10005                                         const ARMSubtarget *Subtarget) {
10006   if (Subtarget->isThumb()) {
10007     if (!Subtarget->hasDSP())
10008       return SDValue();
10009   } else if (!Subtarget->hasV5TEOps())
10010     return SDValue();
10011 
10012   // SMLALBB, SMLALBT, SMLALTB, SMLALTT multiply two 16-bit values and
10013   // accumulates the product into a 64-bit value. The 16-bit values will
10014   // be sign extended somehow or SRA'd into 32-bit values
10015   // (addc (adde (mul 16bit, 16bit), lo), hi)
10016   SDValue Mul = AddcNode->getOperand(0);
10017   SDValue Lo = AddcNode->getOperand(1);
10018   if (Mul.getOpcode() != ISD::MUL) {
10019     Lo = AddcNode->getOperand(0);
10020     Mul = AddcNode->getOperand(1);
10021     if (Mul.getOpcode() != ISD::MUL)
10022       return SDValue();
10023   }
10024 
10025   SDValue SRA = AddeNode->getOperand(0);
10026   SDValue Hi = AddeNode->getOperand(1);
10027   if (SRA.getOpcode() != ISD::SRA) {
10028     SRA = AddeNode->getOperand(1);
10029     Hi = AddeNode->getOperand(0);
10030     if (SRA.getOpcode() != ISD::SRA)
10031       return SDValue();
10032   }
10033   if (auto Const = dyn_cast<ConstantSDNode>(SRA.getOperand(1))) {
10034     if (Const->getZExtValue() != 31)
10035       return SDValue();
10036   } else
10037     return SDValue();
10038 
10039   if (SRA.getOperand(0) != Mul)
10040     return SDValue();
10041 
10042   SelectionDAG &DAG = DCI.DAG;
10043   SDLoc dl(AddcNode);
10044   unsigned Opcode = 0;
10045   SDValue Op0;
10046   SDValue Op1;
10047 
10048   if (isS16(Mul.getOperand(0), DAG) && isS16(Mul.getOperand(1), DAG)) {
10049     Opcode = ARMISD::SMLALBB;
10050     Op0 = Mul.getOperand(0);
10051     Op1 = Mul.getOperand(1);
10052   } else if (isS16(Mul.getOperand(0), DAG) && isSRA16(Mul.getOperand(1))) {
10053     Opcode = ARMISD::SMLALBT;
10054     Op0 = Mul.getOperand(0);
10055     Op1 = Mul.getOperand(1).getOperand(0);
10056   } else if (isSRA16(Mul.getOperand(0)) && isS16(Mul.getOperand(1), DAG)) {
10057     Opcode = ARMISD::SMLALTB;
10058     Op0 = Mul.getOperand(0).getOperand(0);
10059     Op1 = Mul.getOperand(1);
10060   } else if (isSRA16(Mul.getOperand(0)) && isSRA16(Mul.getOperand(1))) {
10061     Opcode = ARMISD::SMLALTT;
10062     Op0 = Mul->getOperand(0).getOperand(0);
10063     Op1 = Mul->getOperand(1).getOperand(0);
10064   }
10065 
10066   if (!Op0 || !Op1)
10067     return SDValue();
10068 
10069   SDValue SMLAL = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
10070                               Op0, Op1, Lo, Hi);
10071   // Replace the ADDs' nodes uses by the MLA node's values.
10072   SDValue HiMLALResult(SMLAL.getNode(), 1);
10073   SDValue LoMLALResult(SMLAL.getNode(), 0);
10074 
10075   DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
10076   DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
10077 
10078   // Return original node to notify the driver to stop replacing.
10079   SDValue resNode(AddcNode, 0);
10080   return resNode;
10081 }
10082 
10083 static SDValue AddCombineTo64bitMLAL(SDNode *AddeSubeNode,
10084                                      TargetLowering::DAGCombinerInfo &DCI,
10085                                      const ARMSubtarget *Subtarget) {
10086   // Look for multiply add opportunities.
10087   // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
10088   // each add nodes consumes a value from ISD::UMUL_LOHI and there is
10089   // a glue link from the first add to the second add.
10090   // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
10091   // a S/UMLAL instruction.
10092   //                  UMUL_LOHI
10093   //                 / :lo    \ :hi
10094   //                V          \          [no multiline comment]
10095   //    loAdd ->  ADDC         |
10096   //                 \ :carry /
10097   //                  V      V
10098   //                    ADDE   <- hiAdd
10099   //
10100   // In the special case where only the higher part of a signed result is used
10101   // and the add to the low part of the result of ISD::UMUL_LOHI adds or subtracts
10102   // a constant with the exact value of 0x80000000, we recognize we are dealing
10103   // with a "rounded multiply and add" (or subtract) and transform it into
10104   // either a ARMISD::SMMLAR or ARMISD::SMMLSR respectively.
10105 
10106   assert((AddeSubeNode->getOpcode() == ARMISD::ADDE ||
10107           AddeSubeNode->getOpcode() == ARMISD::SUBE) &&
10108          "Expect an ADDE or SUBE");
10109 
10110   assert(AddeSubeNode->getNumOperands() == 3 &&
10111          AddeSubeNode->getOperand(2).getValueType() == MVT::i32 &&
10112          "ADDE node has the wrong inputs");
10113 
10114   // Check that we are chained to the right ADDC or SUBC node.
10115   SDNode *AddcSubcNode = AddeSubeNode->getOperand(2).getNode();
10116   if ((AddeSubeNode->getOpcode() == ARMISD::ADDE &&
10117        AddcSubcNode->getOpcode() != ARMISD::ADDC) ||
10118       (AddeSubeNode->getOpcode() == ARMISD::SUBE &&
10119        AddcSubcNode->getOpcode() != ARMISD::SUBC))
10120     return SDValue();
10121 
10122   SDValue AddcSubcOp0 = AddcSubcNode->getOperand(0);
10123   SDValue AddcSubcOp1 = AddcSubcNode->getOperand(1);
10124 
10125   // Check if the two operands are from the same mul_lohi node.
10126   if (AddcSubcOp0.getNode() == AddcSubcOp1.getNode())
10127     return SDValue();
10128 
10129   assert(AddcSubcNode->getNumValues() == 2 &&
10130          AddcSubcNode->getValueType(0) == MVT::i32 &&
10131          "Expect ADDC with two result values. First: i32");
10132 
10133   // Check that the ADDC adds the low result of the S/UMUL_LOHI. If not, it
10134   // maybe a SMLAL which multiplies two 16-bit values.
10135   if (AddeSubeNode->getOpcode() == ARMISD::ADDE &&
10136       AddcSubcOp0->getOpcode() != ISD::UMUL_LOHI &&
10137       AddcSubcOp0->getOpcode() != ISD::SMUL_LOHI &&
10138       AddcSubcOp1->getOpcode() != ISD::UMUL_LOHI &&
10139       AddcSubcOp1->getOpcode() != ISD::SMUL_LOHI)
10140     return AddCombineTo64BitSMLAL16(AddcSubcNode, AddeSubeNode, DCI, Subtarget);
10141 
10142   // Check for the triangle shape.
10143   SDValue AddeSubeOp0 = AddeSubeNode->getOperand(0);
10144   SDValue AddeSubeOp1 = AddeSubeNode->getOperand(1);
10145 
10146   // Make sure that the ADDE/SUBE operands are not coming from the same node.
10147   if (AddeSubeOp0.getNode() == AddeSubeOp1.getNode())
10148     return SDValue();
10149 
10150   // Find the MUL_LOHI node walking up ADDE/SUBE's operands.
10151   bool IsLeftOperandMUL = false;
10152   SDValue MULOp = findMUL_LOHI(AddeSubeOp0);
10153   if (MULOp == SDValue())
10154     MULOp = findMUL_LOHI(AddeSubeOp1);
10155   else
10156     IsLeftOperandMUL = true;
10157   if (MULOp == SDValue())
10158     return SDValue();
10159 
10160   // Figure out the right opcode.
10161   unsigned Opc = MULOp->getOpcode();
10162   unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
10163 
10164   // Figure out the high and low input values to the MLAL node.
10165   SDValue *HiAddSub = nullptr;
10166   SDValue *LoMul = nullptr;
10167   SDValue *LowAddSub = nullptr;
10168 
10169   // Ensure that ADDE/SUBE is from high result of ISD::xMUL_LOHI.
10170   if ((AddeSubeOp0 != MULOp.getValue(1)) && (AddeSubeOp1 != MULOp.getValue(1)))
10171     return SDValue();
10172 
10173   if (IsLeftOperandMUL)
10174     HiAddSub = &AddeSubeOp1;
10175   else
10176     HiAddSub = &AddeSubeOp0;
10177 
10178   // Ensure that LoMul and LowAddSub are taken from correct ISD::SMUL_LOHI node
10179   // whose low result is fed to the ADDC/SUBC we are checking.
10180 
10181   if (AddcSubcOp0 == MULOp.getValue(0)) {
10182     LoMul = &AddcSubcOp0;
10183     LowAddSub = &AddcSubcOp1;
10184   }
10185   if (AddcSubcOp1 == MULOp.getValue(0)) {
10186     LoMul = &AddcSubcOp1;
10187     LowAddSub = &AddcSubcOp0;
10188   }
10189 
10190   if (!LoMul)
10191     return SDValue();
10192 
10193   // If HiAddSub is the same node as ADDC/SUBC or is a predecessor of ADDC/SUBC
10194   // the replacement below will create a cycle.
10195   if (AddcSubcNode == HiAddSub->getNode() ||
10196       AddcSubcNode->isPredecessorOf(HiAddSub->getNode()))
10197     return SDValue();
10198 
10199   // Create the merged node.
10200   SelectionDAG &DAG = DCI.DAG;
10201 
10202   // Start building operand list.
10203   SmallVector<SDValue, 8> Ops;
10204   Ops.push_back(LoMul->getOperand(0));
10205   Ops.push_back(LoMul->getOperand(1));
10206 
10207   // Check whether we can use SMMLAR, SMMLSR or SMMULR instead.  For this to be
10208   // the case, we must be doing signed multiplication and only use the higher
10209   // part of the result of the MLAL, furthermore the LowAddSub must be a constant
10210   // addition or subtraction with the value of 0x800000.
10211   if (Subtarget->hasV6Ops() && Subtarget->hasDSP() && Subtarget->useMulOps() &&
10212       FinalOpc == ARMISD::SMLAL && !AddeSubeNode->hasAnyUseOfValue(1) &&
10213       LowAddSub->getNode()->getOpcode() == ISD::Constant &&
10214       static_cast<ConstantSDNode *>(LowAddSub->getNode())->getZExtValue() ==
10215           0x80000000) {
10216     Ops.push_back(*HiAddSub);
10217     if (AddcSubcNode->getOpcode() == ARMISD::SUBC) {
10218       FinalOpc = ARMISD::SMMLSR;
10219     } else {
10220       FinalOpc = ARMISD::SMMLAR;
10221     }
10222     SDValue NewNode = DAG.getNode(FinalOpc, SDLoc(AddcSubcNode), MVT::i32, Ops);
10223     DAG.ReplaceAllUsesOfValueWith(SDValue(AddeSubeNode, 0), NewNode);
10224 
10225     return SDValue(AddeSubeNode, 0);
10226   } else if (AddcSubcNode->getOpcode() == ARMISD::SUBC)
10227     // SMMLS is generated during instruction selection and the rest of this
10228     // function can not handle the case where AddcSubcNode is a SUBC.
10229     return SDValue();
10230 
10231   // Finish building the operand list for {U/S}MLAL
10232   Ops.push_back(*LowAddSub);
10233   Ops.push_back(*HiAddSub);
10234 
10235   SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcSubcNode),
10236                                  DAG.getVTList(MVT::i32, MVT::i32), Ops);
10237 
10238   // Replace the ADDs' nodes uses by the MLA node's values.
10239   SDValue HiMLALResult(MLALNode.getNode(), 1);
10240   DAG.ReplaceAllUsesOfValueWith(SDValue(AddeSubeNode, 0), HiMLALResult);
10241 
10242   SDValue LoMLALResult(MLALNode.getNode(), 0);
10243   DAG.ReplaceAllUsesOfValueWith(SDValue(AddcSubcNode, 0), LoMLALResult);
10244 
10245   // Return original node to notify the driver to stop replacing.
10246   return SDValue(AddeSubeNode, 0);
10247 }
10248 
10249 static SDValue AddCombineTo64bitUMAAL(SDNode *AddeNode,
10250                                       TargetLowering::DAGCombinerInfo &DCI,
10251                                       const ARMSubtarget *Subtarget) {
10252   // UMAAL is similar to UMLAL except that it adds two unsigned values.
10253   // While trying to combine for the other MLAL nodes, first search for the
10254   // chance to use UMAAL. Check if Addc uses a node which has already
10255   // been combined into a UMLAL. The other pattern is UMLAL using Addc/Adde
10256   // as the addend, and it's handled in PerformUMLALCombine.
10257 
10258   if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP())
10259     return AddCombineTo64bitMLAL(AddeNode, DCI, Subtarget);
10260 
10261   // Check that we have a glued ADDC node.
10262   SDNode* AddcNode = AddeNode->getOperand(2).getNode();
10263   if (AddcNode->getOpcode() != ARMISD::ADDC)
10264     return SDValue();
10265 
10266   // Find the converted UMAAL or quit if it doesn't exist.
10267   SDNode *UmlalNode = nullptr;
10268   SDValue AddHi;
10269   if (AddcNode->getOperand(0).getOpcode() == ARMISD::UMLAL) {
10270     UmlalNode = AddcNode->getOperand(0).getNode();
10271     AddHi = AddcNode->getOperand(1);
10272   } else if (AddcNode->getOperand(1).getOpcode() == ARMISD::UMLAL) {
10273     UmlalNode = AddcNode->getOperand(1).getNode();
10274     AddHi = AddcNode->getOperand(0);
10275   } else {
10276     return AddCombineTo64bitMLAL(AddeNode, DCI, Subtarget);
10277   }
10278 
10279   // The ADDC should be glued to an ADDE node, which uses the same UMLAL as
10280   // the ADDC as well as Zero.
10281   if (!isNullConstant(UmlalNode->getOperand(3)))
10282     return SDValue();
10283 
10284   if ((isNullConstant(AddeNode->getOperand(0)) &&
10285        AddeNode->getOperand(1).getNode() == UmlalNode) ||
10286       (AddeNode->getOperand(0).getNode() == UmlalNode &&
10287        isNullConstant(AddeNode->getOperand(1)))) {
10288     SelectionDAG &DAG = DCI.DAG;
10289     SDValue Ops[] = { UmlalNode->getOperand(0), UmlalNode->getOperand(1),
10290                       UmlalNode->getOperand(2), AddHi };
10291     SDValue UMAAL =  DAG.getNode(ARMISD::UMAAL, SDLoc(AddcNode),
10292                                  DAG.getVTList(MVT::i32, MVT::i32), Ops);
10293 
10294     // Replace the ADDs' nodes uses by the UMAAL node's values.
10295     DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), SDValue(UMAAL.getNode(), 1));
10296     DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), SDValue(UMAAL.getNode(), 0));
10297 
10298     // Return original node to notify the driver to stop replacing.
10299     return SDValue(AddeNode, 0);
10300   }
10301   return SDValue();
10302 }
10303 
10304 static SDValue PerformUMLALCombine(SDNode *N, SelectionDAG &DAG,
10305                                    const ARMSubtarget *Subtarget) {
10306   if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP())
10307     return SDValue();
10308 
10309   // Check that we have a pair of ADDC and ADDE as operands.
10310   // Both addends of the ADDE must be zero.
10311   SDNode* AddcNode = N->getOperand(2).getNode();
10312   SDNode* AddeNode = N->getOperand(3).getNode();
10313   if ((AddcNode->getOpcode() == ARMISD::ADDC) &&
10314       (AddeNode->getOpcode() == ARMISD::ADDE) &&
10315       isNullConstant(AddeNode->getOperand(0)) &&
10316       isNullConstant(AddeNode->getOperand(1)) &&
10317       (AddeNode->getOperand(2).getNode() == AddcNode))
10318     return DAG.getNode(ARMISD::UMAAL, SDLoc(N),
10319                        DAG.getVTList(MVT::i32, MVT::i32),
10320                        {N->getOperand(0), N->getOperand(1),
10321                         AddcNode->getOperand(0), AddcNode->getOperand(1)});
10322   else
10323     return SDValue();
10324 }
10325 
10326 static SDValue PerformAddcSubcCombine(SDNode *N,
10327                                       TargetLowering::DAGCombinerInfo &DCI,
10328                                       const ARMSubtarget *Subtarget) {
10329   SelectionDAG &DAG(DCI.DAG);
10330 
10331   if (N->getOpcode() == ARMISD::SUBC) {
10332     // (SUBC (ADDE 0, 0, C), 1) -> C
10333     SDValue LHS = N->getOperand(0);
10334     SDValue RHS = N->getOperand(1);
10335     if (LHS->getOpcode() == ARMISD::ADDE &&
10336         isNullConstant(LHS->getOperand(0)) &&
10337         isNullConstant(LHS->getOperand(1)) && isOneConstant(RHS)) {
10338       return DCI.CombineTo(N, SDValue(N, 0), LHS->getOperand(2));
10339     }
10340   }
10341 
10342   if (Subtarget->isThumb1Only()) {
10343     SDValue RHS = N->getOperand(1);
10344     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
10345       int32_t imm = C->getSExtValue();
10346       if (imm < 0 && imm > std::numeric_limits<int>::min()) {
10347         SDLoc DL(N);
10348         RHS = DAG.getConstant(-imm, DL, MVT::i32);
10349         unsigned Opcode = (N->getOpcode() == ARMISD::ADDC) ? ARMISD::SUBC
10350                                                            : ARMISD::ADDC;
10351         return DAG.getNode(Opcode, DL, N->getVTList(), N->getOperand(0), RHS);
10352       }
10353     }
10354   }
10355 
10356   return SDValue();
10357 }
10358 
10359 static SDValue PerformAddeSubeCombine(SDNode *N,
10360                                       TargetLowering::DAGCombinerInfo &DCI,
10361                                       const ARMSubtarget *Subtarget) {
10362   if (Subtarget->isThumb1Only()) {
10363     SelectionDAG &DAG = DCI.DAG;
10364     SDValue RHS = N->getOperand(1);
10365     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
10366       int64_t imm = C->getSExtValue();
10367       if (imm < 0) {
10368         SDLoc DL(N);
10369 
10370         // The with-carry-in form matches bitwise not instead of the negation.
10371         // Effectively, the inverse interpretation of the carry flag already
10372         // accounts for part of the negation.
10373         RHS = DAG.getConstant(~imm, DL, MVT::i32);
10374 
10375         unsigned Opcode = (N->getOpcode() == ARMISD::ADDE) ? ARMISD::SUBE
10376                                                            : ARMISD::ADDE;
10377         return DAG.getNode(Opcode, DL, N->getVTList(),
10378                            N->getOperand(0), RHS, N->getOperand(2));
10379       }
10380     }
10381   } else if (N->getOperand(1)->getOpcode() == ISD::SMUL_LOHI) {
10382     return AddCombineTo64bitMLAL(N, DCI, Subtarget);
10383   }
10384   return SDValue();
10385 }
10386 
10387 static SDValue PerformABSCombine(SDNode *N,
10388                                   TargetLowering::DAGCombinerInfo &DCI,
10389                                   const ARMSubtarget *Subtarget) {
10390   SDValue res;
10391   SelectionDAG &DAG = DCI.DAG;
10392   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10393 
10394   if (!TLI.expandABS(N, res, DAG))
10395     return SDValue();
10396 
10397   return res;
10398 }
10399 
10400 /// PerformADDECombine - Target-specific dag combine transform from
10401 /// ARMISD::ADDC, ARMISD::ADDE, and ISD::MUL_LOHI to MLAL or
10402 /// ARMISD::ADDC, ARMISD::ADDE and ARMISD::UMLAL to ARMISD::UMAAL
10403 static SDValue PerformADDECombine(SDNode *N,
10404                                   TargetLowering::DAGCombinerInfo &DCI,
10405                                   const ARMSubtarget *Subtarget) {
10406   // Only ARM and Thumb2 support UMLAL/SMLAL.
10407   if (Subtarget->isThumb1Only())
10408     return PerformAddeSubeCombine(N, DCI, Subtarget);
10409 
10410   // Only perform the checks after legalize when the pattern is available.
10411   if (DCI.isBeforeLegalize()) return SDValue();
10412 
10413   return AddCombineTo64bitUMAAL(N, DCI, Subtarget);
10414 }
10415 
10416 /// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
10417 /// operands N0 and N1.  This is a helper for PerformADDCombine that is
10418 /// called with the default operands, and if that fails, with commuted
10419 /// operands.
10420 static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
10421                                           TargetLowering::DAGCombinerInfo &DCI,
10422                                           const ARMSubtarget *Subtarget){
10423   // Attempt to create vpadd for this add.
10424   if (SDValue Result = AddCombineToVPADD(N, N0, N1, DCI, Subtarget))
10425     return Result;
10426 
10427   // Attempt to create vpaddl for this add.
10428   if (SDValue Result = AddCombineVUZPToVPADDL(N, N0, N1, DCI, Subtarget))
10429     return Result;
10430   if (SDValue Result = AddCombineBUILD_VECTORToVPADDL(N, N0, N1, DCI,
10431                                                       Subtarget))
10432     return Result;
10433 
10434   // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
10435   if (N0.getNode()->hasOneUse())
10436     if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI))
10437       return Result;
10438   return SDValue();
10439 }
10440 
10441 bool
10442 ARMTargetLowering::isDesirableToCommuteWithShift(const SDNode *N,
10443                                                  CombineLevel Level) const {
10444   if (Level == BeforeLegalizeTypes)
10445     return true;
10446 
10447   if (N->getOpcode() != ISD::SHL)
10448     return true;
10449 
10450   if (Subtarget->isThumb1Only()) {
10451     // Avoid making expensive immediates by commuting shifts. (This logic
10452     // only applies to Thumb1 because ARM and Thumb2 immediates can be shifted
10453     // for free.)
10454     if (N->getOpcode() != ISD::SHL)
10455       return true;
10456     SDValue N1 = N->getOperand(0);
10457     if (N1->getOpcode() != ISD::ADD && N1->getOpcode() != ISD::AND &&
10458         N1->getOpcode() != ISD::OR && N1->getOpcode() != ISD::XOR)
10459       return true;
10460     if (auto *Const = dyn_cast<ConstantSDNode>(N1->getOperand(1))) {
10461       if (Const->getAPIntValue().ult(256))
10462         return false;
10463       if (N1->getOpcode() == ISD::ADD && Const->getAPIntValue().slt(0) &&
10464           Const->getAPIntValue().sgt(-256))
10465         return false;
10466     }
10467     return true;
10468   }
10469 
10470   // Turn off commute-with-shift transform after legalization, so it doesn't
10471   // conflict with PerformSHLSimplify.  (We could try to detect when
10472   // PerformSHLSimplify would trigger more precisely, but it isn't
10473   // really necessary.)
10474   return false;
10475 }
10476 
10477 bool
10478 ARMTargetLowering::shouldFoldShiftPairToMask(const SDNode *N,
10479                                              CombineLevel Level) const {
10480   if (!Subtarget->isThumb1Only())
10481     return true;
10482 
10483   if (Level == BeforeLegalizeTypes)
10484     return true;
10485 
10486   return false;
10487 }
10488 
10489 static SDValue PerformSHLSimplify(SDNode *N,
10490                                 TargetLowering::DAGCombinerInfo &DCI,
10491                                 const ARMSubtarget *ST) {
10492   // Allow the generic combiner to identify potential bswaps.
10493   if (DCI.isBeforeLegalize())
10494     return SDValue();
10495 
10496   // DAG combiner will fold:
10497   // (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2)
10498   // (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2
10499   // Other code patterns that can be also be modified have the following form:
10500   // b + ((a << 1) | 510)
10501   // b + ((a << 1) & 510)
10502   // b + ((a << 1) ^ 510)
10503   // b + ((a << 1) + 510)
10504 
10505   // Many instructions can  perform the shift for free, but it requires both
10506   // the operands to be registers. If c1 << c2 is too large, a mov immediate
10507   // instruction will needed. So, unfold back to the original pattern if:
10508   // - if c1 and c2 are small enough that they don't require mov imms.
10509   // - the user(s) of the node can perform an shl
10510 
10511   // No shifted operands for 16-bit instructions.
10512   if (ST->isThumb() && ST->isThumb1Only())
10513     return SDValue();
10514 
10515   // Check that all the users could perform the shl themselves.
10516   for (auto U : N->uses()) {
10517     switch(U->getOpcode()) {
10518     default:
10519       return SDValue();
10520     case ISD::SUB:
10521     case ISD::ADD:
10522     case ISD::AND:
10523     case ISD::OR:
10524     case ISD::XOR:
10525     case ISD::SETCC:
10526     case ARMISD::CMP:
10527       // Check that the user isn't already using a constant because there
10528       // aren't any instructions that support an immediate operand and a
10529       // shifted operand.
10530       if (isa<ConstantSDNode>(U->getOperand(0)) ||
10531           isa<ConstantSDNode>(U->getOperand(1)))
10532         return SDValue();
10533 
10534       // Check that it's not already using a shift.
10535       if (U->getOperand(0).getOpcode() == ISD::SHL ||
10536           U->getOperand(1).getOpcode() == ISD::SHL)
10537         return SDValue();
10538       break;
10539     }
10540   }
10541 
10542   if (N->getOpcode() != ISD::ADD && N->getOpcode() != ISD::OR &&
10543       N->getOpcode() != ISD::XOR && N->getOpcode() != ISD::AND)
10544     return SDValue();
10545 
10546   if (N->getOperand(0).getOpcode() != ISD::SHL)
10547     return SDValue();
10548 
10549   SDValue SHL = N->getOperand(0);
10550 
10551   auto *C1ShlC2 = dyn_cast<ConstantSDNode>(N->getOperand(1));
10552   auto *C2 = dyn_cast<ConstantSDNode>(SHL.getOperand(1));
10553   if (!C1ShlC2 || !C2)
10554     return SDValue();
10555 
10556   APInt C2Int = C2->getAPIntValue();
10557   APInt C1Int = C1ShlC2->getAPIntValue();
10558 
10559   // Check that performing a lshr will not lose any information.
10560   APInt Mask = APInt::getHighBitsSet(C2Int.getBitWidth(),
10561                                      C2Int.getBitWidth() - C2->getZExtValue());
10562   if ((C1Int & Mask) != C1Int)
10563     return SDValue();
10564 
10565   // Shift the first constant.
10566   C1Int.lshrInPlace(C2Int);
10567 
10568   // The immediates are encoded as an 8-bit value that can be rotated.
10569   auto LargeImm = [](const APInt &Imm) {
10570     unsigned Zeros = Imm.countLeadingZeros() + Imm.countTrailingZeros();
10571     return Imm.getBitWidth() - Zeros > 8;
10572   };
10573 
10574   if (LargeImm(C1Int) || LargeImm(C2Int))
10575     return SDValue();
10576 
10577   SelectionDAG &DAG = DCI.DAG;
10578   SDLoc dl(N);
10579   SDValue X = SHL.getOperand(0);
10580   SDValue BinOp = DAG.getNode(N->getOpcode(), dl, MVT::i32, X,
10581                               DAG.getConstant(C1Int, dl, MVT::i32));
10582   // Shift left to compensate for the lshr of C1Int.
10583   SDValue Res = DAG.getNode(ISD::SHL, dl, MVT::i32, BinOp, SHL.getOperand(1));
10584 
10585   LLVM_DEBUG(dbgs() << "Simplify shl use:\n"; SHL.getOperand(0).dump();
10586              SHL.dump(); N->dump());
10587   LLVM_DEBUG(dbgs() << "Into:\n"; X.dump(); BinOp.dump(); Res.dump());
10588   return Res;
10589 }
10590 
10591 
10592 /// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
10593 ///
10594 static SDValue PerformADDCombine(SDNode *N,
10595                                  TargetLowering::DAGCombinerInfo &DCI,
10596                                  const ARMSubtarget *Subtarget) {
10597   SDValue N0 = N->getOperand(0);
10598   SDValue N1 = N->getOperand(1);
10599 
10600   // Only works one way, because it needs an immediate operand.
10601   if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget))
10602     return Result;
10603 
10604   // First try with the default operand order.
10605   if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget))
10606     return Result;
10607 
10608   // If that didn't work, try again with the operands commuted.
10609   return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
10610 }
10611 
10612 /// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
10613 ///
10614 static SDValue PerformSUBCombine(SDNode *N,
10615                                  TargetLowering::DAGCombinerInfo &DCI) {
10616   SDValue N0 = N->getOperand(0);
10617   SDValue N1 = N->getOperand(1);
10618 
10619   // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
10620   if (N1.getNode()->hasOneUse())
10621     if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI))
10622       return Result;
10623 
10624   return SDValue();
10625 }
10626 
10627 /// PerformVMULCombine
10628 /// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
10629 /// special multiplier accumulator forwarding.
10630 ///   vmul d3, d0, d2
10631 ///   vmla d3, d1, d2
10632 /// is faster than
10633 ///   vadd d3, d0, d1
10634 ///   vmul d3, d3, d2
10635 //  However, for (A + B) * (A + B),
10636 //    vadd d2, d0, d1
10637 //    vmul d3, d0, d2
10638 //    vmla d3, d1, d2
10639 //  is slower than
10640 //    vadd d2, d0, d1
10641 //    vmul d3, d2, d2
10642 static SDValue PerformVMULCombine(SDNode *N,
10643                                   TargetLowering::DAGCombinerInfo &DCI,
10644                                   const ARMSubtarget *Subtarget) {
10645   if (!Subtarget->hasVMLxForwarding())
10646     return SDValue();
10647 
10648   SelectionDAG &DAG = DCI.DAG;
10649   SDValue N0 = N->getOperand(0);
10650   SDValue N1 = N->getOperand(1);
10651   unsigned Opcode = N0.getOpcode();
10652   if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
10653       Opcode != ISD::FADD && Opcode != ISD::FSUB) {
10654     Opcode = N1.getOpcode();
10655     if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
10656         Opcode != ISD::FADD && Opcode != ISD::FSUB)
10657       return SDValue();
10658     std::swap(N0, N1);
10659   }
10660 
10661   if (N0 == N1)
10662     return SDValue();
10663 
10664   EVT VT = N->getValueType(0);
10665   SDLoc DL(N);
10666   SDValue N00 = N0->getOperand(0);
10667   SDValue N01 = N0->getOperand(1);
10668   return DAG.getNode(Opcode, DL, VT,
10669                      DAG.getNode(ISD::MUL, DL, VT, N00, N1),
10670                      DAG.getNode(ISD::MUL, DL, VT, N01, N1));
10671 }
10672 
10673 static SDValue PerformMULCombine(SDNode *N,
10674                                  TargetLowering::DAGCombinerInfo &DCI,
10675                                  const ARMSubtarget *Subtarget) {
10676   SelectionDAG &DAG = DCI.DAG;
10677 
10678   if (Subtarget->isThumb1Only())
10679     return SDValue();
10680 
10681   if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10682     return SDValue();
10683 
10684   EVT VT = N->getValueType(0);
10685   if (VT.is64BitVector() || VT.is128BitVector())
10686     return PerformVMULCombine(N, DCI, Subtarget);
10687   if (VT != MVT::i32)
10688     return SDValue();
10689 
10690   ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10691   if (!C)
10692     return SDValue();
10693 
10694   int64_t MulAmt = C->getSExtValue();
10695   unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
10696 
10697   ShiftAmt = ShiftAmt & (32 - 1);
10698   SDValue V = N->getOperand(0);
10699   SDLoc DL(N);
10700 
10701   SDValue Res;
10702   MulAmt >>= ShiftAmt;
10703 
10704   if (MulAmt >= 0) {
10705     if (isPowerOf2_32(MulAmt - 1)) {
10706       // (mul x, 2^N + 1) => (add (shl x, N), x)
10707       Res = DAG.getNode(ISD::ADD, DL, VT,
10708                         V,
10709                         DAG.getNode(ISD::SHL, DL, VT,
10710                                     V,
10711                                     DAG.getConstant(Log2_32(MulAmt - 1), DL,
10712                                                     MVT::i32)));
10713     } else if (isPowerOf2_32(MulAmt + 1)) {
10714       // (mul x, 2^N - 1) => (sub (shl x, N), x)
10715       Res = DAG.getNode(ISD::SUB, DL, VT,
10716                         DAG.getNode(ISD::SHL, DL, VT,
10717                                     V,
10718                                     DAG.getConstant(Log2_32(MulAmt + 1), DL,
10719                                                     MVT::i32)),
10720                         V);
10721     } else
10722       return SDValue();
10723   } else {
10724     uint64_t MulAmtAbs = -MulAmt;
10725     if (isPowerOf2_32(MulAmtAbs + 1)) {
10726       // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
10727       Res = DAG.getNode(ISD::SUB, DL, VT,
10728                         V,
10729                         DAG.getNode(ISD::SHL, DL, VT,
10730                                     V,
10731                                     DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
10732                                                     MVT::i32)));
10733     } else if (isPowerOf2_32(MulAmtAbs - 1)) {
10734       // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
10735       Res = DAG.getNode(ISD::ADD, DL, VT,
10736                         V,
10737                         DAG.getNode(ISD::SHL, DL, VT,
10738                                     V,
10739                                     DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
10740                                                     MVT::i32)));
10741       Res = DAG.getNode(ISD::SUB, DL, VT,
10742                         DAG.getConstant(0, DL, MVT::i32), Res);
10743     } else
10744       return SDValue();
10745   }
10746 
10747   if (ShiftAmt != 0)
10748     Res = DAG.getNode(ISD::SHL, DL, VT,
10749                       Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
10750 
10751   // Do not add new nodes to DAG combiner worklist.
10752   DCI.CombineTo(N, Res, false);
10753   return SDValue();
10754 }
10755 
10756 static SDValue CombineANDShift(SDNode *N,
10757                                TargetLowering::DAGCombinerInfo &DCI,
10758                                const ARMSubtarget *Subtarget) {
10759   // Allow DAGCombine to pattern-match before we touch the canonical form.
10760   if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10761     return SDValue();
10762 
10763   if (N->getValueType(0) != MVT::i32)
10764     return SDValue();
10765 
10766   ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10767   if (!N1C)
10768     return SDValue();
10769 
10770   uint32_t C1 = (uint32_t)N1C->getZExtValue();
10771   // Don't transform uxtb/uxth.
10772   if (C1 == 255 || C1 == 65535)
10773     return SDValue();
10774 
10775   SDNode *N0 = N->getOperand(0).getNode();
10776   if (!N0->hasOneUse())
10777     return SDValue();
10778 
10779   if (N0->getOpcode() != ISD::SHL && N0->getOpcode() != ISD::SRL)
10780     return SDValue();
10781 
10782   bool LeftShift = N0->getOpcode() == ISD::SHL;
10783 
10784   ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0->getOperand(1));
10785   if (!N01C)
10786     return SDValue();
10787 
10788   uint32_t C2 = (uint32_t)N01C->getZExtValue();
10789   if (!C2 || C2 >= 32)
10790     return SDValue();
10791 
10792   // Clear irrelevant bits in the mask.
10793   if (LeftShift)
10794     C1 &= (-1U << C2);
10795   else
10796     C1 &= (-1U >> C2);
10797 
10798   SelectionDAG &DAG = DCI.DAG;
10799   SDLoc DL(N);
10800 
10801   // We have a pattern of the form "(and (shl x, c2) c1)" or
10802   // "(and (srl x, c2) c1)", where c1 is a shifted mask. Try to
10803   // transform to a pair of shifts, to save materializing c1.
10804 
10805   // First pattern: right shift, then mask off leading bits.
10806   // FIXME: Use demanded bits?
10807   if (!LeftShift && isMask_32(C1)) {
10808     uint32_t C3 = countLeadingZeros(C1);
10809     if (C2 < C3) {
10810       SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, N0->getOperand(0),
10811                                 DAG.getConstant(C3 - C2, DL, MVT::i32));
10812       return DAG.getNode(ISD::SRL, DL, MVT::i32, SHL,
10813                          DAG.getConstant(C3, DL, MVT::i32));
10814     }
10815   }
10816 
10817   // First pattern, reversed: left shift, then mask off trailing bits.
10818   if (LeftShift && isMask_32(~C1)) {
10819     uint32_t C3 = countTrailingZeros(C1);
10820     if (C2 < C3) {
10821       SDValue SHL = DAG.getNode(ISD::SRL, DL, MVT::i32, N0->getOperand(0),
10822                                 DAG.getConstant(C3 - C2, DL, MVT::i32));
10823       return DAG.getNode(ISD::SHL, DL, MVT::i32, SHL,
10824                          DAG.getConstant(C3, DL, MVT::i32));
10825     }
10826   }
10827 
10828   // Second pattern: left shift, then mask off leading bits.
10829   // FIXME: Use demanded bits?
10830   if (LeftShift && isShiftedMask_32(C1)) {
10831     uint32_t Trailing = countTrailingZeros(C1);
10832     uint32_t C3 = countLeadingZeros(C1);
10833     if (Trailing == C2 && C2 + C3 < 32) {
10834       SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, N0->getOperand(0),
10835                                 DAG.getConstant(C2 + C3, DL, MVT::i32));
10836       return DAG.getNode(ISD::SRL, DL, MVT::i32, SHL,
10837                         DAG.getConstant(C3, DL, MVT::i32));
10838     }
10839   }
10840 
10841   // Second pattern, reversed: right shift, then mask off trailing bits.
10842   // FIXME: Handle other patterns of known/demanded bits.
10843   if (!LeftShift && isShiftedMask_32(C1)) {
10844     uint32_t Leading = countLeadingZeros(C1);
10845     uint32_t C3 = countTrailingZeros(C1);
10846     if (Leading == C2 && C2 + C3 < 32) {
10847       SDValue SHL = DAG.getNode(ISD::SRL, DL, MVT::i32, N0->getOperand(0),
10848                                 DAG.getConstant(C2 + C3, DL, MVT::i32));
10849       return DAG.getNode(ISD::SHL, DL, MVT::i32, SHL,
10850                          DAG.getConstant(C3, DL, MVT::i32));
10851     }
10852   }
10853 
10854   // FIXME: Transform "(and (shl x, c2) c1)" ->
10855   // "(shl (and x, c1>>c2), c2)" if "c1 >> c2" is a cheaper immediate than
10856   // c1.
10857   return SDValue();
10858 }
10859 
10860 static SDValue PerformANDCombine(SDNode *N,
10861                                  TargetLowering::DAGCombinerInfo &DCI,
10862                                  const ARMSubtarget *Subtarget) {
10863   // Attempt to use immediate-form VBIC
10864   BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
10865   SDLoc dl(N);
10866   EVT VT = N->getValueType(0);
10867   SelectionDAG &DAG = DCI.DAG;
10868 
10869   if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
10870     return SDValue();
10871 
10872   APInt SplatBits, SplatUndef;
10873   unsigned SplatBitSize;
10874   bool HasAnyUndefs;
10875   if (BVN &&
10876       BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
10877     if (SplatBitSize <= 64) {
10878       EVT VbicVT;
10879       SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
10880                                       SplatUndef.getZExtValue(), SplatBitSize,
10881                                       DAG, dl, VbicVT, VT.is128BitVector(),
10882                                       OtherModImm);
10883       if (Val.getNode()) {
10884         SDValue Input =
10885           DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
10886         SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
10887         return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
10888       }
10889     }
10890   }
10891 
10892   if (!Subtarget->isThumb1Only()) {
10893     // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
10894     if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI))
10895       return Result;
10896 
10897     if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget))
10898       return Result;
10899   }
10900 
10901   if (Subtarget->isThumb1Only())
10902     if (SDValue Result = CombineANDShift(N, DCI, Subtarget))
10903       return Result;
10904 
10905   return SDValue();
10906 }
10907 
10908 // Try combining OR nodes to SMULWB, SMULWT.
10909 static SDValue PerformORCombineToSMULWBT(SDNode *OR,
10910                                          TargetLowering::DAGCombinerInfo &DCI,
10911                                          const ARMSubtarget *Subtarget) {
10912   if (!Subtarget->hasV6Ops() ||
10913       (Subtarget->isThumb() &&
10914        (!Subtarget->hasThumb2() || !Subtarget->hasDSP())))
10915     return SDValue();
10916 
10917   SDValue SRL = OR->getOperand(0);
10918   SDValue SHL = OR->getOperand(1);
10919 
10920   if (SRL.getOpcode() != ISD::SRL || SHL.getOpcode() != ISD::SHL) {
10921     SRL = OR->getOperand(1);
10922     SHL = OR->getOperand(0);
10923   }
10924   if (!isSRL16(SRL) || !isSHL16(SHL))
10925     return SDValue();
10926 
10927   // The first operands to the shifts need to be the two results from the
10928   // same smul_lohi node.
10929   if ((SRL.getOperand(0).getNode() != SHL.getOperand(0).getNode()) ||
10930        SRL.getOperand(0).getOpcode() != ISD::SMUL_LOHI)
10931     return SDValue();
10932 
10933   SDNode *SMULLOHI = SRL.getOperand(0).getNode();
10934   if (SRL.getOperand(0) != SDValue(SMULLOHI, 0) ||
10935       SHL.getOperand(0) != SDValue(SMULLOHI, 1))
10936     return SDValue();
10937 
10938   // Now we have:
10939   // (or (srl (smul_lohi ?, ?), 16), (shl (smul_lohi ?, ?), 16)))
10940   // For SMUL[B|T] smul_lohi will take a 32-bit and a 16-bit arguments.
10941   // For SMUWB the 16-bit value will signed extended somehow.
10942   // For SMULWT only the SRA is required.
10943   // Check both sides of SMUL_LOHI
10944   SDValue OpS16 = SMULLOHI->getOperand(0);
10945   SDValue OpS32 = SMULLOHI->getOperand(1);
10946 
10947   SelectionDAG &DAG = DCI.DAG;
10948   if (!isS16(OpS16, DAG) && !isSRA16(OpS16)) {
10949     OpS16 = OpS32;
10950     OpS32 = SMULLOHI->getOperand(0);
10951   }
10952 
10953   SDLoc dl(OR);
10954   unsigned Opcode = 0;
10955   if (isS16(OpS16, DAG))
10956     Opcode = ARMISD::SMULWB;
10957   else if (isSRA16(OpS16)) {
10958     Opcode = ARMISD::SMULWT;
10959     OpS16 = OpS16->getOperand(0);
10960   }
10961   else
10962     return SDValue();
10963 
10964   SDValue Res = DAG.getNode(Opcode, dl, MVT::i32, OpS32, OpS16);
10965   DAG.ReplaceAllUsesOfValueWith(SDValue(OR, 0), Res);
10966   return SDValue(OR, 0);
10967 }
10968 
10969 static SDValue PerformORCombineToBFI(SDNode *N,
10970                                      TargetLowering::DAGCombinerInfo &DCI,
10971                                      const ARMSubtarget *Subtarget) {
10972   // BFI is only available on V6T2+
10973   if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
10974     return SDValue();
10975 
10976   EVT VT = N->getValueType(0);
10977   SDValue N0 = N->getOperand(0);
10978   SDValue N1 = N->getOperand(1);
10979   SelectionDAG &DAG = DCI.DAG;
10980   SDLoc DL(N);
10981   // 1) or (and A, mask), val => ARMbfi A, val, mask
10982   //      iff (val & mask) == val
10983   //
10984   // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
10985   //  2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
10986   //          && mask == ~mask2
10987   //  2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
10988   //          && ~mask == mask2
10989   //  (i.e., copy a bitfield value into another bitfield of the same width)
10990 
10991   if (VT != MVT::i32)
10992     return SDValue();
10993 
10994   SDValue N00 = N0.getOperand(0);
10995 
10996   // The value and the mask need to be constants so we can verify this is
10997   // actually a bitfield set. If the mask is 0xffff, we can do better
10998   // via a movt instruction, so don't use BFI in that case.
10999   SDValue MaskOp = N0.getOperand(1);
11000   ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
11001   if (!MaskC)
11002     return SDValue();
11003   unsigned Mask = MaskC->getZExtValue();
11004   if (Mask == 0xffff)
11005     return SDValue();
11006   SDValue Res;
11007   // Case (1): or (and A, mask), val => ARMbfi A, val, mask
11008   ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11009   if (N1C) {
11010     unsigned Val = N1C->getZExtValue();
11011     if ((Val & ~Mask) != Val)
11012       return SDValue();
11013 
11014     if (ARM::isBitFieldInvertedMask(Mask)) {
11015       Val >>= countTrailingZeros(~Mask);
11016 
11017       Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
11018                         DAG.getConstant(Val, DL, MVT::i32),
11019                         DAG.getConstant(Mask, DL, MVT::i32));
11020 
11021       DCI.CombineTo(N, Res, false);
11022       // Return value from the original node to inform the combiner than N is
11023       // now dead.
11024       return SDValue(N, 0);
11025     }
11026   } else if (N1.getOpcode() == ISD::AND) {
11027     // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
11028     ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
11029     if (!N11C)
11030       return SDValue();
11031     unsigned Mask2 = N11C->getZExtValue();
11032 
11033     // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
11034     // as is to match.
11035     if (ARM::isBitFieldInvertedMask(Mask) &&
11036         (Mask == ~Mask2)) {
11037       // The pack halfword instruction works better for masks that fit it,
11038       // so use that when it's available.
11039       if (Subtarget->hasDSP() &&
11040           (Mask == 0xffff || Mask == 0xffff0000))
11041         return SDValue();
11042       // 2a
11043       unsigned amt = countTrailingZeros(Mask2);
11044       Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
11045                         DAG.getConstant(amt, DL, MVT::i32));
11046       Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
11047                         DAG.getConstant(Mask, DL, MVT::i32));
11048       DCI.CombineTo(N, Res, false);
11049       // Return value from the original node to inform the combiner than N is
11050       // now dead.
11051       return SDValue(N, 0);
11052     } else if (ARM::isBitFieldInvertedMask(~Mask) &&
11053                (~Mask == Mask2)) {
11054       // The pack halfword instruction works better for masks that fit it,
11055       // so use that when it's available.
11056       if (Subtarget->hasDSP() &&
11057           (Mask2 == 0xffff || Mask2 == 0xffff0000))
11058         return SDValue();
11059       // 2b
11060       unsigned lsb = countTrailingZeros(Mask);
11061       Res = DAG.getNode(ISD::SRL, DL, VT, N00,
11062                         DAG.getConstant(lsb, DL, MVT::i32));
11063       Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
11064                         DAG.getConstant(Mask2, DL, MVT::i32));
11065       DCI.CombineTo(N, Res, false);
11066       // Return value from the original node to inform the combiner than N is
11067       // now dead.
11068       return SDValue(N, 0);
11069     }
11070   }
11071 
11072   if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
11073       N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
11074       ARM::isBitFieldInvertedMask(~Mask)) {
11075     // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
11076     // where lsb(mask) == #shamt and masked bits of B are known zero.
11077     SDValue ShAmt = N00.getOperand(1);
11078     unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
11079     unsigned LSB = countTrailingZeros(Mask);
11080     if (ShAmtC != LSB)
11081       return SDValue();
11082 
11083     Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
11084                       DAG.getConstant(~Mask, DL, MVT::i32));
11085 
11086     DCI.CombineTo(N, Res, false);
11087     // Return value from the original node to inform the combiner than N is
11088     // now dead.
11089     return SDValue(N, 0);
11090   }
11091 
11092   return SDValue();
11093 }
11094 
11095 /// PerformORCombine - Target-specific dag combine xforms for ISD::OR
11096 static SDValue PerformORCombine(SDNode *N,
11097                                 TargetLowering::DAGCombinerInfo &DCI,
11098                                 const ARMSubtarget *Subtarget) {
11099   // Attempt to use immediate-form VORR
11100   BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
11101   SDLoc dl(N);
11102   EVT VT = N->getValueType(0);
11103   SelectionDAG &DAG = DCI.DAG;
11104 
11105   if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11106     return SDValue();
11107 
11108   APInt SplatBits, SplatUndef;
11109   unsigned SplatBitSize;
11110   bool HasAnyUndefs;
11111   if (BVN && Subtarget->hasNEON() &&
11112       BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
11113     if (SplatBitSize <= 64) {
11114       EVT VorrVT;
11115       SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
11116                                       SplatUndef.getZExtValue(), SplatBitSize,
11117                                       DAG, dl, VorrVT, VT.is128BitVector(),
11118                                       OtherModImm);
11119       if (Val.getNode()) {
11120         SDValue Input =
11121           DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
11122         SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
11123         return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
11124       }
11125     }
11126   }
11127 
11128   if (!Subtarget->isThumb1Only()) {
11129     // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
11130     if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
11131       return Result;
11132     if (SDValue Result = PerformORCombineToSMULWBT(N, DCI, Subtarget))
11133       return Result;
11134   }
11135 
11136   SDValue N0 = N->getOperand(0);
11137   SDValue N1 = N->getOperand(1);
11138 
11139   // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
11140   if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
11141       DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
11142 
11143     // The code below optimizes (or (and X, Y), Z).
11144     // The AND operand needs to have a single user to make these optimizations
11145     // profitable.
11146     if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
11147       return SDValue();
11148 
11149     APInt SplatUndef;
11150     unsigned SplatBitSize;
11151     bool HasAnyUndefs;
11152 
11153     APInt SplatBits0, SplatBits1;
11154     BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
11155     BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
11156     // Ensure that the second operand of both ands are constants
11157     if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
11158                                       HasAnyUndefs) && !HasAnyUndefs) {
11159         if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
11160                                           HasAnyUndefs) && !HasAnyUndefs) {
11161             // Ensure that the bit width of the constants are the same and that
11162             // the splat arguments are logical inverses as per the pattern we
11163             // are trying to simplify.
11164             if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
11165                 SplatBits0 == ~SplatBits1) {
11166                 // Canonicalize the vector type to make instruction selection
11167                 // simpler.
11168                 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
11169                 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
11170                                              N0->getOperand(1),
11171                                              N0->getOperand(0),
11172                                              N1->getOperand(0));
11173                 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
11174             }
11175         }
11176     }
11177   }
11178 
11179   // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
11180   // reasonable.
11181   if (N0.getOpcode() == ISD::AND && N0.hasOneUse()) {
11182     if (SDValue Res = PerformORCombineToBFI(N, DCI, Subtarget))
11183       return Res;
11184   }
11185 
11186   if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget))
11187     return Result;
11188 
11189   return SDValue();
11190 }
11191 
11192 static SDValue PerformXORCombine(SDNode *N,
11193                                  TargetLowering::DAGCombinerInfo &DCI,
11194                                  const ARMSubtarget *Subtarget) {
11195   EVT VT = N->getValueType(0);
11196   SelectionDAG &DAG = DCI.DAG;
11197 
11198   if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11199     return SDValue();
11200 
11201   if (!Subtarget->isThumb1Only()) {
11202     // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
11203     if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
11204       return Result;
11205 
11206     if (SDValue Result = PerformSHLSimplify(N, DCI, Subtarget))
11207       return Result;
11208   }
11209 
11210   return SDValue();
11211 }
11212 
11213 // ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it,
11214 // and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and
11215 // their position in "to" (Rd).
11216 static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) {
11217   assert(N->getOpcode() == ARMISD::BFI);
11218 
11219   SDValue From = N->getOperand(1);
11220   ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue();
11221   FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation());
11222 
11223   // If the Base came from a SHR #C, we can deduce that it is really testing bit
11224   // #C in the base of the SHR.
11225   if (From->getOpcode() == ISD::SRL &&
11226       isa<ConstantSDNode>(From->getOperand(1))) {
11227     APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue();
11228     assert(Shift.getLimitedValue() < 32 && "Shift too large!");
11229     FromMask <<= Shift.getLimitedValue(31);
11230     From = From->getOperand(0);
11231   }
11232 
11233   return From;
11234 }
11235 
11236 // If A and B contain one contiguous set of bits, does A | B == A . B?
11237 //
11238 // Neither A nor B must be zero.
11239 static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
11240   unsigned LastActiveBitInA =  A.countTrailingZeros();
11241   unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
11242   return LastActiveBitInA - 1 == FirstActiveBitInB;
11243 }
11244 
11245 static SDValue FindBFIToCombineWith(SDNode *N) {
11246   // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with,
11247   // if one exists.
11248   APInt ToMask, FromMask;
11249   SDValue From = ParseBFI(N, ToMask, FromMask);
11250   SDValue To = N->getOperand(0);
11251 
11252   // Now check for a compatible BFI to merge with. We can pass through BFIs that
11253   // aren't compatible, but not if they set the same bit in their destination as
11254   // we do (or that of any BFI we're going to combine with).
11255   SDValue V = To;
11256   APInt CombinedToMask = ToMask;
11257   while (V.getOpcode() == ARMISD::BFI) {
11258     APInt NewToMask, NewFromMask;
11259     SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask);
11260     if (NewFrom != From) {
11261       // This BFI has a different base. Keep going.
11262       CombinedToMask |= NewToMask;
11263       V = V.getOperand(0);
11264       continue;
11265     }
11266 
11267     // Do the written bits conflict with any we've seen so far?
11268     if ((NewToMask & CombinedToMask).getBoolValue())
11269       // Conflicting bits - bail out because going further is unsafe.
11270       return SDValue();
11271 
11272     // Are the new bits contiguous when combined with the old bits?
11273     if (BitsProperlyConcatenate(ToMask, NewToMask) &&
11274         BitsProperlyConcatenate(FromMask, NewFromMask))
11275       return V;
11276     if (BitsProperlyConcatenate(NewToMask, ToMask) &&
11277         BitsProperlyConcatenate(NewFromMask, FromMask))
11278       return V;
11279 
11280     // We've seen a write to some bits, so track it.
11281     CombinedToMask |= NewToMask;
11282     // Keep going...
11283     V = V.getOperand(0);
11284   }
11285 
11286   return SDValue();
11287 }
11288 
11289 static SDValue PerformBFICombine(SDNode *N,
11290                                  TargetLowering::DAGCombinerInfo &DCI) {
11291   SDValue N1 = N->getOperand(1);
11292   if (N1.getOpcode() == ISD::AND) {
11293     // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
11294     // the bits being cleared by the AND are not demanded by the BFI.
11295     ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
11296     if (!N11C)
11297       return SDValue();
11298     unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
11299     unsigned LSB = countTrailingZeros(~InvMask);
11300     unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
11301     assert(Width <
11302                static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
11303            "undefined behavior");
11304     unsigned Mask = (1u << Width) - 1;
11305     unsigned Mask2 = N11C->getZExtValue();
11306     if ((Mask & (~Mask2)) == 0)
11307       return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
11308                              N->getOperand(0), N1.getOperand(0),
11309                              N->getOperand(2));
11310   } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) {
11311     // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes.
11312     // Keep track of any consecutive bits set that all come from the same base
11313     // value. We can combine these together into a single BFI.
11314     SDValue CombineBFI = FindBFIToCombineWith(N);
11315     if (CombineBFI == SDValue())
11316       return SDValue();
11317 
11318     // We've found a BFI.
11319     APInt ToMask1, FromMask1;
11320     SDValue From1 = ParseBFI(N, ToMask1, FromMask1);
11321 
11322     APInt ToMask2, FromMask2;
11323     SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2);
11324     assert(From1 == From2);
11325     (void)From2;
11326 
11327     // First, unlink CombineBFI.
11328     DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0));
11329     // Then create a new BFI, combining the two together.
11330     APInt NewFromMask = FromMask1 | FromMask2;
11331     APInt NewToMask = ToMask1 | ToMask2;
11332 
11333     EVT VT = N->getValueType(0);
11334     SDLoc dl(N);
11335 
11336     if (NewFromMask[0] == 0)
11337       From1 = DCI.DAG.getNode(
11338         ISD::SRL, dl, VT, From1,
11339         DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
11340     return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1,
11341                            DCI.DAG.getConstant(~NewToMask, dl, VT));
11342   }
11343   return SDValue();
11344 }
11345 
11346 /// PerformVMOVRRDCombine - Target-specific dag combine xforms for
11347 /// ARMISD::VMOVRRD.
11348 static SDValue PerformVMOVRRDCombine(SDNode *N,
11349                                      TargetLowering::DAGCombinerInfo &DCI,
11350                                      const ARMSubtarget *Subtarget) {
11351   // vmovrrd(vmovdrr x, y) -> x,y
11352   SDValue InDouble = N->getOperand(0);
11353   if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
11354     return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
11355 
11356   // vmovrrd(load f64) -> (load i32), (load i32)
11357   SDNode *InNode = InDouble.getNode();
11358   if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
11359       InNode->getValueType(0) == MVT::f64 &&
11360       InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
11361       !cast<LoadSDNode>(InNode)->isVolatile()) {
11362     // TODO: Should this be done for non-FrameIndex operands?
11363     LoadSDNode *LD = cast<LoadSDNode>(InNode);
11364 
11365     SelectionDAG &DAG = DCI.DAG;
11366     SDLoc DL(LD);
11367     SDValue BasePtr = LD->getBasePtr();
11368     SDValue NewLD1 =
11369         DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, LD->getPointerInfo(),
11370                     LD->getAlignment(), LD->getMemOperand()->getFlags());
11371 
11372     SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
11373                                     DAG.getConstant(4, DL, MVT::i32));
11374     SDValue NewLD2 = DAG.getLoad(
11375         MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, LD->getPointerInfo(),
11376         std::min(4U, LD->getAlignment() / 2), LD->getMemOperand()->getFlags());
11377 
11378     DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
11379     if (DCI.DAG.getDataLayout().isBigEndian())
11380       std::swap (NewLD1, NewLD2);
11381     SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
11382     return Result;
11383   }
11384 
11385   return SDValue();
11386 }
11387 
11388 /// PerformVMOVDRRCombine - Target-specific dag combine xforms for
11389 /// ARMISD::VMOVDRR.  This is also used for BUILD_VECTORs with 2 operands.
11390 static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
11391   // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
11392   SDValue Op0 = N->getOperand(0);
11393   SDValue Op1 = N->getOperand(1);
11394   if (Op0.getOpcode() == ISD::BITCAST)
11395     Op0 = Op0.getOperand(0);
11396   if (Op1.getOpcode() == ISD::BITCAST)
11397     Op1 = Op1.getOperand(0);
11398   if (Op0.getOpcode() == ARMISD::VMOVRRD &&
11399       Op0.getNode() == Op1.getNode() &&
11400       Op0.getResNo() == 0 && Op1.getResNo() == 1)
11401     return DAG.getNode(ISD::BITCAST, SDLoc(N),
11402                        N->getValueType(0), Op0.getOperand(0));
11403   return SDValue();
11404 }
11405 
11406 /// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
11407 /// are normal, non-volatile loads.  If so, it is profitable to bitcast an
11408 /// i64 vector to have f64 elements, since the value can then be loaded
11409 /// directly into a VFP register.
11410 static bool hasNormalLoadOperand(SDNode *N) {
11411   unsigned NumElts = N->getValueType(0).getVectorNumElements();
11412   for (unsigned i = 0; i < NumElts; ++i) {
11413     SDNode *Elt = N->getOperand(i).getNode();
11414     if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
11415       return true;
11416   }
11417   return false;
11418 }
11419 
11420 /// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
11421 /// ISD::BUILD_VECTOR.
11422 static SDValue PerformBUILD_VECTORCombine(SDNode *N,
11423                                           TargetLowering::DAGCombinerInfo &DCI,
11424                                           const ARMSubtarget *Subtarget) {
11425   // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
11426   // VMOVRRD is introduced when legalizing i64 types.  It forces the i64 value
11427   // into a pair of GPRs, which is fine when the value is used as a scalar,
11428   // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
11429   SelectionDAG &DAG = DCI.DAG;
11430   if (N->getNumOperands() == 2)
11431     if (SDValue RV = PerformVMOVDRRCombine(N, DAG))
11432       return RV;
11433 
11434   // Load i64 elements as f64 values so that type legalization does not split
11435   // them up into i32 values.
11436   EVT VT = N->getValueType(0);
11437   if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
11438     return SDValue();
11439   SDLoc dl(N);
11440   SmallVector<SDValue, 8> Ops;
11441   unsigned NumElts = VT.getVectorNumElements();
11442   for (unsigned i = 0; i < NumElts; ++i) {
11443     SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
11444     Ops.push_back(V);
11445     // Make the DAGCombiner fold the bitcast.
11446     DCI.AddToWorklist(V.getNode());
11447   }
11448   EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
11449   SDValue BV = DAG.getBuildVector(FloatVT, dl, Ops);
11450   return DAG.getNode(ISD::BITCAST, dl, VT, BV);
11451 }
11452 
11453 /// Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
11454 static SDValue
11455 PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
11456   // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
11457   // At that time, we may have inserted bitcasts from integer to float.
11458   // If these bitcasts have survived DAGCombine, change the lowering of this
11459   // BUILD_VECTOR in something more vector friendly, i.e., that does not
11460   // force to use floating point types.
11461 
11462   // Make sure we can change the type of the vector.
11463   // This is possible iff:
11464   // 1. The vector is only used in a bitcast to a integer type. I.e.,
11465   //    1.1. Vector is used only once.
11466   //    1.2. Use is a bit convert to an integer type.
11467   // 2. The size of its operands are 32-bits (64-bits are not legal).
11468   EVT VT = N->getValueType(0);
11469   EVT EltVT = VT.getVectorElementType();
11470 
11471   // Check 1.1. and 2.
11472   if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
11473     return SDValue();
11474 
11475   // By construction, the input type must be float.
11476   assert(EltVT == MVT::f32 && "Unexpected type!");
11477 
11478   // Check 1.2.
11479   SDNode *Use = *N->use_begin();
11480   if (Use->getOpcode() != ISD::BITCAST ||
11481       Use->getValueType(0).isFloatingPoint())
11482     return SDValue();
11483 
11484   // Check profitability.
11485   // Model is, if more than half of the relevant operands are bitcast from
11486   // i32, turn the build_vector into a sequence of insert_vector_elt.
11487   // Relevant operands are everything that is not statically
11488   // (i.e., at compile time) bitcasted.
11489   unsigned NumOfBitCastedElts = 0;
11490   unsigned NumElts = VT.getVectorNumElements();
11491   unsigned NumOfRelevantElts = NumElts;
11492   for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
11493     SDValue Elt = N->getOperand(Idx);
11494     if (Elt->getOpcode() == ISD::BITCAST) {
11495       // Assume only bit cast to i32 will go away.
11496       if (Elt->getOperand(0).getValueType() == MVT::i32)
11497         ++NumOfBitCastedElts;
11498     } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt))
11499       // Constants are statically casted, thus do not count them as
11500       // relevant operands.
11501       --NumOfRelevantElts;
11502   }
11503 
11504   // Check if more than half of the elements require a non-free bitcast.
11505   if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
11506     return SDValue();
11507 
11508   SelectionDAG &DAG = DCI.DAG;
11509   // Create the new vector type.
11510   EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
11511   // Check if the type is legal.
11512   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11513   if (!TLI.isTypeLegal(VecVT))
11514     return SDValue();
11515 
11516   // Combine:
11517   // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
11518   // => BITCAST INSERT_VECTOR_ELT
11519   //                      (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
11520   //                      (BITCAST EN), N.
11521   SDValue Vec = DAG.getUNDEF(VecVT);
11522   SDLoc dl(N);
11523   for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
11524     SDValue V = N->getOperand(Idx);
11525     if (V.isUndef())
11526       continue;
11527     if (V.getOpcode() == ISD::BITCAST &&
11528         V->getOperand(0).getValueType() == MVT::i32)
11529       // Fold obvious case.
11530       V = V.getOperand(0);
11531     else {
11532       V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
11533       // Make the DAGCombiner fold the bitcasts.
11534       DCI.AddToWorklist(V.getNode());
11535     }
11536     SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
11537     Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
11538   }
11539   Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
11540   // Make the DAGCombiner fold the bitcasts.
11541   DCI.AddToWorklist(Vec.getNode());
11542   return Vec;
11543 }
11544 
11545 /// PerformInsertEltCombine - Target-specific dag combine xforms for
11546 /// ISD::INSERT_VECTOR_ELT.
11547 static SDValue PerformInsertEltCombine(SDNode *N,
11548                                        TargetLowering::DAGCombinerInfo &DCI) {
11549   // Bitcast an i64 load inserted into a vector to f64.
11550   // Otherwise, the i64 value will be legalized to a pair of i32 values.
11551   EVT VT = N->getValueType(0);
11552   SDNode *Elt = N->getOperand(1).getNode();
11553   if (VT.getVectorElementType() != MVT::i64 ||
11554       !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
11555     return SDValue();
11556 
11557   SelectionDAG &DAG = DCI.DAG;
11558   SDLoc dl(N);
11559   EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
11560                                  VT.getVectorNumElements());
11561   SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
11562   SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
11563   // Make the DAGCombiner fold the bitcasts.
11564   DCI.AddToWorklist(Vec.getNode());
11565   DCI.AddToWorklist(V.getNode());
11566   SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
11567                                Vec, V, N->getOperand(2));
11568   return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
11569 }
11570 
11571 /// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
11572 /// ISD::VECTOR_SHUFFLE.
11573 static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
11574   // The LLVM shufflevector instruction does not require the shuffle mask
11575   // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
11576   // have that requirement.  When translating to ISD::VECTOR_SHUFFLE, if the
11577   // operands do not match the mask length, they are extended by concatenating
11578   // them with undef vectors.  That is probably the right thing for other
11579   // targets, but for NEON it is better to concatenate two double-register
11580   // size vector operands into a single quad-register size vector.  Do that
11581   // transformation here:
11582   //   shuffle(concat(v1, undef), concat(v2, undef)) ->
11583   //   shuffle(concat(v1, v2), undef)
11584   SDValue Op0 = N->getOperand(0);
11585   SDValue Op1 = N->getOperand(1);
11586   if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
11587       Op1.getOpcode() != ISD::CONCAT_VECTORS ||
11588       Op0.getNumOperands() != 2 ||
11589       Op1.getNumOperands() != 2)
11590     return SDValue();
11591   SDValue Concat0Op1 = Op0.getOperand(1);
11592   SDValue Concat1Op1 = Op1.getOperand(1);
11593   if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef())
11594     return SDValue();
11595   // Skip the transformation if any of the types are illegal.
11596   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11597   EVT VT = N->getValueType(0);
11598   if (!TLI.isTypeLegal(VT) ||
11599       !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
11600       !TLI.isTypeLegal(Concat1Op1.getValueType()))
11601     return SDValue();
11602 
11603   SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
11604                                   Op0.getOperand(0), Op1.getOperand(0));
11605   // Translate the shuffle mask.
11606   SmallVector<int, 16> NewMask;
11607   unsigned NumElts = VT.getVectorNumElements();
11608   unsigned HalfElts = NumElts/2;
11609   ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
11610   for (unsigned n = 0; n < NumElts; ++n) {
11611     int MaskElt = SVN->getMaskElt(n);
11612     int NewElt = -1;
11613     if (MaskElt < (int)HalfElts)
11614       NewElt = MaskElt;
11615     else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
11616       NewElt = HalfElts + MaskElt - NumElts;
11617     NewMask.push_back(NewElt);
11618   }
11619   return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
11620                               DAG.getUNDEF(VT), NewMask);
11621 }
11622 
11623 /// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
11624 /// NEON load/store intrinsics, and generic vector load/stores, to merge
11625 /// base address updates.
11626 /// For generic load/stores, the memory type is assumed to be a vector.
11627 /// The caller is assumed to have checked legality.
11628 static SDValue CombineBaseUpdate(SDNode *N,
11629                                  TargetLowering::DAGCombinerInfo &DCI) {
11630   SelectionDAG &DAG = DCI.DAG;
11631   const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
11632                             N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
11633   const bool isStore = N->getOpcode() == ISD::STORE;
11634   const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
11635   SDValue Addr = N->getOperand(AddrOpIdx);
11636   MemSDNode *MemN = cast<MemSDNode>(N);
11637   SDLoc dl(N);
11638 
11639   // Search for a use of the address operand that is an increment.
11640   for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
11641          UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
11642     SDNode *User = *UI;
11643     if (User->getOpcode() != ISD::ADD ||
11644         UI.getUse().getResNo() != Addr.getResNo())
11645       continue;
11646 
11647     // Check that the add is independent of the load/store.  Otherwise, folding
11648     // it would create a cycle. We can avoid searching through Addr as it's a
11649     // predecessor to both.
11650     SmallPtrSet<const SDNode *, 32> Visited;
11651     SmallVector<const SDNode *, 16> Worklist;
11652     Visited.insert(Addr.getNode());
11653     Worklist.push_back(N);
11654     Worklist.push_back(User);
11655     if (SDNode::hasPredecessorHelper(N, Visited, Worklist) ||
11656         SDNode::hasPredecessorHelper(User, Visited, Worklist))
11657       continue;
11658 
11659     // Find the new opcode for the updating load/store.
11660     bool isLoadOp = true;
11661     bool isLaneOp = false;
11662     unsigned NewOpc = 0;
11663     unsigned NumVecs = 0;
11664     if (isIntrinsic) {
11665       unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
11666       switch (IntNo) {
11667       default: llvm_unreachable("unexpected intrinsic for Neon base update");
11668       case Intrinsic::arm_neon_vld1:     NewOpc = ARMISD::VLD1_UPD;
11669         NumVecs = 1; break;
11670       case Intrinsic::arm_neon_vld2:     NewOpc = ARMISD::VLD2_UPD;
11671         NumVecs = 2; break;
11672       case Intrinsic::arm_neon_vld3:     NewOpc = ARMISD::VLD3_UPD;
11673         NumVecs = 3; break;
11674       case Intrinsic::arm_neon_vld4:     NewOpc = ARMISD::VLD4_UPD;
11675         NumVecs = 4; break;
11676       case Intrinsic::arm_neon_vld2dup:
11677       case Intrinsic::arm_neon_vld3dup:
11678       case Intrinsic::arm_neon_vld4dup:
11679         // TODO: Support updating VLDxDUP nodes. For now, we just skip
11680         // combining base updates for such intrinsics.
11681         continue;
11682       case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
11683         NumVecs = 2; isLaneOp = true; break;
11684       case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
11685         NumVecs = 3; isLaneOp = true; break;
11686       case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
11687         NumVecs = 4; isLaneOp = true; break;
11688       case Intrinsic::arm_neon_vst1:     NewOpc = ARMISD::VST1_UPD;
11689         NumVecs = 1; isLoadOp = false; break;
11690       case Intrinsic::arm_neon_vst2:     NewOpc = ARMISD::VST2_UPD;
11691         NumVecs = 2; isLoadOp = false; break;
11692       case Intrinsic::arm_neon_vst3:     NewOpc = ARMISD::VST3_UPD;
11693         NumVecs = 3; isLoadOp = false; break;
11694       case Intrinsic::arm_neon_vst4:     NewOpc = ARMISD::VST4_UPD;
11695         NumVecs = 4; isLoadOp = false; break;
11696       case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
11697         NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
11698       case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
11699         NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
11700       case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
11701         NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
11702       }
11703     } else {
11704       isLaneOp = true;
11705       switch (N->getOpcode()) {
11706       default: llvm_unreachable("unexpected opcode for Neon base update");
11707       case ARMISD::VLD1DUP: NewOpc = ARMISD::VLD1DUP_UPD; NumVecs = 1; break;
11708       case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
11709       case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
11710       case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
11711       case ISD::LOAD:       NewOpc = ARMISD::VLD1_UPD;
11712         NumVecs = 1; isLaneOp = false; break;
11713       case ISD::STORE:      NewOpc = ARMISD::VST1_UPD;
11714         NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
11715       }
11716     }
11717 
11718     // Find the size of memory referenced by the load/store.
11719     EVT VecTy;
11720     if (isLoadOp) {
11721       VecTy = N->getValueType(0);
11722     } else if (isIntrinsic) {
11723       VecTy = N->getOperand(AddrOpIdx+1).getValueType();
11724     } else {
11725       assert(isStore && "Node has to be a load, a store, or an intrinsic!");
11726       VecTy = N->getOperand(1).getValueType();
11727     }
11728 
11729     unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
11730     if (isLaneOp)
11731       NumBytes /= VecTy.getVectorNumElements();
11732 
11733     // If the increment is a constant, it must match the memory ref size.
11734     SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
11735     ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode());
11736     if (NumBytes >= 3 * 16 && (!CInc || CInc->getZExtValue() != NumBytes)) {
11737       // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
11738       // separate instructions that make it harder to use a non-constant update.
11739       continue;
11740     }
11741 
11742     // OK, we found an ADD we can fold into the base update.
11743     // Now, create a _UPD node, taking care of not breaking alignment.
11744 
11745     EVT AlignedVecTy = VecTy;
11746     unsigned Alignment = MemN->getAlignment();
11747 
11748     // If this is a less-than-standard-aligned load/store, change the type to
11749     // match the standard alignment.
11750     // The alignment is overlooked when selecting _UPD variants; and it's
11751     // easier to introduce bitcasts here than fix that.
11752     // There are 3 ways to get to this base-update combine:
11753     // - intrinsics: they are assumed to be properly aligned (to the standard
11754     //   alignment of the memory type), so we don't need to do anything.
11755     // - ARMISD::VLDx nodes: they are only generated from the aforementioned
11756     //   intrinsics, so, likewise, there's nothing to do.
11757     // - generic load/store instructions: the alignment is specified as an
11758     //   explicit operand, rather than implicitly as the standard alignment
11759     //   of the memory type (like the intrisics).  We need to change the
11760     //   memory type to match the explicit alignment.  That way, we don't
11761     //   generate non-standard-aligned ARMISD::VLDx nodes.
11762     if (isa<LSBaseSDNode>(N)) {
11763       if (Alignment == 0)
11764         Alignment = 1;
11765       if (Alignment < VecTy.getScalarSizeInBits() / 8) {
11766         MVT EltTy = MVT::getIntegerVT(Alignment * 8);
11767         assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
11768         assert(!isLaneOp && "Unexpected generic load/store lane.");
11769         unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
11770         AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
11771       }
11772       // Don't set an explicit alignment on regular load/stores that we want
11773       // to transform to VLD/VST 1_UPD nodes.
11774       // This matches the behavior of regular load/stores, which only get an
11775       // explicit alignment if the MMO alignment is larger than the standard
11776       // alignment of the memory type.
11777       // Intrinsics, however, always get an explicit alignment, set to the
11778       // alignment of the MMO.
11779       Alignment = 1;
11780     }
11781 
11782     // Create the new updating load/store node.
11783     // First, create an SDVTList for the new updating node's results.
11784     EVT Tys[6];
11785     unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
11786     unsigned n;
11787     for (n = 0; n < NumResultVecs; ++n)
11788       Tys[n] = AlignedVecTy;
11789     Tys[n++] = MVT::i32;
11790     Tys[n] = MVT::Other;
11791     SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
11792 
11793     // Then, gather the new node's operands.
11794     SmallVector<SDValue, 8> Ops;
11795     Ops.push_back(N->getOperand(0)); // incoming chain
11796     Ops.push_back(N->getOperand(AddrOpIdx));
11797     Ops.push_back(Inc);
11798 
11799     if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
11800       // Try to match the intrinsic's signature
11801       Ops.push_back(StN->getValue());
11802     } else {
11803       // Loads (and of course intrinsics) match the intrinsics' signature,
11804       // so just add all but the alignment operand.
11805       for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
11806         Ops.push_back(N->getOperand(i));
11807     }
11808 
11809     // For all node types, the alignment operand is always the last one.
11810     Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
11811 
11812     // If this is a non-standard-aligned STORE, the penultimate operand is the
11813     // stored value.  Bitcast it to the aligned type.
11814     if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
11815       SDValue &StVal = Ops[Ops.size()-2];
11816       StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
11817     }
11818 
11819     EVT LoadVT = isLaneOp ? VecTy.getVectorElementType() : AlignedVecTy;
11820     SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys, Ops, LoadVT,
11821                                            MemN->getMemOperand());
11822 
11823     // Update the uses.
11824     SmallVector<SDValue, 5> NewResults;
11825     for (unsigned i = 0; i < NumResultVecs; ++i)
11826       NewResults.push_back(SDValue(UpdN.getNode(), i));
11827 
11828     // If this is an non-standard-aligned LOAD, the first result is the loaded
11829     // value.  Bitcast it to the expected result type.
11830     if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
11831       SDValue &LdVal = NewResults[0];
11832       LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
11833     }
11834 
11835     NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
11836     DCI.CombineTo(N, NewResults);
11837     DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
11838 
11839     break;
11840   }
11841   return SDValue();
11842 }
11843 
11844 static SDValue PerformVLDCombine(SDNode *N,
11845                                  TargetLowering::DAGCombinerInfo &DCI) {
11846   if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
11847     return SDValue();
11848 
11849   return CombineBaseUpdate(N, DCI);
11850 }
11851 
11852 /// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
11853 /// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
11854 /// are also VDUPLANEs.  If so, combine them to a vldN-dup operation and
11855 /// return true.
11856 static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
11857   SelectionDAG &DAG = DCI.DAG;
11858   EVT VT = N->getValueType(0);
11859   // vldN-dup instructions only support 64-bit vectors for N > 1.
11860   if (!VT.is64BitVector())
11861     return false;
11862 
11863   // Check if the VDUPLANE operand is a vldN-dup intrinsic.
11864   SDNode *VLD = N->getOperand(0).getNode();
11865   if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
11866     return false;
11867   unsigned NumVecs = 0;
11868   unsigned NewOpc = 0;
11869   unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
11870   if (IntNo == Intrinsic::arm_neon_vld2lane) {
11871     NumVecs = 2;
11872     NewOpc = ARMISD::VLD2DUP;
11873   } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
11874     NumVecs = 3;
11875     NewOpc = ARMISD::VLD3DUP;
11876   } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
11877     NumVecs = 4;
11878     NewOpc = ARMISD::VLD4DUP;
11879   } else {
11880     return false;
11881   }
11882 
11883   // First check that all the vldN-lane uses are VDUPLANEs and that the lane
11884   // numbers match the load.
11885   unsigned VLDLaneNo =
11886     cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
11887   for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
11888        UI != UE; ++UI) {
11889     // Ignore uses of the chain result.
11890     if (UI.getUse().getResNo() == NumVecs)
11891       continue;
11892     SDNode *User = *UI;
11893     if (User->getOpcode() != ARMISD::VDUPLANE ||
11894         VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
11895       return false;
11896   }
11897 
11898   // Create the vldN-dup node.
11899   EVT Tys[5];
11900   unsigned n;
11901   for (n = 0; n < NumVecs; ++n)
11902     Tys[n] = VT;
11903   Tys[n] = MVT::Other;
11904   SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
11905   SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
11906   MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
11907   SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
11908                                            Ops, VLDMemInt->getMemoryVT(),
11909                                            VLDMemInt->getMemOperand());
11910 
11911   // Update the uses.
11912   for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
11913        UI != UE; ++UI) {
11914     unsigned ResNo = UI.getUse().getResNo();
11915     // Ignore uses of the chain result.
11916     if (ResNo == NumVecs)
11917       continue;
11918     SDNode *User = *UI;
11919     DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
11920   }
11921 
11922   // Now the vldN-lane intrinsic is dead except for its chain result.
11923   // Update uses of the chain.
11924   std::vector<SDValue> VLDDupResults;
11925   for (unsigned n = 0; n < NumVecs; ++n)
11926     VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
11927   VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
11928   DCI.CombineTo(VLD, VLDDupResults);
11929 
11930   return true;
11931 }
11932 
11933 /// PerformVDUPLANECombine - Target-specific dag combine xforms for
11934 /// ARMISD::VDUPLANE.
11935 static SDValue PerformVDUPLANECombine(SDNode *N,
11936                                       TargetLowering::DAGCombinerInfo &DCI) {
11937   SDValue Op = N->getOperand(0);
11938 
11939   // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
11940   // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
11941   if (CombineVLDDUP(N, DCI))
11942     return SDValue(N, 0);
11943 
11944   // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
11945   // redundant.  Ignore bit_converts for now; element sizes are checked below.
11946   while (Op.getOpcode() == ISD::BITCAST)
11947     Op = Op.getOperand(0);
11948   if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
11949     return SDValue();
11950 
11951   // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
11952   unsigned EltSize = Op.getScalarValueSizeInBits();
11953   // The canonical VMOV for a zero vector uses a 32-bit element size.
11954   unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
11955   unsigned EltBits;
11956   if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
11957     EltSize = 8;
11958   EVT VT = N->getValueType(0);
11959   if (EltSize > VT.getScalarSizeInBits())
11960     return SDValue();
11961 
11962   return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
11963 }
11964 
11965 /// PerformVDUPCombine - Target-specific dag combine xforms for ARMISD::VDUP.
11966 static SDValue PerformVDUPCombine(SDNode *N,
11967                                   TargetLowering::DAGCombinerInfo &DCI) {
11968   SelectionDAG &DAG = DCI.DAG;
11969   SDValue Op = N->getOperand(0);
11970 
11971   // Match VDUP(LOAD) -> VLD1DUP.
11972   // We match this pattern here rather than waiting for isel because the
11973   // transform is only legal for unindexed loads.
11974   LoadSDNode *LD = dyn_cast<LoadSDNode>(Op.getNode());
11975   if (LD && Op.hasOneUse() && LD->isUnindexed() &&
11976       LD->getMemoryVT() == N->getValueType(0).getVectorElementType()) {
11977     SDValue Ops[] = { LD->getOperand(0), LD->getOperand(1),
11978                       DAG.getConstant(LD->getAlignment(), SDLoc(N), MVT::i32) };
11979     SDVTList SDTys = DAG.getVTList(N->getValueType(0), MVT::Other);
11980     SDValue VLDDup = DAG.getMemIntrinsicNode(ARMISD::VLD1DUP, SDLoc(N), SDTys,
11981                                              Ops, LD->getMemoryVT(),
11982                                              LD->getMemOperand());
11983     DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), VLDDup.getValue(1));
11984     return VLDDup;
11985   }
11986 
11987   return SDValue();
11988 }
11989 
11990 static SDValue PerformLOADCombine(SDNode *N,
11991                                   TargetLowering::DAGCombinerInfo &DCI) {
11992   EVT VT = N->getValueType(0);
11993 
11994   // If this is a legal vector load, try to combine it into a VLD1_UPD.
11995   if (ISD::isNormalLoad(N) && VT.isVector() &&
11996       DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
11997     return CombineBaseUpdate(N, DCI);
11998 
11999   return SDValue();
12000 }
12001 
12002 /// PerformSTORECombine - Target-specific dag combine xforms for
12003 /// ISD::STORE.
12004 static SDValue PerformSTORECombine(SDNode *N,
12005                                    TargetLowering::DAGCombinerInfo &DCI) {
12006   StoreSDNode *St = cast<StoreSDNode>(N);
12007   if (St->isVolatile())
12008     return SDValue();
12009 
12010   // Optimize trunc store (of multiple scalars) to shuffle and store.  First,
12011   // pack all of the elements in one place.  Next, store to memory in fewer
12012   // chunks.
12013   SDValue StVal = St->getValue();
12014   EVT VT = StVal.getValueType();
12015   if (St->isTruncatingStore() && VT.isVector()) {
12016     SelectionDAG &DAG = DCI.DAG;
12017     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12018     EVT StVT = St->getMemoryVT();
12019     unsigned NumElems = VT.getVectorNumElements();
12020     assert(StVT != VT && "Cannot truncate to the same type");
12021     unsigned FromEltSz = VT.getScalarSizeInBits();
12022     unsigned ToEltSz = StVT.getScalarSizeInBits();
12023 
12024     // From, To sizes and ElemCount must be pow of two
12025     if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
12026 
12027     // We are going to use the original vector elt for storing.
12028     // Accumulated smaller vector elements must be a multiple of the store size.
12029     if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
12030 
12031     unsigned SizeRatio  = FromEltSz / ToEltSz;
12032     assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
12033 
12034     // Create a type on which we perform the shuffle.
12035     EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
12036                                      NumElems*SizeRatio);
12037     assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
12038 
12039     SDLoc DL(St);
12040     SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
12041     SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
12042     for (unsigned i = 0; i < NumElems; ++i)
12043       ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
12044                           ? (i + 1) * SizeRatio - 1
12045                           : i * SizeRatio;
12046 
12047     // Can't shuffle using an illegal type.
12048     if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
12049 
12050     SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
12051                                 DAG.getUNDEF(WideVec.getValueType()),
12052                                 ShuffleVec);
12053     // At this point all of the data is stored at the bottom of the
12054     // register. We now need to save it to mem.
12055 
12056     // Find the largest store unit
12057     MVT StoreType = MVT::i8;
12058     for (MVT Tp : MVT::integer_valuetypes()) {
12059       if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
12060         StoreType = Tp;
12061     }
12062     // Didn't find a legal store type.
12063     if (!TLI.isTypeLegal(StoreType))
12064       return SDValue();
12065 
12066     // Bitcast the original vector into a vector of store-size units
12067     EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
12068             StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
12069     assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
12070     SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
12071     SmallVector<SDValue, 8> Chains;
12072     SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
12073                                         TLI.getPointerTy(DAG.getDataLayout()));
12074     SDValue BasePtr = St->getBasePtr();
12075 
12076     // Perform one or more big stores into memory.
12077     unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
12078     for (unsigned I = 0; I < E; I++) {
12079       SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
12080                                    StoreType, ShuffWide,
12081                                    DAG.getIntPtrConstant(I, DL));
12082       SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
12083                                 St->getPointerInfo(), St->getAlignment(),
12084                                 St->getMemOperand()->getFlags());
12085       BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
12086                             Increment);
12087       Chains.push_back(Ch);
12088     }
12089     return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
12090   }
12091 
12092   if (!ISD::isNormalStore(St))
12093     return SDValue();
12094 
12095   // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
12096   // ARM stores of arguments in the same cache line.
12097   if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
12098       StVal.getNode()->hasOneUse()) {
12099     SelectionDAG  &DAG = DCI.DAG;
12100     bool isBigEndian = DAG.getDataLayout().isBigEndian();
12101     SDLoc DL(St);
12102     SDValue BasePtr = St->getBasePtr();
12103     SDValue NewST1 = DAG.getStore(
12104         St->getChain(), DL, StVal.getNode()->getOperand(isBigEndian ? 1 : 0),
12105         BasePtr, St->getPointerInfo(), St->getAlignment(),
12106         St->getMemOperand()->getFlags());
12107 
12108     SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
12109                                     DAG.getConstant(4, DL, MVT::i32));
12110     return DAG.getStore(NewST1.getValue(0), DL,
12111                         StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
12112                         OffsetPtr, St->getPointerInfo(),
12113                         std::min(4U, St->getAlignment() / 2),
12114                         St->getMemOperand()->getFlags());
12115   }
12116 
12117   if (StVal.getValueType() == MVT::i64 &&
12118       StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
12119 
12120     // Bitcast an i64 store extracted from a vector to f64.
12121     // Otherwise, the i64 value will be legalized to a pair of i32 values.
12122     SelectionDAG &DAG = DCI.DAG;
12123     SDLoc dl(StVal);
12124     SDValue IntVec = StVal.getOperand(0);
12125     EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
12126                                    IntVec.getValueType().getVectorNumElements());
12127     SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
12128     SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
12129                                  Vec, StVal.getOperand(1));
12130     dl = SDLoc(N);
12131     SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
12132     // Make the DAGCombiner fold the bitcasts.
12133     DCI.AddToWorklist(Vec.getNode());
12134     DCI.AddToWorklist(ExtElt.getNode());
12135     DCI.AddToWorklist(V.getNode());
12136     return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
12137                         St->getPointerInfo(), St->getAlignment(),
12138                         St->getMemOperand()->getFlags(), St->getAAInfo());
12139   }
12140 
12141   // If this is a legal vector store, try to combine it into a VST1_UPD.
12142   if (ISD::isNormalStore(N) && VT.isVector() &&
12143       DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
12144     return CombineBaseUpdate(N, DCI);
12145 
12146   return SDValue();
12147 }
12148 
12149 /// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
12150 /// can replace combinations of VMUL and VCVT (floating-point to integer)
12151 /// when the VMUL has a constant operand that is a power of 2.
12152 ///
12153 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
12154 ///  vmul.f32        d16, d17, d16
12155 ///  vcvt.s32.f32    d16, d16
12156 /// becomes:
12157 ///  vcvt.s32.f32    d16, d16, #3
12158 static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
12159                                   const ARMSubtarget *Subtarget) {
12160   if (!Subtarget->hasNEON())
12161     return SDValue();
12162 
12163   SDValue Op = N->getOperand(0);
12164   if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() ||
12165       Op.getOpcode() != ISD::FMUL)
12166     return SDValue();
12167 
12168   SDValue ConstVec = Op->getOperand(1);
12169   if (!isa<BuildVectorSDNode>(ConstVec))
12170     return SDValue();
12171 
12172   MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
12173   uint32_t FloatBits = FloatTy.getSizeInBits();
12174   MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
12175   uint32_t IntBits = IntTy.getSizeInBits();
12176   unsigned NumLanes = Op.getValueType().getVectorNumElements();
12177   if (FloatBits != 32 || IntBits > 32 || (NumLanes != 4 && NumLanes != 2)) {
12178     // These instructions only exist converting from f32 to i32. We can handle
12179     // smaller integers by generating an extra truncate, but larger ones would
12180     // be lossy. We also can't handle anything other than 2 or 4 lanes, since
12181     // these intructions only support v2i32/v4i32 types.
12182     return SDValue();
12183   }
12184 
12185   BitVector UndefElements;
12186   BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
12187   int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
12188   if (C == -1 || C == 0 || C > 32)
12189     return SDValue();
12190 
12191   SDLoc dl(N);
12192   bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
12193   unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
12194     Intrinsic::arm_neon_vcvtfp2fxu;
12195   SDValue FixConv = DAG.getNode(
12196       ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
12197       DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
12198       DAG.getConstant(C, dl, MVT::i32));
12199 
12200   if (IntBits < FloatBits)
12201     FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
12202 
12203   return FixConv;
12204 }
12205 
12206 /// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
12207 /// can replace combinations of VCVT (integer to floating-point) and VDIV
12208 /// when the VDIV has a constant operand that is a power of 2.
12209 ///
12210 /// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
12211 ///  vcvt.f32.s32    d16, d16
12212 ///  vdiv.f32        d16, d17, d16
12213 /// becomes:
12214 ///  vcvt.f32.s32    d16, d16, #3
12215 static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
12216                                   const ARMSubtarget *Subtarget) {
12217   if (!Subtarget->hasNEON())
12218     return SDValue();
12219 
12220   SDValue Op = N->getOperand(0);
12221   unsigned OpOpcode = Op.getNode()->getOpcode();
12222   if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() ||
12223       (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
12224     return SDValue();
12225 
12226   SDValue ConstVec = N->getOperand(1);
12227   if (!isa<BuildVectorSDNode>(ConstVec))
12228     return SDValue();
12229 
12230   MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
12231   uint32_t FloatBits = FloatTy.getSizeInBits();
12232   MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
12233   uint32_t IntBits = IntTy.getSizeInBits();
12234   unsigned NumLanes = Op.getValueType().getVectorNumElements();
12235   if (FloatBits != 32 || IntBits > 32 || (NumLanes != 4 && NumLanes != 2)) {
12236     // These instructions only exist converting from i32 to f32. We can handle
12237     // smaller integers by generating an extra extend, but larger ones would
12238     // be lossy. We also can't handle anything other than 2 or 4 lanes, since
12239     // these intructions only support v2i32/v4i32 types.
12240     return SDValue();
12241   }
12242 
12243   BitVector UndefElements;
12244   BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
12245   int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
12246   if (C == -1 || C == 0 || C > 32)
12247     return SDValue();
12248 
12249   SDLoc dl(N);
12250   bool isSigned = OpOpcode == ISD::SINT_TO_FP;
12251   SDValue ConvInput = Op.getOperand(0);
12252   if (IntBits < FloatBits)
12253     ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
12254                             dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
12255                             ConvInput);
12256 
12257   unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
12258     Intrinsic::arm_neon_vcvtfxu2fp;
12259   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
12260                      Op.getValueType(),
12261                      DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
12262                      ConvInput, DAG.getConstant(C, dl, MVT::i32));
12263 }
12264 
12265 /// Getvshiftimm - Check if this is a valid build_vector for the immediate
12266 /// operand of a vector shift operation, where all the elements of the
12267 /// build_vector must have the same constant integer value.
12268 static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
12269   // Ignore bit_converts.
12270   while (Op.getOpcode() == ISD::BITCAST)
12271     Op = Op.getOperand(0);
12272   BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
12273   APInt SplatBits, SplatUndef;
12274   unsigned SplatBitSize;
12275   bool HasAnyUndefs;
12276   if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
12277                                       HasAnyUndefs, ElementBits) ||
12278       SplatBitSize > ElementBits)
12279     return false;
12280   Cnt = SplatBits.getSExtValue();
12281   return true;
12282 }
12283 
12284 /// isVShiftLImm - Check if this is a valid build_vector for the immediate
12285 /// operand of a vector shift left operation.  That value must be in the range:
12286 ///   0 <= Value < ElementBits for a left shift; or
12287 ///   0 <= Value <= ElementBits for a long left shift.
12288 static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
12289   assert(VT.isVector() && "vector shift count is not a vector type");
12290   int64_t ElementBits = VT.getScalarSizeInBits();
12291   if (! getVShiftImm(Op, ElementBits, Cnt))
12292     return false;
12293   return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
12294 }
12295 
12296 /// isVShiftRImm - Check if this is a valid build_vector for the immediate
12297 /// operand of a vector shift right operation.  For a shift opcode, the value
12298 /// is positive, but for an intrinsic the value count must be negative. The
12299 /// absolute value must be in the range:
12300 ///   1 <= |Value| <= ElementBits for a right shift; or
12301 ///   1 <= |Value| <= ElementBits/2 for a narrow right shift.
12302 static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
12303                          int64_t &Cnt) {
12304   assert(VT.isVector() && "vector shift count is not a vector type");
12305   int64_t ElementBits = VT.getScalarSizeInBits();
12306   if (! getVShiftImm(Op, ElementBits, Cnt))
12307     return false;
12308   if (!isIntrinsic)
12309     return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
12310   if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
12311     Cnt = -Cnt;
12312     return true;
12313   }
12314   return false;
12315 }
12316 
12317 /// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
12318 static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
12319   unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
12320   switch (IntNo) {
12321   default:
12322     // Don't do anything for most intrinsics.
12323     break;
12324 
12325   // Vector shifts: check for immediate versions and lower them.
12326   // Note: This is done during DAG combining instead of DAG legalizing because
12327   // the build_vectors for 64-bit vector element shift counts are generally
12328   // not legal, and it is hard to see their values after they get legalized to
12329   // loads from a constant pool.
12330   case Intrinsic::arm_neon_vshifts:
12331   case Intrinsic::arm_neon_vshiftu:
12332   case Intrinsic::arm_neon_vrshifts:
12333   case Intrinsic::arm_neon_vrshiftu:
12334   case Intrinsic::arm_neon_vrshiftn:
12335   case Intrinsic::arm_neon_vqshifts:
12336   case Intrinsic::arm_neon_vqshiftu:
12337   case Intrinsic::arm_neon_vqshiftsu:
12338   case Intrinsic::arm_neon_vqshiftns:
12339   case Intrinsic::arm_neon_vqshiftnu:
12340   case Intrinsic::arm_neon_vqshiftnsu:
12341   case Intrinsic::arm_neon_vqrshiftns:
12342   case Intrinsic::arm_neon_vqrshiftnu:
12343   case Intrinsic::arm_neon_vqrshiftnsu: {
12344     EVT VT = N->getOperand(1).getValueType();
12345     int64_t Cnt;
12346     unsigned VShiftOpc = 0;
12347 
12348     switch (IntNo) {
12349     case Intrinsic::arm_neon_vshifts:
12350     case Intrinsic::arm_neon_vshiftu:
12351       if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
12352         VShiftOpc = ARMISD::VSHL;
12353         break;
12354       }
12355       if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
12356         VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
12357                      ARMISD::VSHRs : ARMISD::VSHRu);
12358         break;
12359       }
12360       return SDValue();
12361 
12362     case Intrinsic::arm_neon_vrshifts:
12363     case Intrinsic::arm_neon_vrshiftu:
12364       if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
12365         break;
12366       return SDValue();
12367 
12368     case Intrinsic::arm_neon_vqshifts:
12369     case Intrinsic::arm_neon_vqshiftu:
12370       if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
12371         break;
12372       return SDValue();
12373 
12374     case Intrinsic::arm_neon_vqshiftsu:
12375       if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
12376         break;
12377       llvm_unreachable("invalid shift count for vqshlu intrinsic");
12378 
12379     case Intrinsic::arm_neon_vrshiftn:
12380     case Intrinsic::arm_neon_vqshiftns:
12381     case Intrinsic::arm_neon_vqshiftnu:
12382     case Intrinsic::arm_neon_vqshiftnsu:
12383     case Intrinsic::arm_neon_vqrshiftns:
12384     case Intrinsic::arm_neon_vqrshiftnu:
12385     case Intrinsic::arm_neon_vqrshiftnsu:
12386       // Narrowing shifts require an immediate right shift.
12387       if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
12388         break;
12389       llvm_unreachable("invalid shift count for narrowing vector shift "
12390                        "intrinsic");
12391 
12392     default:
12393       llvm_unreachable("unhandled vector shift");
12394     }
12395 
12396     switch (IntNo) {
12397     case Intrinsic::arm_neon_vshifts:
12398     case Intrinsic::arm_neon_vshiftu:
12399       // Opcode already set above.
12400       break;
12401     case Intrinsic::arm_neon_vrshifts:
12402       VShiftOpc = ARMISD::VRSHRs; break;
12403     case Intrinsic::arm_neon_vrshiftu:
12404       VShiftOpc = ARMISD::VRSHRu; break;
12405     case Intrinsic::arm_neon_vrshiftn:
12406       VShiftOpc = ARMISD::VRSHRN; break;
12407     case Intrinsic::arm_neon_vqshifts:
12408       VShiftOpc = ARMISD::VQSHLs; break;
12409     case Intrinsic::arm_neon_vqshiftu:
12410       VShiftOpc = ARMISD::VQSHLu; break;
12411     case Intrinsic::arm_neon_vqshiftsu:
12412       VShiftOpc = ARMISD::VQSHLsu; break;
12413     case Intrinsic::arm_neon_vqshiftns:
12414       VShiftOpc = ARMISD::VQSHRNs; break;
12415     case Intrinsic::arm_neon_vqshiftnu:
12416       VShiftOpc = ARMISD::VQSHRNu; break;
12417     case Intrinsic::arm_neon_vqshiftnsu:
12418       VShiftOpc = ARMISD::VQSHRNsu; break;
12419     case Intrinsic::arm_neon_vqrshiftns:
12420       VShiftOpc = ARMISD::VQRSHRNs; break;
12421     case Intrinsic::arm_neon_vqrshiftnu:
12422       VShiftOpc = ARMISD::VQRSHRNu; break;
12423     case Intrinsic::arm_neon_vqrshiftnsu:
12424       VShiftOpc = ARMISD::VQRSHRNsu; break;
12425     }
12426 
12427     SDLoc dl(N);
12428     return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
12429                        N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
12430   }
12431 
12432   case Intrinsic::arm_neon_vshiftins: {
12433     EVT VT = N->getOperand(1).getValueType();
12434     int64_t Cnt;
12435     unsigned VShiftOpc = 0;
12436 
12437     if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
12438       VShiftOpc = ARMISD::VSLI;
12439     else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
12440       VShiftOpc = ARMISD::VSRI;
12441     else {
12442       llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
12443     }
12444 
12445     SDLoc dl(N);
12446     return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
12447                        N->getOperand(1), N->getOperand(2),
12448                        DAG.getConstant(Cnt, dl, MVT::i32));
12449   }
12450 
12451   case Intrinsic::arm_neon_vqrshifts:
12452   case Intrinsic::arm_neon_vqrshiftu:
12453     // No immediate versions of these to check for.
12454     break;
12455   }
12456 
12457   return SDValue();
12458 }
12459 
12460 /// PerformShiftCombine - Checks for immediate versions of vector shifts and
12461 /// lowers them.  As with the vector shift intrinsics, this is done during DAG
12462 /// combining instead of DAG legalizing because the build_vectors for 64-bit
12463 /// vector element shift counts are generally not legal, and it is hard to see
12464 /// their values after they get legalized to loads from a constant pool.
12465 static SDValue PerformShiftCombine(SDNode *N,
12466                                    TargetLowering::DAGCombinerInfo &DCI,
12467                                    const ARMSubtarget *ST) {
12468   SelectionDAG &DAG = DCI.DAG;
12469   EVT VT = N->getValueType(0);
12470   if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
12471     // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
12472     // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
12473     SDValue N1 = N->getOperand(1);
12474     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
12475       SDValue N0 = N->getOperand(0);
12476       if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
12477           DAG.MaskedValueIsZero(N0.getOperand(0),
12478                                 APInt::getHighBitsSet(32, 16)))
12479         return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
12480     }
12481   }
12482 
12483   if (ST->isThumb1Only() && N->getOpcode() == ISD::SHL && VT == MVT::i32 &&
12484       N->getOperand(0)->getOpcode() == ISD::AND &&
12485       N->getOperand(0)->hasOneUse()) {
12486     if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
12487       return SDValue();
12488     // Look for the pattern (shl (and x, AndMask), ShiftAmt). This doesn't
12489     // usually show up because instcombine prefers to canonicalize it to
12490     // (and (shl x, ShiftAmt) (shl AndMask, ShiftAmt)), but the shift can come
12491     // out of GEP lowering in some cases.
12492     SDValue N0 = N->getOperand(0);
12493     ConstantSDNode *ShiftAmtNode = dyn_cast<ConstantSDNode>(N->getOperand(1));
12494     if (!ShiftAmtNode)
12495       return SDValue();
12496     uint32_t ShiftAmt = static_cast<uint32_t>(ShiftAmtNode->getZExtValue());
12497     ConstantSDNode *AndMaskNode = dyn_cast<ConstantSDNode>(N0->getOperand(1));
12498     if (!AndMaskNode)
12499       return SDValue();
12500     uint32_t AndMask = static_cast<uint32_t>(AndMaskNode->getZExtValue());
12501     // Don't transform uxtb/uxth.
12502     if (AndMask == 255 || AndMask == 65535)
12503       return SDValue();
12504     if (isMask_32(AndMask)) {
12505       uint32_t MaskedBits = countLeadingZeros(AndMask);
12506       if (MaskedBits > ShiftAmt) {
12507         SDLoc DL(N);
12508         SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, N0->getOperand(0),
12509                                   DAG.getConstant(MaskedBits, DL, MVT::i32));
12510         return DAG.getNode(
12511             ISD::SRL, DL, MVT::i32, SHL,
12512             DAG.getConstant(MaskedBits - ShiftAmt, DL, MVT::i32));
12513       }
12514     }
12515   }
12516 
12517   // Nothing to be done for scalar shifts.
12518   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12519   if (!VT.isVector() || !TLI.isTypeLegal(VT))
12520     return SDValue();
12521 
12522   assert(ST->hasNEON() && "unexpected vector shift");
12523   int64_t Cnt;
12524 
12525   switch (N->getOpcode()) {
12526   default: llvm_unreachable("unexpected shift opcode");
12527 
12528   case ISD::SHL:
12529     if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
12530       SDLoc dl(N);
12531       return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
12532                          DAG.getConstant(Cnt, dl, MVT::i32));
12533     }
12534     break;
12535 
12536   case ISD::SRA:
12537   case ISD::SRL:
12538     if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
12539       unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
12540                             ARMISD::VSHRs : ARMISD::VSHRu);
12541       SDLoc dl(N);
12542       return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
12543                          DAG.getConstant(Cnt, dl, MVT::i32));
12544     }
12545   }
12546   return SDValue();
12547 }
12548 
12549 /// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
12550 /// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
12551 static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
12552                                     const ARMSubtarget *ST) {
12553   SDValue N0 = N->getOperand(0);
12554 
12555   // Check for sign- and zero-extensions of vector extract operations of 8-
12556   // and 16-bit vector elements.  NEON supports these directly.  They are
12557   // handled during DAG combining because type legalization will promote them
12558   // to 32-bit types and it is messy to recognize the operations after that.
12559   if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
12560     SDValue Vec = N0.getOperand(0);
12561     SDValue Lane = N0.getOperand(1);
12562     EVT VT = N->getValueType(0);
12563     EVT EltVT = N0.getValueType();
12564     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12565 
12566     if (VT == MVT::i32 &&
12567         (EltVT == MVT::i8 || EltVT == MVT::i16) &&
12568         TLI.isTypeLegal(Vec.getValueType()) &&
12569         isa<ConstantSDNode>(Lane)) {
12570 
12571       unsigned Opc = 0;
12572       switch (N->getOpcode()) {
12573       default: llvm_unreachable("unexpected opcode");
12574       case ISD::SIGN_EXTEND:
12575         Opc = ARMISD::VGETLANEs;
12576         break;
12577       case ISD::ZERO_EXTEND:
12578       case ISD::ANY_EXTEND:
12579         Opc = ARMISD::VGETLANEu;
12580         break;
12581       }
12582       return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
12583     }
12584   }
12585 
12586   return SDValue();
12587 }
12588 
12589 static const APInt *isPowerOf2Constant(SDValue V) {
12590   ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
12591   if (!C)
12592     return nullptr;
12593   const APInt *CV = &C->getAPIntValue();
12594   return CV->isPowerOf2() ? CV : nullptr;
12595 }
12596 
12597 SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const {
12598   // If we have a CMOV, OR and AND combination such as:
12599   //   if (x & CN)
12600   //     y |= CM;
12601   //
12602   // And:
12603   //   * CN is a single bit;
12604   //   * All bits covered by CM are known zero in y
12605   //
12606   // Then we can convert this into a sequence of BFI instructions. This will
12607   // always be a win if CM is a single bit, will always be no worse than the
12608   // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is
12609   // three bits (due to the extra IT instruction).
12610 
12611   SDValue Op0 = CMOV->getOperand(0);
12612   SDValue Op1 = CMOV->getOperand(1);
12613   auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2));
12614   auto CC = CCNode->getAPIntValue().getLimitedValue();
12615   SDValue CmpZ = CMOV->getOperand(4);
12616 
12617   // The compare must be against zero.
12618   if (!isNullConstant(CmpZ->getOperand(1)))
12619     return SDValue();
12620 
12621   assert(CmpZ->getOpcode() == ARMISD::CMPZ);
12622   SDValue And = CmpZ->getOperand(0);
12623   if (And->getOpcode() != ISD::AND)
12624     return SDValue();
12625   const APInt *AndC = isPowerOf2Constant(And->getOperand(1));
12626   if (!AndC)
12627     return SDValue();
12628   SDValue X = And->getOperand(0);
12629 
12630   if (CC == ARMCC::EQ) {
12631     // We're performing an "equal to zero" compare. Swap the operands so we
12632     // canonicalize on a "not equal to zero" compare.
12633     std::swap(Op0, Op1);
12634   } else {
12635     assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?");
12636   }
12637 
12638   if (Op1->getOpcode() != ISD::OR)
12639     return SDValue();
12640 
12641   ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1));
12642   if (!OrC)
12643     return SDValue();
12644   SDValue Y = Op1->getOperand(0);
12645 
12646   if (Op0 != Y)
12647     return SDValue();
12648 
12649   // Now, is it profitable to continue?
12650   APInt OrCI = OrC->getAPIntValue();
12651   unsigned Heuristic = Subtarget->isThumb() ? 3 : 2;
12652   if (OrCI.countPopulation() > Heuristic)
12653     return SDValue();
12654 
12655   // Lastly, can we determine that the bits defined by OrCI
12656   // are zero in Y?
12657   KnownBits Known = DAG.computeKnownBits(Y);
12658   if ((OrCI & Known.Zero) != OrCI)
12659     return SDValue();
12660 
12661   // OK, we can do the combine.
12662   SDValue V = Y;
12663   SDLoc dl(X);
12664   EVT VT = X.getValueType();
12665   unsigned BitInX = AndC->logBase2();
12666 
12667   if (BitInX != 0) {
12668     // We must shift X first.
12669     X = DAG.getNode(ISD::SRL, dl, VT, X,
12670                     DAG.getConstant(BitInX, dl, VT));
12671   }
12672 
12673   for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits();
12674        BitInY < NumActiveBits; ++BitInY) {
12675     if (OrCI[BitInY] == 0)
12676       continue;
12677     APInt Mask(VT.getSizeInBits(), 0);
12678     Mask.setBit(BitInY);
12679     V = DAG.getNode(ARMISD::BFI, dl, VT, V, X,
12680                     // Confusingly, the operand is an *inverted* mask.
12681                     DAG.getConstant(~Mask, dl, VT));
12682   }
12683 
12684   return V;
12685 }
12686 
12687 /// PerformBRCONDCombine - Target-specific DAG combining for ARMISD::BRCOND.
12688 SDValue
12689 ARMTargetLowering::PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const {
12690   SDValue Cmp = N->getOperand(4);
12691   if (Cmp.getOpcode() != ARMISD::CMPZ)
12692     // Only looking at NE cases.
12693     return SDValue();
12694 
12695   EVT VT = N->getValueType(0);
12696   SDLoc dl(N);
12697   SDValue LHS = Cmp.getOperand(0);
12698   SDValue RHS = Cmp.getOperand(1);
12699   SDValue Chain = N->getOperand(0);
12700   SDValue BB = N->getOperand(1);
12701   SDValue ARMcc = N->getOperand(2);
12702   ARMCC::CondCodes CC =
12703     (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
12704 
12705   // (brcond Chain BB ne CPSR (cmpz (and (cmov 0 1 CC CPSR Cmp) 1) 0))
12706   // -> (brcond Chain BB CC CPSR Cmp)
12707   if (CC == ARMCC::NE && LHS.getOpcode() == ISD::AND && LHS->hasOneUse() &&
12708       LHS->getOperand(0)->getOpcode() == ARMISD::CMOV &&
12709       LHS->getOperand(0)->hasOneUse()) {
12710     auto *LHS00C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(0));
12711     auto *LHS01C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(1));
12712     auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
12713     auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
12714     if ((LHS00C && LHS00C->getZExtValue() == 0) &&
12715         (LHS01C && LHS01C->getZExtValue() == 1) &&
12716         (LHS1C && LHS1C->getZExtValue() == 1) &&
12717         (RHSC && RHSC->getZExtValue() == 0)) {
12718       return DAG.getNode(
12719           ARMISD::BRCOND, dl, VT, Chain, BB, LHS->getOperand(0)->getOperand(2),
12720           LHS->getOperand(0)->getOperand(3), LHS->getOperand(0)->getOperand(4));
12721     }
12722   }
12723 
12724   return SDValue();
12725 }
12726 
12727 /// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
12728 SDValue
12729 ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
12730   SDValue Cmp = N->getOperand(4);
12731   if (Cmp.getOpcode() != ARMISD::CMPZ)
12732     // Only looking at EQ and NE cases.
12733     return SDValue();
12734 
12735   EVT VT = N->getValueType(0);
12736   SDLoc dl(N);
12737   SDValue LHS = Cmp.getOperand(0);
12738   SDValue RHS = Cmp.getOperand(1);
12739   SDValue FalseVal = N->getOperand(0);
12740   SDValue TrueVal = N->getOperand(1);
12741   SDValue ARMcc = N->getOperand(2);
12742   ARMCC::CondCodes CC =
12743     (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
12744 
12745   // BFI is only available on V6T2+.
12746   if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) {
12747     SDValue R = PerformCMOVToBFICombine(N, DAG);
12748     if (R)
12749       return R;
12750   }
12751 
12752   // Simplify
12753   //   mov     r1, r0
12754   //   cmp     r1, x
12755   //   mov     r0, y
12756   //   moveq   r0, x
12757   // to
12758   //   cmp     r0, x
12759   //   movne   r0, y
12760   //
12761   //   mov     r1, r0
12762   //   cmp     r1, x
12763   //   mov     r0, x
12764   //   movne   r0, y
12765   // to
12766   //   cmp     r0, x
12767   //   movne   r0, y
12768   /// FIXME: Turn this into a target neutral optimization?
12769   SDValue Res;
12770   if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
12771     Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
12772                       N->getOperand(3), Cmp);
12773   } else if (CC == ARMCC::EQ && TrueVal == RHS) {
12774     SDValue ARMcc;
12775     SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
12776     Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
12777                       N->getOperand(3), NewCmp);
12778   }
12779 
12780   // (cmov F T ne CPSR (cmpz (cmov 0 1 CC CPSR Cmp) 0))
12781   // -> (cmov F T CC CPSR Cmp)
12782   if (CC == ARMCC::NE && LHS.getOpcode() == ARMISD::CMOV && LHS->hasOneUse()) {
12783     auto *LHS0C = dyn_cast<ConstantSDNode>(LHS->getOperand(0));
12784     auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
12785     auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
12786     if ((LHS0C && LHS0C->getZExtValue() == 0) &&
12787         (LHS1C && LHS1C->getZExtValue() == 1) &&
12788         (RHSC && RHSC->getZExtValue() == 0)) {
12789       return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
12790                          LHS->getOperand(2), LHS->getOperand(3),
12791                          LHS->getOperand(4));
12792     }
12793   }
12794 
12795   if (!VT.isInteger())
12796       return SDValue();
12797 
12798   // Materialize a boolean comparison for integers so we can avoid branching.
12799   if (isNullConstant(FalseVal)) {
12800     if (CC == ARMCC::EQ && isOneConstant(TrueVal)) {
12801       if (!Subtarget->isThumb1Only() && Subtarget->hasV5TOps()) {
12802         // If x == y then x - y == 0 and ARM's CLZ will return 32, shifting it
12803         // right 5 bits will make that 32 be 1, otherwise it will be 0.
12804         // CMOV 0, 1, ==, (CMPZ x, y) -> SRL (CTLZ (SUB x, y)), 5
12805         SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, LHS, RHS);
12806         Res = DAG.getNode(ISD::SRL, dl, VT, DAG.getNode(ISD::CTLZ, dl, VT, Sub),
12807                           DAG.getConstant(5, dl, MVT::i32));
12808       } else {
12809         // CMOV 0, 1, ==, (CMPZ x, y) ->
12810         //     (ADDCARRY (SUB x, y), t:0, t:1)
12811         // where t = (SUBCARRY 0, (SUB x, y), 0)
12812         //
12813         // The SUBCARRY computes 0 - (x - y) and this will give a borrow when
12814         // x != y. In other words, a carry C == 1 when x == y, C == 0
12815         // otherwise.
12816         // The final ADDCARRY computes
12817         //     x - y + (0 - (x - y)) + C == C
12818         SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, LHS, RHS);
12819         SDVTList VTs = DAG.getVTList(VT, MVT::i32);
12820         SDValue Neg = DAG.getNode(ISD::USUBO, dl, VTs, FalseVal, Sub);
12821         // ISD::SUBCARRY returns a borrow but we want the carry here
12822         // actually.
12823         SDValue Carry =
12824             DAG.getNode(ISD::SUB, dl, MVT::i32,
12825                         DAG.getConstant(1, dl, MVT::i32), Neg.getValue(1));
12826         Res = DAG.getNode(ISD::ADDCARRY, dl, VTs, Sub, Neg, Carry);
12827       }
12828     } else if (CC == ARMCC::NE && !isNullConstant(RHS) &&
12829                (!Subtarget->isThumb1Only() || isPowerOf2Constant(TrueVal))) {
12830       // This seems pointless but will allow us to combine it further below.
12831       // CMOV 0, z, !=, (CMPZ x, y) -> CMOV (SUBS x, y), z, !=, (SUBS x, y):1
12832       SDValue Sub =
12833           DAG.getNode(ARMISD::SUBS, dl, DAG.getVTList(VT, MVT::i32), LHS, RHS);
12834       SDValue CPSRGlue = DAG.getCopyToReg(DAG.getEntryNode(), dl, ARM::CPSR,
12835                                           Sub.getValue(1), SDValue());
12836       Res = DAG.getNode(ARMISD::CMOV, dl, VT, Sub, TrueVal, ARMcc,
12837                         N->getOperand(3), CPSRGlue.getValue(1));
12838       FalseVal = Sub;
12839     }
12840   } else if (isNullConstant(TrueVal)) {
12841     if (CC == ARMCC::EQ && !isNullConstant(RHS) &&
12842         (!Subtarget->isThumb1Only() || isPowerOf2Constant(FalseVal))) {
12843       // This seems pointless but will allow us to combine it further below
12844       // Note that we change == for != as this is the dual for the case above.
12845       // CMOV z, 0, ==, (CMPZ x, y) -> CMOV (SUBS x, y), z, !=, (SUBS x, y):1
12846       SDValue Sub =
12847           DAG.getNode(ARMISD::SUBS, dl, DAG.getVTList(VT, MVT::i32), LHS, RHS);
12848       SDValue CPSRGlue = DAG.getCopyToReg(DAG.getEntryNode(), dl, ARM::CPSR,
12849                                           Sub.getValue(1), SDValue());
12850       Res = DAG.getNode(ARMISD::CMOV, dl, VT, Sub, FalseVal,
12851                         DAG.getConstant(ARMCC::NE, dl, MVT::i32),
12852                         N->getOperand(3), CPSRGlue.getValue(1));
12853       FalseVal = Sub;
12854     }
12855   }
12856 
12857   // On Thumb1, the DAG above may be further combined if z is a power of 2
12858   // (z == 2 ^ K).
12859   // CMOV (SUBS x, y), z, !=, (SUBS x, y):1 ->
12860   //       merge t3, t4
12861   // where t1 = (SUBCARRY (SUB x, y), z, 0)
12862   //       t2 = (SUBCARRY (SUB x, y), t1:0, t1:1)
12863   //       t3 = if K != 0 then (SHL t2:0, K) else t2:0
12864   //       t4 = (SUB 1, t2:1)   [ we want a carry, not a borrow ]
12865   const APInt *TrueConst;
12866   if (Subtarget->isThumb1Only() && CC == ARMCC::NE &&
12867       (FalseVal.getOpcode() == ARMISD::SUBS) &&
12868       (FalseVal.getOperand(0) == LHS) && (FalseVal.getOperand(1) == RHS) &&
12869       (TrueConst = isPowerOf2Constant(TrueVal))) {
12870     SDVTList VTs = DAG.getVTList(VT, MVT::i32);
12871     unsigned ShiftAmount = TrueConst->logBase2();
12872     if (ShiftAmount)
12873       TrueVal = DAG.getConstant(1, dl, VT);
12874     SDValue Subc = DAG.getNode(ISD::USUBO, dl, VTs, FalseVal, TrueVal);
12875     Res = DAG.getNode(ISD::SUBCARRY, dl, VTs, FalseVal, Subc, Subc.getValue(1));
12876     // Make it a carry, not a borrow.
12877     SDValue Carry = DAG.getNode(
12878         ISD::SUB, dl, VT, DAG.getConstant(1, dl, MVT::i32), Res.getValue(1));
12879     Res = DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Res, Carry);
12880 
12881     if (ShiftAmount)
12882       Res = DAG.getNode(ISD::SHL, dl, VT, Res,
12883                         DAG.getConstant(ShiftAmount, dl, MVT::i32));
12884   }
12885 
12886   if (Res.getNode()) {
12887     KnownBits Known = DAG.computeKnownBits(SDValue(N,0));
12888     // Capture demanded bits information that would be otherwise lost.
12889     if (Known.Zero == 0xfffffffe)
12890       Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
12891                         DAG.getValueType(MVT::i1));
12892     else if (Known.Zero == 0xffffff00)
12893       Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
12894                         DAG.getValueType(MVT::i8));
12895     else if (Known.Zero == 0xffff0000)
12896       Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
12897                         DAG.getValueType(MVT::i16));
12898   }
12899 
12900   return Res;
12901 }
12902 
12903 SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
12904                                              DAGCombinerInfo &DCI) const {
12905   switch (N->getOpcode()) {
12906   default: break;
12907   case ISD::ABS:        return PerformABSCombine(N, DCI, Subtarget);
12908   case ARMISD::ADDE:    return PerformADDECombine(N, DCI, Subtarget);
12909   case ARMISD::UMLAL:   return PerformUMLALCombine(N, DCI.DAG, Subtarget);
12910   case ISD::ADD:        return PerformADDCombine(N, DCI, Subtarget);
12911   case ISD::SUB:        return PerformSUBCombine(N, DCI);
12912   case ISD::MUL:        return PerformMULCombine(N, DCI, Subtarget);
12913   case ISD::OR:         return PerformORCombine(N, DCI, Subtarget);
12914   case ISD::XOR:        return PerformXORCombine(N, DCI, Subtarget);
12915   case ISD::AND:        return PerformANDCombine(N, DCI, Subtarget);
12916   case ARMISD::ADDC:
12917   case ARMISD::SUBC:    return PerformAddcSubcCombine(N, DCI, Subtarget);
12918   case ARMISD::SUBE:    return PerformAddeSubeCombine(N, DCI, Subtarget);
12919   case ARMISD::BFI:     return PerformBFICombine(N, DCI);
12920   case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
12921   case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
12922   case ISD::STORE:      return PerformSTORECombine(N, DCI);
12923   case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
12924   case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
12925   case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
12926   case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
12927   case ARMISD::VDUP: return PerformVDUPCombine(N, DCI);
12928   case ISD::FP_TO_SINT:
12929   case ISD::FP_TO_UINT:
12930     return PerformVCVTCombine(N, DCI.DAG, Subtarget);
12931   case ISD::FDIV:
12932     return PerformVDIVCombine(N, DCI.DAG, Subtarget);
12933   case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
12934   case ISD::SHL:
12935   case ISD::SRA:
12936   case ISD::SRL:
12937     return PerformShiftCombine(N, DCI, Subtarget);
12938   case ISD::SIGN_EXTEND:
12939   case ISD::ZERO_EXTEND:
12940   case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
12941   case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
12942   case ARMISD::BRCOND: return PerformBRCONDCombine(N, DCI.DAG);
12943   case ISD::LOAD:       return PerformLOADCombine(N, DCI);
12944   case ARMISD::VLD1DUP:
12945   case ARMISD::VLD2DUP:
12946   case ARMISD::VLD3DUP:
12947   case ARMISD::VLD4DUP:
12948     return PerformVLDCombine(N, DCI);
12949   case ARMISD::BUILD_VECTOR:
12950     return PerformARMBUILD_VECTORCombine(N, DCI);
12951   case ARMISD::SMULWB: {
12952     unsigned BitWidth = N->getValueType(0).getSizeInBits();
12953     APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 16);
12954     if (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI))
12955       return SDValue();
12956     break;
12957   }
12958   case ARMISD::SMULWT: {
12959     unsigned BitWidth = N->getValueType(0).getSizeInBits();
12960     APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 16);
12961     if (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI))
12962       return SDValue();
12963     break;
12964   }
12965   case ARMISD::SMLALBB: {
12966     unsigned BitWidth = N->getValueType(0).getSizeInBits();
12967     APInt DemandedMask = APInt::getLowBitsSet(BitWidth, 16);
12968     if ((SimplifyDemandedBits(N->getOperand(0), DemandedMask, DCI)) ||
12969         (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI)))
12970       return SDValue();
12971     break;
12972   }
12973   case ARMISD::SMLALBT: {
12974     unsigned LowWidth = N->getOperand(0).getValueType().getSizeInBits();
12975     APInt LowMask = APInt::getLowBitsSet(LowWidth, 16);
12976     unsigned HighWidth = N->getOperand(1).getValueType().getSizeInBits();
12977     APInt HighMask = APInt::getHighBitsSet(HighWidth, 16);
12978     if ((SimplifyDemandedBits(N->getOperand(0), LowMask, DCI)) ||
12979         (SimplifyDemandedBits(N->getOperand(1), HighMask, DCI)))
12980       return SDValue();
12981     break;
12982   }
12983   case ARMISD::SMLALTB: {
12984     unsigned HighWidth = N->getOperand(0).getValueType().getSizeInBits();
12985     APInt HighMask = APInt::getHighBitsSet(HighWidth, 16);
12986     unsigned LowWidth = N->getOperand(1).getValueType().getSizeInBits();
12987     APInt LowMask = APInt::getLowBitsSet(LowWidth, 16);
12988     if ((SimplifyDemandedBits(N->getOperand(0), HighMask, DCI)) ||
12989         (SimplifyDemandedBits(N->getOperand(1), LowMask, DCI)))
12990       return SDValue();
12991     break;
12992   }
12993   case ARMISD::SMLALTT: {
12994     unsigned BitWidth = N->getValueType(0).getSizeInBits();
12995     APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 16);
12996     if ((SimplifyDemandedBits(N->getOperand(0), DemandedMask, DCI)) ||
12997         (SimplifyDemandedBits(N->getOperand(1), DemandedMask, DCI)))
12998       return SDValue();
12999     break;
13000   }
13001   case ISD::INTRINSIC_VOID:
13002   case ISD::INTRINSIC_W_CHAIN:
13003     switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
13004     case Intrinsic::arm_neon_vld1:
13005     case Intrinsic::arm_neon_vld1x2:
13006     case Intrinsic::arm_neon_vld1x3:
13007     case Intrinsic::arm_neon_vld1x4:
13008     case Intrinsic::arm_neon_vld2:
13009     case Intrinsic::arm_neon_vld3:
13010     case Intrinsic::arm_neon_vld4:
13011     case Intrinsic::arm_neon_vld2lane:
13012     case Intrinsic::arm_neon_vld3lane:
13013     case Intrinsic::arm_neon_vld4lane:
13014     case Intrinsic::arm_neon_vld2dup:
13015     case Intrinsic::arm_neon_vld3dup:
13016     case Intrinsic::arm_neon_vld4dup:
13017     case Intrinsic::arm_neon_vst1:
13018     case Intrinsic::arm_neon_vst1x2:
13019     case Intrinsic::arm_neon_vst1x3:
13020     case Intrinsic::arm_neon_vst1x4:
13021     case Intrinsic::arm_neon_vst2:
13022     case Intrinsic::arm_neon_vst3:
13023     case Intrinsic::arm_neon_vst4:
13024     case Intrinsic::arm_neon_vst2lane:
13025     case Intrinsic::arm_neon_vst3lane:
13026     case Intrinsic::arm_neon_vst4lane:
13027       return PerformVLDCombine(N, DCI);
13028     default: break;
13029     }
13030     break;
13031   }
13032   return SDValue();
13033 }
13034 
13035 bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
13036                                                           EVT VT) const {
13037   return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
13038 }
13039 
13040 bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
13041                                                        unsigned,
13042                                                        unsigned,
13043                                                        bool *Fast) const {
13044   // Depends what it gets converted into if the type is weird.
13045   if (!VT.isSimple())
13046     return false;
13047 
13048   // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
13049   bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
13050 
13051   switch (VT.getSimpleVT().SimpleTy) {
13052   default:
13053     return false;
13054   case MVT::i8:
13055   case MVT::i16:
13056   case MVT::i32: {
13057     // Unaligned access can use (for example) LRDB, LRDH, LDR
13058     if (AllowsUnaligned) {
13059       if (Fast)
13060         *Fast = Subtarget->hasV7Ops();
13061       return true;
13062     }
13063     return false;
13064   }
13065   case MVT::f64:
13066   case MVT::v2f64: {
13067     // For any little-endian targets with neon, we can support unaligned ld/st
13068     // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
13069     // A big-endian target may also explicitly support unaligned accesses
13070     if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
13071       if (Fast)
13072         *Fast = true;
13073       return true;
13074     }
13075     return false;
13076   }
13077   }
13078 }
13079 
13080 static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
13081                        unsigned AlignCheck) {
13082   return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
13083           (DstAlign == 0 || DstAlign % AlignCheck == 0));
13084 }
13085 
13086 EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
13087                                            unsigned DstAlign, unsigned SrcAlign,
13088                                            bool IsMemset, bool ZeroMemset,
13089                                            bool MemcpyStrSrc,
13090                                            MachineFunction &MF) const {
13091   const Function &F = MF.getFunction();
13092 
13093   // See if we can use NEON instructions for this...
13094   if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
13095       !F.hasFnAttribute(Attribute::NoImplicitFloat)) {
13096     bool Fast;
13097     if (Size >= 16 &&
13098         (memOpAlign(SrcAlign, DstAlign, 16) ||
13099          (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
13100       return MVT::v2f64;
13101     } else if (Size >= 8 &&
13102                (memOpAlign(SrcAlign, DstAlign, 8) ||
13103                 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
13104                  Fast))) {
13105       return MVT::f64;
13106     }
13107   }
13108 
13109   // Let the target-independent logic figure it out.
13110   return MVT::Other;
13111 }
13112 
13113 // 64-bit integers are split into their high and low parts and held in two
13114 // different registers, so the trunc is free since the low register can just
13115 // be used.
13116 bool ARMTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const {
13117   if (!SrcTy->isIntegerTy() || !DstTy->isIntegerTy())
13118     return false;
13119   unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();
13120   unsigned DestBits = DstTy->getPrimitiveSizeInBits();
13121   return (SrcBits == 64 && DestBits == 32);
13122 }
13123 
13124 bool ARMTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const {
13125   if (SrcVT.isVector() || DstVT.isVector() || !SrcVT.isInteger() ||
13126       !DstVT.isInteger())
13127     return false;
13128   unsigned SrcBits = SrcVT.getSizeInBits();
13129   unsigned DestBits = DstVT.getSizeInBits();
13130   return (SrcBits == 64 && DestBits == 32);
13131 }
13132 
13133 bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
13134   if (Val.getOpcode() != ISD::LOAD)
13135     return false;
13136 
13137   EVT VT1 = Val.getValueType();
13138   if (!VT1.isSimple() || !VT1.isInteger() ||
13139       !VT2.isSimple() || !VT2.isInteger())
13140     return false;
13141 
13142   switch (VT1.getSimpleVT().SimpleTy) {
13143   default: break;
13144   case MVT::i1:
13145   case MVT::i8:
13146   case MVT::i16:
13147     // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
13148     return true;
13149   }
13150 
13151   return false;
13152 }
13153 
13154 bool ARMTargetLowering::isFNegFree(EVT VT) const {
13155   if (!VT.isSimple())
13156     return false;
13157 
13158   // There are quite a few FP16 instructions (e.g. VNMLA, VNMLS, etc.) that
13159   // negate values directly (fneg is free). So, we don't want to let the DAG
13160   // combiner rewrite fneg into xors and some other instructions.  For f16 and
13161   // FullFP16 argument passing, some bitcast nodes may be introduced,
13162   // triggering this DAG combine rewrite, so we are avoiding that with this.
13163   switch (VT.getSimpleVT().SimpleTy) {
13164   default: break;
13165   case MVT::f16:
13166     return Subtarget->hasFullFP16();
13167   }
13168 
13169   return false;
13170 }
13171 
13172 /// Check if Ext1 and Ext2 are extends of the same type, doubling the bitwidth
13173 /// of the vector elements.
13174 static bool areExtractExts(Value *Ext1, Value *Ext2) {
13175   auto areExtDoubled = [](Instruction *Ext) {
13176     return Ext->getType()->getScalarSizeInBits() ==
13177            2 * Ext->getOperand(0)->getType()->getScalarSizeInBits();
13178   };
13179 
13180   if (!match(Ext1, m_ZExtOrSExt(m_Value())) ||
13181       !match(Ext2, m_ZExtOrSExt(m_Value())) ||
13182       !areExtDoubled(cast<Instruction>(Ext1)) ||
13183       !areExtDoubled(cast<Instruction>(Ext2)))
13184     return false;
13185 
13186   return true;
13187 }
13188 
13189 /// Check if sinking \p I's operands to I's basic block is profitable, because
13190 /// the operands can be folded into a target instruction, e.g.
13191 /// sext/zext can be folded into vsubl.
13192 bool ARMTargetLowering::shouldSinkOperands(Instruction *I,
13193                                            SmallVectorImpl<Use *> &Ops) const {
13194   if (!Subtarget->hasNEON() || !I->getType()->isVectorTy())
13195     return false;
13196 
13197   switch (I->getOpcode()) {
13198   case Instruction::Sub:
13199   case Instruction::Add: {
13200     if (!areExtractExts(I->getOperand(0), I->getOperand(1)))
13201       return false;
13202     Ops.push_back(&I->getOperandUse(0));
13203     Ops.push_back(&I->getOperandUse(1));
13204     return true;
13205   }
13206   default:
13207     return false;
13208   }
13209   return false;
13210 }
13211 
13212 bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
13213   EVT VT = ExtVal.getValueType();
13214 
13215   if (!isTypeLegal(VT))
13216     return false;
13217 
13218   // Don't create a loadext if we can fold the extension into a wide/long
13219   // instruction.
13220   // If there's more than one user instruction, the loadext is desirable no
13221   // matter what.  There can be two uses by the same instruction.
13222   if (ExtVal->use_empty() ||
13223       !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
13224     return true;
13225 
13226   SDNode *U = *ExtVal->use_begin();
13227   if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
13228        U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
13229     return false;
13230 
13231   return true;
13232 }
13233 
13234 bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
13235   if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
13236     return false;
13237 
13238   if (!isTypeLegal(EVT::getEVT(Ty1)))
13239     return false;
13240 
13241   assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
13242 
13243   // Assuming the caller doesn't have a zeroext or signext return parameter,
13244   // truncation all the way down to i1 is valid.
13245   return true;
13246 }
13247 
13248 int ARMTargetLowering::getScalingFactorCost(const DataLayout &DL,
13249                                                 const AddrMode &AM, Type *Ty,
13250                                                 unsigned AS) const {
13251   if (isLegalAddressingMode(DL, AM, Ty, AS)) {
13252     if (Subtarget->hasFPAO())
13253       return AM.Scale < 0 ? 1 : 0; // positive offsets execute faster
13254     return 0;
13255   }
13256   return -1;
13257 }
13258 
13259 static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
13260   if (V < 0)
13261     return false;
13262 
13263   unsigned Scale = 1;
13264   switch (VT.getSimpleVT().SimpleTy) {
13265   default: return false;
13266   case MVT::i1:
13267   case MVT::i8:
13268     // Scale == 1;
13269     break;
13270   case MVT::i16:
13271     // Scale == 2;
13272     Scale = 2;
13273     break;
13274   case MVT::i32:
13275     // Scale == 4;
13276     Scale = 4;
13277     break;
13278   }
13279 
13280   if ((V & (Scale - 1)) != 0)
13281     return false;
13282   V /= Scale;
13283   return V == (V & ((1LL << 5) - 1));
13284 }
13285 
13286 static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
13287                                       const ARMSubtarget *Subtarget) {
13288   bool isNeg = false;
13289   if (V < 0) {
13290     isNeg = true;
13291     V = - V;
13292   }
13293 
13294   switch (VT.getSimpleVT().SimpleTy) {
13295   default: return false;
13296   case MVT::i1:
13297   case MVT::i8:
13298   case MVT::i16:
13299   case MVT::i32:
13300     // + imm12 or - imm8
13301     if (isNeg)
13302       return V == (V & ((1LL << 8) - 1));
13303     return V == (V & ((1LL << 12) - 1));
13304   case MVT::f32:
13305   case MVT::f64:
13306     // Same as ARM mode. FIXME: NEON?
13307     if (!Subtarget->hasVFP2())
13308       return false;
13309     if ((V & 3) != 0)
13310       return false;
13311     V >>= 2;
13312     return V == (V & ((1LL << 8) - 1));
13313   }
13314 }
13315 
13316 /// isLegalAddressImmediate - Return true if the integer value can be used
13317 /// as the offset of the target addressing mode for load / store of the
13318 /// given type.
13319 static bool isLegalAddressImmediate(int64_t V, EVT VT,
13320                                     const ARMSubtarget *Subtarget) {
13321   if (V == 0)
13322     return true;
13323 
13324   if (!VT.isSimple())
13325     return false;
13326 
13327   if (Subtarget->isThumb1Only())
13328     return isLegalT1AddressImmediate(V, VT);
13329   else if (Subtarget->isThumb2())
13330     return isLegalT2AddressImmediate(V, VT, Subtarget);
13331 
13332   // ARM mode.
13333   if (V < 0)
13334     V = - V;
13335   switch (VT.getSimpleVT().SimpleTy) {
13336   default: return false;
13337   case MVT::i1:
13338   case MVT::i8:
13339   case MVT::i32:
13340     // +- imm12
13341     return V == (V & ((1LL << 12) - 1));
13342   case MVT::i16:
13343     // +- imm8
13344     return V == (V & ((1LL << 8) - 1));
13345   case MVT::f32:
13346   case MVT::f64:
13347     if (!Subtarget->hasVFP2()) // FIXME: NEON?
13348       return false;
13349     if ((V & 3) != 0)
13350       return false;
13351     V >>= 2;
13352     return V == (V & ((1LL << 8) - 1));
13353   }
13354 }
13355 
13356 bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
13357                                                       EVT VT) const {
13358   int Scale = AM.Scale;
13359   if (Scale < 0)
13360     return false;
13361 
13362   switch (VT.getSimpleVT().SimpleTy) {
13363   default: return false;
13364   case MVT::i1:
13365   case MVT::i8:
13366   case MVT::i16:
13367   case MVT::i32:
13368     if (Scale == 1)
13369       return true;
13370     // r + r << imm
13371     Scale = Scale & ~1;
13372     return Scale == 2 || Scale == 4 || Scale == 8;
13373   case MVT::i64:
13374     // FIXME: What are we trying to model here? ldrd doesn't have an r + r
13375     // version in Thumb mode.
13376     // r + r
13377     if (Scale == 1)
13378       return true;
13379     // r * 2 (this can be lowered to r + r).
13380     if (!AM.HasBaseReg && Scale == 2)
13381       return true;
13382     return false;
13383   case MVT::isVoid:
13384     // Note, we allow "void" uses (basically, uses that aren't loads or
13385     // stores), because arm allows folding a scale into many arithmetic
13386     // operations.  This should be made more precise and revisited later.
13387 
13388     // Allow r << imm, but the imm has to be a multiple of two.
13389     if (Scale & 1) return false;
13390     return isPowerOf2_32(Scale);
13391   }
13392 }
13393 
13394 bool ARMTargetLowering::isLegalT1ScaledAddressingMode(const AddrMode &AM,
13395                                                       EVT VT) const {
13396   const int Scale = AM.Scale;
13397 
13398   // Negative scales are not supported in Thumb1.
13399   if (Scale < 0)
13400     return false;
13401 
13402   // Thumb1 addressing modes do not support register scaling excepting the
13403   // following cases:
13404   // 1. Scale == 1 means no scaling.
13405   // 2. Scale == 2 this can be lowered to r + r if there is no base register.
13406   return (Scale == 1) || (!AM.HasBaseReg && Scale == 2);
13407 }
13408 
13409 /// isLegalAddressingMode - Return true if the addressing mode represented
13410 /// by AM is legal for this target, for a load/store of the specified type.
13411 bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
13412                                               const AddrMode &AM, Type *Ty,
13413                                               unsigned AS, Instruction *I) const {
13414   EVT VT = getValueType(DL, Ty, true);
13415   if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
13416     return false;
13417 
13418   // Can never fold addr of global into load/store.
13419   if (AM.BaseGV)
13420     return false;
13421 
13422   switch (AM.Scale) {
13423   case 0:  // no scale reg, must be "r+i" or "r", or "i".
13424     break;
13425   default:
13426     // ARM doesn't support any R+R*scale+imm addr modes.
13427     if (AM.BaseOffs)
13428       return false;
13429 
13430     if (!VT.isSimple())
13431       return false;
13432 
13433     if (Subtarget->isThumb1Only())
13434       return isLegalT1ScaledAddressingMode(AM, VT);
13435 
13436     if (Subtarget->isThumb2())
13437       return isLegalT2ScaledAddressingMode(AM, VT);
13438 
13439     int Scale = AM.Scale;
13440     switch (VT.getSimpleVT().SimpleTy) {
13441     default: return false;
13442     case MVT::i1:
13443     case MVT::i8:
13444     case MVT::i32:
13445       if (Scale < 0) Scale = -Scale;
13446       if (Scale == 1)
13447         return true;
13448       // r + r << imm
13449       return isPowerOf2_32(Scale & ~1);
13450     case MVT::i16:
13451     case MVT::i64:
13452       // r +/- r
13453       if (Scale == 1 || (AM.HasBaseReg && Scale == -1))
13454         return true;
13455       // r * 2 (this can be lowered to r + r).
13456       if (!AM.HasBaseReg && Scale == 2)
13457         return true;
13458       return false;
13459 
13460     case MVT::isVoid:
13461       // Note, we allow "void" uses (basically, uses that aren't loads or
13462       // stores), because arm allows folding a scale into many arithmetic
13463       // operations.  This should be made more precise and revisited later.
13464 
13465       // Allow r << imm, but the imm has to be a multiple of two.
13466       if (Scale & 1) return false;
13467       return isPowerOf2_32(Scale);
13468     }
13469   }
13470   return true;
13471 }
13472 
13473 /// isLegalICmpImmediate - Return true if the specified immediate is legal
13474 /// icmp immediate, that is the target has icmp instructions which can compare
13475 /// a register against the immediate without having to materialize the
13476 /// immediate into a register.
13477 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
13478   // Thumb2 and ARM modes can use cmn for negative immediates.
13479   if (!Subtarget->isThumb())
13480     return ARM_AM::getSOImmVal((uint32_t)Imm) != -1 ||
13481            ARM_AM::getSOImmVal(-(uint32_t)Imm) != -1;
13482   if (Subtarget->isThumb2())
13483     return ARM_AM::getT2SOImmVal((uint32_t)Imm) != -1 ||
13484            ARM_AM::getT2SOImmVal(-(uint32_t)Imm) != -1;
13485   // Thumb1 doesn't have cmn, and only 8-bit immediates.
13486   return Imm >= 0 && Imm <= 255;
13487 }
13488 
13489 /// isLegalAddImmediate - Return true if the specified immediate is a legal add
13490 /// *or sub* immediate, that is the target has add or sub instructions which can
13491 /// add a register with the immediate without having to materialize the
13492 /// immediate into a register.
13493 bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
13494   // Same encoding for add/sub, just flip the sign.
13495   int64_t AbsImm = std::abs(Imm);
13496   if (!Subtarget->isThumb())
13497     return ARM_AM::getSOImmVal(AbsImm) != -1;
13498   if (Subtarget->isThumb2())
13499     return ARM_AM::getT2SOImmVal(AbsImm) != -1;
13500   // Thumb1 only has 8-bit unsigned immediate.
13501   return AbsImm >= 0 && AbsImm <= 255;
13502 }
13503 
13504 static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
13505                                       bool isSEXTLoad, SDValue &Base,
13506                                       SDValue &Offset, bool &isInc,
13507                                       SelectionDAG &DAG) {
13508   if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
13509     return false;
13510 
13511   if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
13512     // AddressingMode 3
13513     Base = Ptr->getOperand(0);
13514     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
13515       int RHSC = (int)RHS->getZExtValue();
13516       if (RHSC < 0 && RHSC > -256) {
13517         assert(Ptr->getOpcode() == ISD::ADD);
13518         isInc = false;
13519         Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
13520         return true;
13521       }
13522     }
13523     isInc = (Ptr->getOpcode() == ISD::ADD);
13524     Offset = Ptr->getOperand(1);
13525     return true;
13526   } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
13527     // AddressingMode 2
13528     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
13529       int RHSC = (int)RHS->getZExtValue();
13530       if (RHSC < 0 && RHSC > -0x1000) {
13531         assert(Ptr->getOpcode() == ISD::ADD);
13532         isInc = false;
13533         Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
13534         Base = Ptr->getOperand(0);
13535         return true;
13536       }
13537     }
13538 
13539     if (Ptr->getOpcode() == ISD::ADD) {
13540       isInc = true;
13541       ARM_AM::ShiftOpc ShOpcVal=
13542         ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
13543       if (ShOpcVal != ARM_AM::no_shift) {
13544         Base = Ptr->getOperand(1);
13545         Offset = Ptr->getOperand(0);
13546       } else {
13547         Base = Ptr->getOperand(0);
13548         Offset = Ptr->getOperand(1);
13549       }
13550       return true;
13551     }
13552 
13553     isInc = (Ptr->getOpcode() == ISD::ADD);
13554     Base = Ptr->getOperand(0);
13555     Offset = Ptr->getOperand(1);
13556     return true;
13557   }
13558 
13559   // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
13560   return false;
13561 }
13562 
13563 static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
13564                                      bool isSEXTLoad, SDValue &Base,
13565                                      SDValue &Offset, bool &isInc,
13566                                      SelectionDAG &DAG) {
13567   if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
13568     return false;
13569 
13570   Base = Ptr->getOperand(0);
13571   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
13572     int RHSC = (int)RHS->getZExtValue();
13573     if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
13574       assert(Ptr->getOpcode() == ISD::ADD);
13575       isInc = false;
13576       Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
13577       return true;
13578     } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
13579       isInc = Ptr->getOpcode() == ISD::ADD;
13580       Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
13581       return true;
13582     }
13583   }
13584 
13585   return false;
13586 }
13587 
13588 /// getPreIndexedAddressParts - returns true by value, base pointer and
13589 /// offset pointer and addressing mode by reference if the node's address
13590 /// can be legally represented as pre-indexed load / store address.
13591 bool
13592 ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
13593                                              SDValue &Offset,
13594                                              ISD::MemIndexedMode &AM,
13595                                              SelectionDAG &DAG) const {
13596   if (Subtarget->isThumb1Only())
13597     return false;
13598 
13599   EVT VT;
13600   SDValue Ptr;
13601   bool isSEXTLoad = false;
13602   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
13603     Ptr = LD->getBasePtr();
13604     VT  = LD->getMemoryVT();
13605     isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
13606   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
13607     Ptr = ST->getBasePtr();
13608     VT  = ST->getMemoryVT();
13609   } else
13610     return false;
13611 
13612   bool isInc;
13613   bool isLegal = false;
13614   if (Subtarget->isThumb2())
13615     isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
13616                                        Offset, isInc, DAG);
13617   else
13618     isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
13619                                         Offset, isInc, DAG);
13620   if (!isLegal)
13621     return false;
13622 
13623   AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
13624   return true;
13625 }
13626 
13627 /// getPostIndexedAddressParts - returns true by value, base pointer and
13628 /// offset pointer and addressing mode by reference if this node can be
13629 /// combined with a load / store to form a post-indexed load / store.
13630 bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
13631                                                    SDValue &Base,
13632                                                    SDValue &Offset,
13633                                                    ISD::MemIndexedMode &AM,
13634                                                    SelectionDAG &DAG) const {
13635   EVT VT;
13636   SDValue Ptr;
13637   bool isSEXTLoad = false, isNonExt;
13638   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
13639     VT  = LD->getMemoryVT();
13640     Ptr = LD->getBasePtr();
13641     isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
13642     isNonExt = LD->getExtensionType() == ISD::NON_EXTLOAD;
13643   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
13644     VT  = ST->getMemoryVT();
13645     Ptr = ST->getBasePtr();
13646     isNonExt = !ST->isTruncatingStore();
13647   } else
13648     return false;
13649 
13650   if (Subtarget->isThumb1Only()) {
13651     // Thumb-1 can do a limited post-inc load or store as an updating LDM. It
13652     // must be non-extending/truncating, i32, with an offset of 4.
13653     assert(Op->getValueType(0) == MVT::i32 && "Non-i32 post-inc op?!");
13654     if (Op->getOpcode() != ISD::ADD || !isNonExt)
13655       return false;
13656     auto *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1));
13657     if (!RHS || RHS->getZExtValue() != 4)
13658       return false;
13659 
13660     Offset = Op->getOperand(1);
13661     Base = Op->getOperand(0);
13662     AM = ISD::POST_INC;
13663     return true;
13664   }
13665 
13666   bool isInc;
13667   bool isLegal = false;
13668   if (Subtarget->isThumb2())
13669     isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
13670                                        isInc, DAG);
13671   else
13672     isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
13673                                         isInc, DAG);
13674   if (!isLegal)
13675     return false;
13676 
13677   if (Ptr != Base) {
13678     // Swap base ptr and offset to catch more post-index load / store when
13679     // it's legal. In Thumb2 mode, offset must be an immediate.
13680     if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
13681         !Subtarget->isThumb2())
13682       std::swap(Base, Offset);
13683 
13684     // Post-indexed load / store update the base pointer.
13685     if (Ptr != Base)
13686       return false;
13687   }
13688 
13689   AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
13690   return true;
13691 }
13692 
13693 void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
13694                                                       KnownBits &Known,
13695                                                       const APInt &DemandedElts,
13696                                                       const SelectionDAG &DAG,
13697                                                       unsigned Depth) const {
13698   unsigned BitWidth = Known.getBitWidth();
13699   Known.resetAll();
13700   switch (Op.getOpcode()) {
13701   default: break;
13702   case ARMISD::ADDC:
13703   case ARMISD::ADDE:
13704   case ARMISD::SUBC:
13705   case ARMISD::SUBE:
13706     // Special cases when we convert a carry to a boolean.
13707     if (Op.getResNo() == 0) {
13708       SDValue LHS = Op.getOperand(0);
13709       SDValue RHS = Op.getOperand(1);
13710       // (ADDE 0, 0, C) will give us a single bit.
13711       if (Op->getOpcode() == ARMISD::ADDE && isNullConstant(LHS) &&
13712           isNullConstant(RHS)) {
13713         Known.Zero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
13714         return;
13715       }
13716     }
13717     break;
13718   case ARMISD::CMOV: {
13719     // Bits are known zero/one if known on the LHS and RHS.
13720     Known = DAG.computeKnownBits(Op.getOperand(0), Depth+1);
13721     if (Known.isUnknown())
13722       return;
13723 
13724     KnownBits KnownRHS = DAG.computeKnownBits(Op.getOperand(1), Depth+1);
13725     Known.Zero &= KnownRHS.Zero;
13726     Known.One  &= KnownRHS.One;
13727     return;
13728   }
13729   case ISD::INTRINSIC_W_CHAIN: {
13730     ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
13731     Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
13732     switch (IntID) {
13733     default: return;
13734     case Intrinsic::arm_ldaex:
13735     case Intrinsic::arm_ldrex: {
13736       EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
13737       unsigned MemBits = VT.getScalarSizeInBits();
13738       Known.Zero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
13739       return;
13740     }
13741     }
13742   }
13743   case ARMISD::BFI: {
13744     // Conservatively, we can recurse down the first operand
13745     // and just mask out all affected bits.
13746     Known = DAG.computeKnownBits(Op.getOperand(0), Depth + 1);
13747 
13748     // The operand to BFI is already a mask suitable for removing the bits it
13749     // sets.
13750     ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2));
13751     const APInt &Mask = CI->getAPIntValue();
13752     Known.Zero &= Mask;
13753     Known.One &= Mask;
13754     return;
13755   }
13756   case ARMISD::VGETLANEs:
13757   case ARMISD::VGETLANEu: {
13758     const SDValue &SrcSV = Op.getOperand(0);
13759     EVT VecVT = SrcSV.getValueType();
13760     assert(VecVT.isVector() && "VGETLANE expected a vector type");
13761     const unsigned NumSrcElts = VecVT.getVectorNumElements();
13762     ConstantSDNode *Pos = cast<ConstantSDNode>(Op.getOperand(1).getNode());
13763     assert(Pos->getAPIntValue().ult(NumSrcElts) &&
13764            "VGETLANE index out of bounds");
13765     unsigned Idx = Pos->getZExtValue();
13766     APInt DemandedElt = APInt::getOneBitSet(NumSrcElts, Idx);
13767     Known = DAG.computeKnownBits(SrcSV, DemandedElt, Depth + 1);
13768 
13769     EVT VT = Op.getValueType();
13770     const unsigned DstSz = VT.getScalarSizeInBits();
13771     const unsigned SrcSz = VecVT.getVectorElementType().getSizeInBits();
13772     (void)SrcSz;
13773     assert(SrcSz == Known.getBitWidth());
13774     assert(DstSz > SrcSz);
13775     if (Op.getOpcode() == ARMISD::VGETLANEs)
13776       Known = Known.sext(DstSz);
13777     else {
13778       Known = Known.zext(DstSz, true /* extended bits are known zero */);
13779     }
13780     assert(DstSz == Known.getBitWidth());
13781     break;
13782   }
13783   }
13784 }
13785 
13786 bool
13787 ARMTargetLowering::targetShrinkDemandedConstant(SDValue Op,
13788                                                 const APInt &DemandedAPInt,
13789                                                 TargetLoweringOpt &TLO) const {
13790   // Delay optimization, so we don't have to deal with illegal types, or block
13791   // optimizations.
13792   if (!TLO.LegalOps)
13793     return false;
13794 
13795   // Only optimize AND for now.
13796   if (Op.getOpcode() != ISD::AND)
13797     return false;
13798 
13799   EVT VT = Op.getValueType();
13800 
13801   // Ignore vectors.
13802   if (VT.isVector())
13803     return false;
13804 
13805   assert(VT == MVT::i32 && "Unexpected integer type");
13806 
13807   // Make sure the RHS really is a constant.
13808   ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
13809   if (!C)
13810     return false;
13811 
13812   unsigned Mask = C->getZExtValue();
13813 
13814   unsigned Demanded = DemandedAPInt.getZExtValue();
13815   unsigned ShrunkMask = Mask & Demanded;
13816   unsigned ExpandedMask = Mask | ~Demanded;
13817 
13818   // If the mask is all zeros, let the target-independent code replace the
13819   // result with zero.
13820   if (ShrunkMask == 0)
13821     return false;
13822 
13823   // If the mask is all ones, erase the AND. (Currently, the target-independent
13824   // code won't do this, so we have to do it explicitly to avoid an infinite
13825   // loop in obscure cases.)
13826   if (ExpandedMask == ~0U)
13827     return TLO.CombineTo(Op, Op.getOperand(0));
13828 
13829   auto IsLegalMask = [ShrunkMask, ExpandedMask](unsigned Mask) -> bool {
13830     return (ShrunkMask & Mask) == ShrunkMask && (~ExpandedMask & Mask) == 0;
13831   };
13832   auto UseMask = [Mask, Op, VT, &TLO](unsigned NewMask) -> bool {
13833     if (NewMask == Mask)
13834       return true;
13835     SDLoc DL(Op);
13836     SDValue NewC = TLO.DAG.getConstant(NewMask, DL, VT);
13837     SDValue NewOp = TLO.DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), NewC);
13838     return TLO.CombineTo(Op, NewOp);
13839   };
13840 
13841   // Prefer uxtb mask.
13842   if (IsLegalMask(0xFF))
13843     return UseMask(0xFF);
13844 
13845   // Prefer uxth mask.
13846   if (IsLegalMask(0xFFFF))
13847     return UseMask(0xFFFF);
13848 
13849   // [1, 255] is Thumb1 movs+ands, legal immediate for ARM/Thumb2.
13850   // FIXME: Prefer a contiguous sequence of bits for other optimizations.
13851   if (ShrunkMask < 256)
13852     return UseMask(ShrunkMask);
13853 
13854   // [-256, -2] is Thumb1 movs+bics, legal immediate for ARM/Thumb2.
13855   // FIXME: Prefer a contiguous sequence of bits for other optimizations.
13856   if ((int)ExpandedMask <= -2 && (int)ExpandedMask >= -256)
13857     return UseMask(ExpandedMask);
13858 
13859   // Potential improvements:
13860   //
13861   // We could try to recognize lsls+lsrs or lsrs+lsls pairs here.
13862   // We could try to prefer Thumb1 immediates which can be lowered to a
13863   // two-instruction sequence.
13864   // We could try to recognize more legal ARM/Thumb2 immediates here.
13865 
13866   return false;
13867 }
13868 
13869 
13870 //===----------------------------------------------------------------------===//
13871 //                           ARM Inline Assembly Support
13872 //===----------------------------------------------------------------------===//
13873 
13874 bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
13875   // Looking for "rev" which is V6+.
13876   if (!Subtarget->hasV6Ops())
13877     return false;
13878 
13879   InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
13880   std::string AsmStr = IA->getAsmString();
13881   SmallVector<StringRef, 4> AsmPieces;
13882   SplitString(AsmStr, AsmPieces, ";\n");
13883 
13884   switch (AsmPieces.size()) {
13885   default: return false;
13886   case 1:
13887     AsmStr = AsmPieces[0];
13888     AsmPieces.clear();
13889     SplitString(AsmStr, AsmPieces, " \t,");
13890 
13891     // rev $0, $1
13892     if (AsmPieces.size() == 3 &&
13893         AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
13894         IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
13895       IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
13896       if (Ty && Ty->getBitWidth() == 32)
13897         return IntrinsicLowering::LowerToByteSwap(CI);
13898     }
13899     break;
13900   }
13901 
13902   return false;
13903 }
13904 
13905 const char *ARMTargetLowering::LowerXConstraint(EVT ConstraintVT) const {
13906   // At this point, we have to lower this constraint to something else, so we
13907   // lower it to an "r" or "w". However, by doing this we will force the result
13908   // to be in register, while the X constraint is much more permissive.
13909   //
13910   // Although we are correct (we are free to emit anything, without
13911   // constraints), we might break use cases that would expect us to be more
13912   // efficient and emit something else.
13913   if (!Subtarget->hasVFP2())
13914     return "r";
13915   if (ConstraintVT.isFloatingPoint())
13916     return "w";
13917   if (ConstraintVT.isVector() && Subtarget->hasNEON() &&
13918      (ConstraintVT.getSizeInBits() == 64 ||
13919       ConstraintVT.getSizeInBits() == 128))
13920     return "w";
13921 
13922   return "r";
13923 }
13924 
13925 /// getConstraintType - Given a constraint letter, return the type of
13926 /// constraint it is for this target.
13927 ARMTargetLowering::ConstraintType
13928 ARMTargetLowering::getConstraintType(StringRef Constraint) const {
13929   if (Constraint.size() == 1) {
13930     switch (Constraint[0]) {
13931     default:  break;
13932     case 'l': return C_RegisterClass;
13933     case 'w': return C_RegisterClass;
13934     case 'h': return C_RegisterClass;
13935     case 'x': return C_RegisterClass;
13936     case 't': return C_RegisterClass;
13937     case 'j': return C_Other; // Constant for movw.
13938       // An address with a single base register. Due to the way we
13939       // currently handle addresses it is the same as an 'r' memory constraint.
13940     case 'Q': return C_Memory;
13941     }
13942   } else if (Constraint.size() == 2) {
13943     switch (Constraint[0]) {
13944     default: break;
13945     // All 'U+' constraints are addresses.
13946     case 'U': return C_Memory;
13947     }
13948   }
13949   return TargetLowering::getConstraintType(Constraint);
13950 }
13951 
13952 /// Examine constraint type and operand type and determine a weight value.
13953 /// This object must already have been set up with the operand type
13954 /// and the current alternative constraint selected.
13955 TargetLowering::ConstraintWeight
13956 ARMTargetLowering::getSingleConstraintMatchWeight(
13957     AsmOperandInfo &info, const char *constraint) const {
13958   ConstraintWeight weight = CW_Invalid;
13959   Value *CallOperandVal = info.CallOperandVal;
13960     // If we don't have a value, we can't do a match,
13961     // but allow it at the lowest weight.
13962   if (!CallOperandVal)
13963     return CW_Default;
13964   Type *type = CallOperandVal->getType();
13965   // Look at the constraint type.
13966   switch (*constraint) {
13967   default:
13968     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
13969     break;
13970   case 'l':
13971     if (type->isIntegerTy()) {
13972       if (Subtarget->isThumb())
13973         weight = CW_SpecificReg;
13974       else
13975         weight = CW_Register;
13976     }
13977     break;
13978   case 'w':
13979     if (type->isFloatingPointTy())
13980       weight = CW_Register;
13981     break;
13982   }
13983   return weight;
13984 }
13985 
13986 using RCPair = std::pair<unsigned, const TargetRegisterClass *>;
13987 
13988 RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
13989     const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
13990   if (Constraint.size() == 1) {
13991     // GCC ARM Constraint Letters
13992     switch (Constraint[0]) {
13993     case 'l': // Low regs or general regs.
13994       if (Subtarget->isThumb())
13995         return RCPair(0U, &ARM::tGPRRegClass);
13996       return RCPair(0U, &ARM::GPRRegClass);
13997     case 'h': // High regs or no regs.
13998       if (Subtarget->isThumb())
13999         return RCPair(0U, &ARM::hGPRRegClass);
14000       break;
14001     case 'r':
14002       if (Subtarget->isThumb1Only())
14003         return RCPair(0U, &ARM::tGPRRegClass);
14004       return RCPair(0U, &ARM::GPRRegClass);
14005     case 'w':
14006       if (VT == MVT::Other)
14007         break;
14008       if (VT == MVT::f32)
14009         return RCPair(0U, &ARM::SPRRegClass);
14010       if (VT.getSizeInBits() == 64)
14011         return RCPair(0U, &ARM::DPRRegClass);
14012       if (VT.getSizeInBits() == 128)
14013         return RCPair(0U, &ARM::QPRRegClass);
14014       break;
14015     case 'x':
14016       if (VT == MVT::Other)
14017         break;
14018       if (VT == MVT::f32)
14019         return RCPair(0U, &ARM::SPR_8RegClass);
14020       if (VT.getSizeInBits() == 64)
14021         return RCPair(0U, &ARM::DPR_8RegClass);
14022       if (VT.getSizeInBits() == 128)
14023         return RCPair(0U, &ARM::QPR_8RegClass);
14024       break;
14025     case 't':
14026       if (VT == MVT::Other)
14027         break;
14028       if (VT == MVT::f32 || VT == MVT::i32)
14029         return RCPair(0U, &ARM::SPRRegClass);
14030       if (VT.getSizeInBits() == 64)
14031         return RCPair(0U, &ARM::DPR_VFP2RegClass);
14032       if (VT.getSizeInBits() == 128)
14033         return RCPair(0U, &ARM::QPR_VFP2RegClass);
14034       break;
14035     }
14036   }
14037   if (StringRef("{cc}").equals_lower(Constraint))
14038     return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
14039 
14040   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
14041 }
14042 
14043 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
14044 /// vector.  If it is invalid, don't add anything to Ops.
14045 void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
14046                                                      std::string &Constraint,
14047                                                      std::vector<SDValue>&Ops,
14048                                                      SelectionDAG &DAG) const {
14049   SDValue Result;
14050 
14051   // Currently only support length 1 constraints.
14052   if (Constraint.length() != 1) return;
14053 
14054   char ConstraintLetter = Constraint[0];
14055   switch (ConstraintLetter) {
14056   default: break;
14057   case 'j':
14058   case 'I': case 'J': case 'K': case 'L':
14059   case 'M': case 'N': case 'O':
14060     ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
14061     if (!C)
14062       return;
14063 
14064     int64_t CVal64 = C->getSExtValue();
14065     int CVal = (int) CVal64;
14066     // None of these constraints allow values larger than 32 bits.  Check
14067     // that the value fits in an int.
14068     if (CVal != CVal64)
14069       return;
14070 
14071     switch (ConstraintLetter) {
14072       case 'j':
14073         // Constant suitable for movw, must be between 0 and
14074         // 65535.
14075         if (Subtarget->hasV6T2Ops())
14076           if (CVal >= 0 && CVal <= 65535)
14077             break;
14078         return;
14079       case 'I':
14080         if (Subtarget->isThumb1Only()) {
14081           // This must be a constant between 0 and 255, for ADD
14082           // immediates.
14083           if (CVal >= 0 && CVal <= 255)
14084             break;
14085         } else if (Subtarget->isThumb2()) {
14086           // A constant that can be used as an immediate value in a
14087           // data-processing instruction.
14088           if (ARM_AM::getT2SOImmVal(CVal) != -1)
14089             break;
14090         } else {
14091           // A constant that can be used as an immediate value in a
14092           // data-processing instruction.
14093           if (ARM_AM::getSOImmVal(CVal) != -1)
14094             break;
14095         }
14096         return;
14097 
14098       case 'J':
14099         if (Subtarget->isThumb1Only()) {
14100           // This must be a constant between -255 and -1, for negated ADD
14101           // immediates. This can be used in GCC with an "n" modifier that
14102           // prints the negated value, for use with SUB instructions. It is
14103           // not useful otherwise but is implemented for compatibility.
14104           if (CVal >= -255 && CVal <= -1)
14105             break;
14106         } else {
14107           // This must be a constant between -4095 and 4095. It is not clear
14108           // what this constraint is intended for. Implemented for
14109           // compatibility with GCC.
14110           if (CVal >= -4095 && CVal <= 4095)
14111             break;
14112         }
14113         return;
14114 
14115       case 'K':
14116         if (Subtarget->isThumb1Only()) {
14117           // A 32-bit value where only one byte has a nonzero value. Exclude
14118           // zero to match GCC. This constraint is used by GCC internally for
14119           // constants that can be loaded with a move/shift combination.
14120           // It is not useful otherwise but is implemented for compatibility.
14121           if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
14122             break;
14123         } else if (Subtarget->isThumb2()) {
14124           // A constant whose bitwise inverse can be used as an immediate
14125           // value in a data-processing instruction. This can be used in GCC
14126           // with a "B" modifier that prints the inverted value, for use with
14127           // BIC and MVN instructions. It is not useful otherwise but is
14128           // implemented for compatibility.
14129           if (ARM_AM::getT2SOImmVal(~CVal) != -1)
14130             break;
14131         } else {
14132           // A constant whose bitwise inverse can be used as an immediate
14133           // value in a data-processing instruction. This can be used in GCC
14134           // with a "B" modifier that prints the inverted value, for use with
14135           // BIC and MVN instructions. It is not useful otherwise but is
14136           // implemented for compatibility.
14137           if (ARM_AM::getSOImmVal(~CVal) != -1)
14138             break;
14139         }
14140         return;
14141 
14142       case 'L':
14143         if (Subtarget->isThumb1Only()) {
14144           // This must be a constant between -7 and 7,
14145           // for 3-operand ADD/SUB immediate instructions.
14146           if (CVal >= -7 && CVal < 7)
14147             break;
14148         } else if (Subtarget->isThumb2()) {
14149           // A constant whose negation can be used as an immediate value in a
14150           // data-processing instruction. This can be used in GCC with an "n"
14151           // modifier that prints the negated value, for use with SUB
14152           // instructions. It is not useful otherwise but is implemented for
14153           // compatibility.
14154           if (ARM_AM::getT2SOImmVal(-CVal) != -1)
14155             break;
14156         } else {
14157           // A constant whose negation can be used as an immediate value in a
14158           // data-processing instruction. This can be used in GCC with an "n"
14159           // modifier that prints the negated value, for use with SUB
14160           // instructions. It is not useful otherwise but is implemented for
14161           // compatibility.
14162           if (ARM_AM::getSOImmVal(-CVal) != -1)
14163             break;
14164         }
14165         return;
14166 
14167       case 'M':
14168         if (Subtarget->isThumb1Only()) {
14169           // This must be a multiple of 4 between 0 and 1020, for
14170           // ADD sp + immediate.
14171           if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
14172             break;
14173         } else {
14174           // A power of two or a constant between 0 and 32.  This is used in
14175           // GCC for the shift amount on shifted register operands, but it is
14176           // useful in general for any shift amounts.
14177           if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
14178             break;
14179         }
14180         return;
14181 
14182       case 'N':
14183         if (Subtarget->isThumb()) {  // FIXME thumb2
14184           // This must be a constant between 0 and 31, for shift amounts.
14185           if (CVal >= 0 && CVal <= 31)
14186             break;
14187         }
14188         return;
14189 
14190       case 'O':
14191         if (Subtarget->isThumb()) {  // FIXME thumb2
14192           // This must be a multiple of 4 between -508 and 508, for
14193           // ADD/SUB sp = sp + immediate.
14194           if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
14195             break;
14196         }
14197         return;
14198     }
14199     Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
14200     break;
14201   }
14202 
14203   if (Result.getNode()) {
14204     Ops.push_back(Result);
14205     return;
14206   }
14207   return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
14208 }
14209 
14210 static RTLIB::Libcall getDivRemLibcall(
14211     const SDNode *N, MVT::SimpleValueType SVT) {
14212   assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
14213           N->getOpcode() == ISD::SREM    || N->getOpcode() == ISD::UREM) &&
14214          "Unhandled Opcode in getDivRemLibcall");
14215   bool isSigned = N->getOpcode() == ISD::SDIVREM ||
14216                   N->getOpcode() == ISD::SREM;
14217   RTLIB::Libcall LC;
14218   switch (SVT) {
14219   default: llvm_unreachable("Unexpected request for libcall!");
14220   case MVT::i8:  LC = isSigned ? RTLIB::SDIVREM_I8  : RTLIB::UDIVREM_I8;  break;
14221   case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
14222   case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
14223   case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
14224   }
14225   return LC;
14226 }
14227 
14228 static TargetLowering::ArgListTy getDivRemArgList(
14229     const SDNode *N, LLVMContext *Context, const ARMSubtarget *Subtarget) {
14230   assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
14231           N->getOpcode() == ISD::SREM    || N->getOpcode() == ISD::UREM) &&
14232          "Unhandled Opcode in getDivRemArgList");
14233   bool isSigned = N->getOpcode() == ISD::SDIVREM ||
14234                   N->getOpcode() == ISD::SREM;
14235   TargetLowering::ArgListTy Args;
14236   TargetLowering::ArgListEntry Entry;
14237   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
14238     EVT ArgVT = N->getOperand(i).getValueType();
14239     Type *ArgTy = ArgVT.getTypeForEVT(*Context);
14240     Entry.Node = N->getOperand(i);
14241     Entry.Ty = ArgTy;
14242     Entry.IsSExt = isSigned;
14243     Entry.IsZExt = !isSigned;
14244     Args.push_back(Entry);
14245   }
14246   if (Subtarget->isTargetWindows() && Args.size() >= 2)
14247     std::swap(Args[0], Args[1]);
14248   return Args;
14249 }
14250 
14251 SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
14252   assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
14253           Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() ||
14254           Subtarget->isTargetWindows()) &&
14255          "Register-based DivRem lowering only");
14256   unsigned Opcode = Op->getOpcode();
14257   assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
14258          "Invalid opcode for Div/Rem lowering");
14259   bool isSigned = (Opcode == ISD::SDIVREM);
14260   EVT VT = Op->getValueType(0);
14261   Type *Ty = VT.getTypeForEVT(*DAG.getContext());
14262   SDLoc dl(Op);
14263 
14264   // If the target has hardware divide, use divide + multiply + subtract:
14265   //     div = a / b
14266   //     rem = a - b * div
14267   //     return {div, rem}
14268   // This should be lowered into UDIV/SDIV + MLS later on.
14269   bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivideInThumbMode()
14270                                         : Subtarget->hasDivideInARMMode();
14271   if (hasDivide && Op->getValueType(0).isSimple() &&
14272       Op->getSimpleValueType(0) == MVT::i32) {
14273     unsigned DivOpcode = isSigned ? ISD::SDIV : ISD::UDIV;
14274     const SDValue Dividend = Op->getOperand(0);
14275     const SDValue Divisor = Op->getOperand(1);
14276     SDValue Div = DAG.getNode(DivOpcode, dl, VT, Dividend, Divisor);
14277     SDValue Mul = DAG.getNode(ISD::MUL, dl, VT, Div, Divisor);
14278     SDValue Rem = DAG.getNode(ISD::SUB, dl, VT, Dividend, Mul);
14279 
14280     SDValue Values[2] = {Div, Rem};
14281     return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VT, VT), Values);
14282   }
14283 
14284   RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
14285                                        VT.getSimpleVT().SimpleTy);
14286   SDValue InChain = DAG.getEntryNode();
14287 
14288   TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
14289                                                     DAG.getContext(),
14290                                                     Subtarget);
14291 
14292   SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
14293                                          getPointerTy(DAG.getDataLayout()));
14294 
14295   Type *RetTy = StructType::get(Ty, Ty);
14296 
14297   if (Subtarget->isTargetWindows())
14298     InChain = WinDBZCheckDenominator(DAG, Op.getNode(), InChain);
14299 
14300   TargetLowering::CallLoweringInfo CLI(DAG);
14301   CLI.setDebugLoc(dl).setChain(InChain)
14302     .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
14303     .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
14304 
14305   std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
14306   return CallInfo.first;
14307 }
14308 
14309 // Lowers REM using divmod helpers
14310 // see RTABI section 4.2/4.3
14311 SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
14312   // Build return types (div and rem)
14313   std::vector<Type*> RetTyParams;
14314   Type *RetTyElement;
14315 
14316   switch (N->getValueType(0).getSimpleVT().SimpleTy) {
14317   default: llvm_unreachable("Unexpected request for libcall!");
14318   case MVT::i8:   RetTyElement = Type::getInt8Ty(*DAG.getContext());  break;
14319   case MVT::i16:  RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
14320   case MVT::i32:  RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
14321   case MVT::i64:  RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
14322   }
14323 
14324   RetTyParams.push_back(RetTyElement);
14325   RetTyParams.push_back(RetTyElement);
14326   ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
14327   Type *RetTy = StructType::get(*DAG.getContext(), ret);
14328 
14329   RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
14330                                                              SimpleTy);
14331   SDValue InChain = DAG.getEntryNode();
14332   TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext(),
14333                                                     Subtarget);
14334   bool isSigned = N->getOpcode() == ISD::SREM;
14335   SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
14336                                          getPointerTy(DAG.getDataLayout()));
14337 
14338   if (Subtarget->isTargetWindows())
14339     InChain = WinDBZCheckDenominator(DAG, N, InChain);
14340 
14341   // Lower call
14342   CallLoweringInfo CLI(DAG);
14343   CLI.setChain(InChain)
14344      .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args))
14345      .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
14346   std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
14347 
14348   // Return second (rem) result operand (first contains div)
14349   SDNode *ResNode = CallResult.first.getNode();
14350   assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
14351   return ResNode->getOperand(1);
14352 }
14353 
14354 SDValue
14355 ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
14356   assert(Subtarget->isTargetWindows() && "unsupported target platform");
14357   SDLoc DL(Op);
14358 
14359   // Get the inputs.
14360   SDValue Chain = Op.getOperand(0);
14361   SDValue Size  = Op.getOperand(1);
14362 
14363   if (DAG.getMachineFunction().getFunction().hasFnAttribute(
14364           "no-stack-arg-probe")) {
14365     unsigned Align = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
14366     SDValue SP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
14367     Chain = SP.getValue(1);
14368     SP = DAG.getNode(ISD::SUB, DL, MVT::i32, SP, Size);
14369     if (Align)
14370       SP = DAG.getNode(ISD::AND, DL, MVT::i32, SP.getValue(0),
14371                        DAG.getConstant(-(uint64_t)Align, DL, MVT::i32));
14372     Chain = DAG.getCopyToReg(Chain, DL, ARM::SP, SP);
14373     SDValue Ops[2] = { SP, Chain };
14374     return DAG.getMergeValues(Ops, DL);
14375   }
14376 
14377   SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
14378                               DAG.getConstant(2, DL, MVT::i32));
14379 
14380   SDValue Flag;
14381   Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
14382   Flag = Chain.getValue(1);
14383 
14384   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
14385   Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
14386 
14387   SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
14388   Chain = NewSP.getValue(1);
14389 
14390   SDValue Ops[2] = { NewSP, Chain };
14391   return DAG.getMergeValues(Ops, DL);
14392 }
14393 
14394 SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
14395   assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
14396          "Unexpected type for custom-lowering FP_EXTEND");
14397 
14398   RTLIB::Libcall LC;
14399   LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
14400 
14401   SDValue SrcVal = Op.getOperand(0);
14402   return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
14403                      SDLoc(Op)).first;
14404 }
14405 
14406 SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
14407   assert(Op.getOperand(0).getValueType() == MVT::f64 &&
14408          Subtarget->isFPOnlySP() &&
14409          "Unexpected type for custom-lowering FP_ROUND");
14410 
14411   RTLIB::Libcall LC;
14412   LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
14413 
14414   SDValue SrcVal = Op.getOperand(0);
14415   return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
14416                      SDLoc(Op)).first;
14417 }
14418 
14419 void ARMTargetLowering::lowerABS(SDNode *N, SmallVectorImpl<SDValue> &Results,
14420                                  SelectionDAG &DAG) const {
14421   assert(N->getValueType(0) == MVT::i64 && "Unexpected type (!= i64) on ABS.");
14422   MVT HalfT = MVT::i32;
14423   SDLoc dl(N);
14424   SDValue Hi, Lo, Tmp;
14425 
14426   if (!isOperationLegalOrCustom(ISD::ADDCARRY, HalfT) ||
14427       !isOperationLegalOrCustom(ISD::UADDO, HalfT))
14428     return ;
14429 
14430   unsigned OpTypeBits = HalfT.getScalarSizeInBits();
14431   SDVTList VTList = DAG.getVTList(HalfT, MVT::i1);
14432 
14433   Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(0),
14434                    DAG.getConstant(0, dl, HalfT));
14435   Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(0),
14436                    DAG.getConstant(1, dl, HalfT));
14437 
14438   Tmp = DAG.getNode(ISD::SRA, dl, HalfT, Hi,
14439                     DAG.getConstant(OpTypeBits - 1, dl,
14440                     getShiftAmountTy(HalfT, DAG.getDataLayout())));
14441   Lo = DAG.getNode(ISD::UADDO, dl, VTList, Tmp, Lo);
14442   Hi = DAG.getNode(ISD::ADDCARRY, dl, VTList, Tmp, Hi,
14443                    SDValue(Lo.getNode(), 1));
14444   Hi = DAG.getNode(ISD::XOR, dl, HalfT, Tmp, Hi);
14445   Lo = DAG.getNode(ISD::XOR, dl, HalfT, Tmp, Lo);
14446 
14447   Results.push_back(Lo);
14448   Results.push_back(Hi);
14449 }
14450 
14451 bool
14452 ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
14453   // The ARM target isn't yet aware of offsets.
14454   return false;
14455 }
14456 
14457 bool ARM::isBitFieldInvertedMask(unsigned v) {
14458   if (v == 0xffffffff)
14459     return false;
14460 
14461   // there can be 1's on either or both "outsides", all the "inside"
14462   // bits must be 0's
14463   return isShiftedMask_32(~v);
14464 }
14465 
14466 /// isFPImmLegal - Returns true if the target can instruction select the
14467 /// specified FP immediate natively. If false, the legalizer will
14468 /// materialize the FP immediate as a load from a constant pool.
14469 bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
14470                                      bool ForCodeSize) const {
14471   if (!Subtarget->hasVFP3())
14472     return false;
14473   if (VT == MVT::f16 && Subtarget->hasFullFP16())
14474     return ARM_AM::getFP16Imm(Imm) != -1;
14475   if (VT == MVT::f32)
14476     return ARM_AM::getFP32Imm(Imm) != -1;
14477   if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
14478     return ARM_AM::getFP64Imm(Imm) != -1;
14479   return false;
14480 }
14481 
14482 /// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
14483 /// MemIntrinsicNodes.  The associated MachineMemOperands record the alignment
14484 /// specified in the intrinsic calls.
14485 bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
14486                                            const CallInst &I,
14487                                            MachineFunction &MF,
14488                                            unsigned Intrinsic) const {
14489   switch (Intrinsic) {
14490   case Intrinsic::arm_neon_vld1:
14491   case Intrinsic::arm_neon_vld2:
14492   case Intrinsic::arm_neon_vld3:
14493   case Intrinsic::arm_neon_vld4:
14494   case Intrinsic::arm_neon_vld2lane:
14495   case Intrinsic::arm_neon_vld3lane:
14496   case Intrinsic::arm_neon_vld4lane:
14497   case Intrinsic::arm_neon_vld2dup:
14498   case Intrinsic::arm_neon_vld3dup:
14499   case Intrinsic::arm_neon_vld4dup: {
14500     Info.opc = ISD::INTRINSIC_W_CHAIN;
14501     // Conservatively set memVT to the entire set of vectors loaded.
14502     auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
14503     uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
14504     Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
14505     Info.ptrVal = I.getArgOperand(0);
14506     Info.offset = 0;
14507     Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
14508     Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
14509     // volatile loads with NEON intrinsics not supported
14510     Info.flags = MachineMemOperand::MOLoad;
14511     return true;
14512   }
14513   case Intrinsic::arm_neon_vld1x2:
14514   case Intrinsic::arm_neon_vld1x3:
14515   case Intrinsic::arm_neon_vld1x4: {
14516     Info.opc = ISD::INTRINSIC_W_CHAIN;
14517     // Conservatively set memVT to the entire set of vectors loaded.
14518     auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
14519     uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
14520     Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
14521     Info.ptrVal = I.getArgOperand(I.getNumArgOperands() - 1);
14522     Info.offset = 0;
14523     Info.align = 0;
14524     // volatile loads with NEON intrinsics not supported
14525     Info.flags = MachineMemOperand::MOLoad;
14526     return true;
14527   }
14528   case Intrinsic::arm_neon_vst1:
14529   case Intrinsic::arm_neon_vst2:
14530   case Intrinsic::arm_neon_vst3:
14531   case Intrinsic::arm_neon_vst4:
14532   case Intrinsic::arm_neon_vst2lane:
14533   case Intrinsic::arm_neon_vst3lane:
14534   case Intrinsic::arm_neon_vst4lane: {
14535     Info.opc = ISD::INTRINSIC_VOID;
14536     // Conservatively set memVT to the entire set of vectors stored.
14537     auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
14538     unsigned NumElts = 0;
14539     for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
14540       Type *ArgTy = I.getArgOperand(ArgI)->getType();
14541       if (!ArgTy->isVectorTy())
14542         break;
14543       NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
14544     }
14545     Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
14546     Info.ptrVal = I.getArgOperand(0);
14547     Info.offset = 0;
14548     Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
14549     Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
14550     // volatile stores with NEON intrinsics not supported
14551     Info.flags = MachineMemOperand::MOStore;
14552     return true;
14553   }
14554   case Intrinsic::arm_neon_vst1x2:
14555   case Intrinsic::arm_neon_vst1x3:
14556   case Intrinsic::arm_neon_vst1x4: {
14557     Info.opc = ISD::INTRINSIC_VOID;
14558     // Conservatively set memVT to the entire set of vectors stored.
14559     auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
14560     unsigned NumElts = 0;
14561     for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
14562       Type *ArgTy = I.getArgOperand(ArgI)->getType();
14563       if (!ArgTy->isVectorTy())
14564         break;
14565       NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
14566     }
14567     Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
14568     Info.ptrVal = I.getArgOperand(0);
14569     Info.offset = 0;
14570     Info.align = 0;
14571     // volatile stores with NEON intrinsics not supported
14572     Info.flags = MachineMemOperand::MOStore;
14573     return true;
14574   }
14575   case Intrinsic::arm_ldaex:
14576   case Intrinsic::arm_ldrex: {
14577     auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
14578     PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
14579     Info.opc = ISD::INTRINSIC_W_CHAIN;
14580     Info.memVT = MVT::getVT(PtrTy->getElementType());
14581     Info.ptrVal = I.getArgOperand(0);
14582     Info.offset = 0;
14583     Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
14584     Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile;
14585     return true;
14586   }
14587   case Intrinsic::arm_stlex:
14588   case Intrinsic::arm_strex: {
14589     auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
14590     PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
14591     Info.opc = ISD::INTRINSIC_W_CHAIN;
14592     Info.memVT = MVT::getVT(PtrTy->getElementType());
14593     Info.ptrVal = I.getArgOperand(1);
14594     Info.offset = 0;
14595     Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
14596     Info.flags = MachineMemOperand::MOStore | MachineMemOperand::MOVolatile;
14597     return true;
14598   }
14599   case Intrinsic::arm_stlexd:
14600   case Intrinsic::arm_strexd:
14601     Info.opc = ISD::INTRINSIC_W_CHAIN;
14602     Info.memVT = MVT::i64;
14603     Info.ptrVal = I.getArgOperand(2);
14604     Info.offset = 0;
14605     Info.align = 8;
14606     Info.flags = MachineMemOperand::MOStore | MachineMemOperand::MOVolatile;
14607     return true;
14608 
14609   case Intrinsic::arm_ldaexd:
14610   case Intrinsic::arm_ldrexd:
14611     Info.opc = ISD::INTRINSIC_W_CHAIN;
14612     Info.memVT = MVT::i64;
14613     Info.ptrVal = I.getArgOperand(0);
14614     Info.offset = 0;
14615     Info.align = 8;
14616     Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile;
14617     return true;
14618 
14619   default:
14620     break;
14621   }
14622 
14623   return false;
14624 }
14625 
14626 /// Returns true if it is beneficial to convert a load of a constant
14627 /// to just the constant itself.
14628 bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
14629                                                           Type *Ty) const {
14630   assert(Ty->isIntegerTy());
14631 
14632   unsigned Bits = Ty->getPrimitiveSizeInBits();
14633   if (Bits == 0 || Bits > 32)
14634     return false;
14635   return true;
14636 }
14637 
14638 bool ARMTargetLowering::isExtractSubvectorCheap(EVT ResVT, EVT SrcVT,
14639                                                 unsigned Index) const {
14640   if (!isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, ResVT))
14641     return false;
14642 
14643   return (Index == 0 || Index == ResVT.getVectorNumElements());
14644 }
14645 
14646 Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
14647                                         ARM_MB::MemBOpt Domain) const {
14648   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
14649 
14650   // First, if the target has no DMB, see what fallback we can use.
14651   if (!Subtarget->hasDataBarrier()) {
14652     // Some ARMv6 cpus can support data barriers with an mcr instruction.
14653     // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
14654     // here.
14655     if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
14656       Function *MCR = Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
14657       Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
14658                         Builder.getInt32(0), Builder.getInt32(7),
14659                         Builder.getInt32(10), Builder.getInt32(5)};
14660       return Builder.CreateCall(MCR, args);
14661     } else {
14662       // Instead of using barriers, atomic accesses on these subtargets use
14663       // libcalls.
14664       llvm_unreachable("makeDMB on a target so old that it has no barriers");
14665     }
14666   } else {
14667     Function *DMB = Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
14668     // Only a full system barrier exists in the M-class architectures.
14669     Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
14670     Constant *CDomain = Builder.getInt32(Domain);
14671     return Builder.CreateCall(DMB, CDomain);
14672   }
14673 }
14674 
14675 // Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
14676 Instruction *ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
14677                                                  Instruction *Inst,
14678                                                  AtomicOrdering Ord) const {
14679   switch (Ord) {
14680   case AtomicOrdering::NotAtomic:
14681   case AtomicOrdering::Unordered:
14682     llvm_unreachable("Invalid fence: unordered/non-atomic");
14683   case AtomicOrdering::Monotonic:
14684   case AtomicOrdering::Acquire:
14685     return nullptr; // Nothing to do
14686   case AtomicOrdering::SequentiallyConsistent:
14687     if (!Inst->hasAtomicStore())
14688       return nullptr; // Nothing to do
14689     LLVM_FALLTHROUGH;
14690   case AtomicOrdering::Release:
14691   case AtomicOrdering::AcquireRelease:
14692     if (Subtarget->preferISHSTBarriers())
14693       return makeDMB(Builder, ARM_MB::ISHST);
14694     // FIXME: add a comment with a link to documentation justifying this.
14695     else
14696       return makeDMB(Builder, ARM_MB::ISH);
14697   }
14698   llvm_unreachable("Unknown fence ordering in emitLeadingFence");
14699 }
14700 
14701 Instruction *ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
14702                                                   Instruction *Inst,
14703                                                   AtomicOrdering Ord) const {
14704   switch (Ord) {
14705   case AtomicOrdering::NotAtomic:
14706   case AtomicOrdering::Unordered:
14707     llvm_unreachable("Invalid fence: unordered/not-atomic");
14708   case AtomicOrdering::Monotonic:
14709   case AtomicOrdering::Release:
14710     return nullptr; // Nothing to do
14711   case AtomicOrdering::Acquire:
14712   case AtomicOrdering::AcquireRelease:
14713   case AtomicOrdering::SequentiallyConsistent:
14714     return makeDMB(Builder, ARM_MB::ISH);
14715   }
14716   llvm_unreachable("Unknown fence ordering in emitTrailingFence");
14717 }
14718 
14719 // Loads and stores less than 64-bits are already atomic; ones above that
14720 // are doomed anyway, so defer to the default libcall and blame the OS when
14721 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
14722 // anything for those.
14723 bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
14724   unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
14725   return (Size == 64) && !Subtarget->isMClass();
14726 }
14727 
14728 // Loads and stores less than 64-bits are already atomic; ones above that
14729 // are doomed anyway, so defer to the default libcall and blame the OS when
14730 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
14731 // anything for those.
14732 // FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
14733 // guarantee, see DDI0406C ARM architecture reference manual,
14734 // sections A8.8.72-74 LDRD)
14735 TargetLowering::AtomicExpansionKind
14736 ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
14737   unsigned Size = LI->getType()->getPrimitiveSizeInBits();
14738   return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly
14739                                                   : AtomicExpansionKind::None;
14740 }
14741 
14742 // For the real atomic operations, we have ldrex/strex up to 32 bits,
14743 // and up to 64 bits on the non-M profiles
14744 TargetLowering::AtomicExpansionKind
14745 ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
14746   if (AI->isFloatingPointOperation())
14747     return AtomicExpansionKind::CmpXChg;
14748 
14749   unsigned Size = AI->getType()->getPrimitiveSizeInBits();
14750   bool hasAtomicRMW = !Subtarget->isThumb() || Subtarget->hasV8MBaselineOps();
14751   return (Size <= (Subtarget->isMClass() ? 32U : 64U) && hasAtomicRMW)
14752              ? AtomicExpansionKind::LLSC
14753              : AtomicExpansionKind::None;
14754 }
14755 
14756 TargetLowering::AtomicExpansionKind
14757 ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const {
14758   // At -O0, fast-regalloc cannot cope with the live vregs necessary to
14759   // implement cmpxchg without spilling. If the address being exchanged is also
14760   // on the stack and close enough to the spill slot, this can lead to a
14761   // situation where the monitor always gets cleared and the atomic operation
14762   // can never succeed. So at -O0 we need a late-expanded pseudo-inst instead.
14763   bool HasAtomicCmpXchg =
14764       !Subtarget->isThumb() || Subtarget->hasV8MBaselineOps();
14765   if (getTargetMachine().getOptLevel() != 0 && HasAtomicCmpXchg)
14766     return AtomicExpansionKind::LLSC;
14767   return AtomicExpansionKind::None;
14768 }
14769 
14770 bool ARMTargetLowering::shouldInsertFencesForAtomic(
14771     const Instruction *I) const {
14772   return InsertFencesForAtomic;
14773 }
14774 
14775 // This has so far only been implemented for MachO.
14776 bool ARMTargetLowering::useLoadStackGuardNode() const {
14777   return Subtarget->isTargetMachO();
14778 }
14779 
14780 bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
14781                                                   unsigned &Cost) const {
14782   // If we do not have NEON, vector types are not natively supported.
14783   if (!Subtarget->hasNEON())
14784     return false;
14785 
14786   // Floating point values and vector values map to the same register file.
14787   // Therefore, although we could do a store extract of a vector type, this is
14788   // better to leave at float as we have more freedom in the addressing mode for
14789   // those.
14790   if (VectorTy->isFPOrFPVectorTy())
14791     return false;
14792 
14793   // If the index is unknown at compile time, this is very expensive to lower
14794   // and it is not possible to combine the store with the extract.
14795   if (!isa<ConstantInt>(Idx))
14796     return false;
14797 
14798   assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
14799   unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
14800   // We can do a store + vector extract on any vector that fits perfectly in a D
14801   // or Q register.
14802   if (BitWidth == 64 || BitWidth == 128) {
14803     Cost = 0;
14804     return true;
14805   }
14806   return false;
14807 }
14808 
14809 bool ARMTargetLowering::isCheapToSpeculateCttz() const {
14810   return Subtarget->hasV6T2Ops();
14811 }
14812 
14813 bool ARMTargetLowering::isCheapToSpeculateCtlz() const {
14814   return Subtarget->hasV6T2Ops();
14815 }
14816 
14817 bool ARMTargetLowering::shouldExpandShift(SelectionDAG &DAG, SDNode *N) const {
14818   return !Subtarget->optForMinSize();
14819 }
14820 
14821 Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
14822                                          AtomicOrdering Ord) const {
14823   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
14824   Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
14825   bool IsAcquire = isAcquireOrStronger(Ord);
14826 
14827   // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
14828   // intrinsic must return {i32, i32} and we have to recombine them into a
14829   // single i64 here.
14830   if (ValTy->getPrimitiveSizeInBits() == 64) {
14831     Intrinsic::ID Int =
14832         IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
14833     Function *Ldrex = Intrinsic::getDeclaration(M, Int);
14834 
14835     Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
14836     Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
14837 
14838     Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
14839     Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
14840     if (!Subtarget->isLittle())
14841       std::swap (Lo, Hi);
14842     Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
14843     Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
14844     return Builder.CreateOr(
14845         Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
14846   }
14847 
14848   Type *Tys[] = { Addr->getType() };
14849   Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
14850   Function *Ldrex = Intrinsic::getDeclaration(M, Int, Tys);
14851 
14852   return Builder.CreateTruncOrBitCast(
14853       Builder.CreateCall(Ldrex, Addr),
14854       cast<PointerType>(Addr->getType())->getElementType());
14855 }
14856 
14857 void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
14858     IRBuilder<> &Builder) const {
14859   if (!Subtarget->hasV7Ops())
14860     return;
14861   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
14862   Builder.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
14863 }
14864 
14865 Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
14866                                                Value *Addr,
14867                                                AtomicOrdering Ord) const {
14868   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
14869   bool IsRelease = isReleaseOrStronger(Ord);
14870 
14871   // Since the intrinsics must have legal type, the i64 intrinsics take two
14872   // parameters: "i32, i32". We must marshal Val into the appropriate form
14873   // before the call.
14874   if (Val->getType()->getPrimitiveSizeInBits() == 64) {
14875     Intrinsic::ID Int =
14876         IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
14877     Function *Strex = Intrinsic::getDeclaration(M, Int);
14878     Type *Int32Ty = Type::getInt32Ty(M->getContext());
14879 
14880     Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
14881     Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
14882     if (!Subtarget->isLittle())
14883       std::swap(Lo, Hi);
14884     Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
14885     return Builder.CreateCall(Strex, {Lo, Hi, Addr});
14886   }
14887 
14888   Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
14889   Type *Tys[] = { Addr->getType() };
14890   Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
14891 
14892   return Builder.CreateCall(
14893       Strex, {Builder.CreateZExtOrBitCast(
14894                   Val, Strex->getFunctionType()->getParamType(0)),
14895               Addr});
14896 }
14897 
14898 
14899 bool ARMTargetLowering::alignLoopsWithOptSize() const {
14900   return Subtarget->isMClass();
14901 }
14902 
14903 /// A helper function for determining the number of interleaved accesses we
14904 /// will generate when lowering accesses of the given type.
14905 unsigned
14906 ARMTargetLowering::getNumInterleavedAccesses(VectorType *VecTy,
14907                                              const DataLayout &DL) const {
14908   return (DL.getTypeSizeInBits(VecTy) + 127) / 128;
14909 }
14910 
14911 bool ARMTargetLowering::isLegalInterleavedAccessType(
14912     VectorType *VecTy, const DataLayout &DL) const {
14913 
14914   unsigned VecSize = DL.getTypeSizeInBits(VecTy);
14915   unsigned ElSize = DL.getTypeSizeInBits(VecTy->getElementType());
14916 
14917   // Ensure the vector doesn't have f16 elements. Even though we could do an
14918   // i16 vldN, we can't hold the f16 vectors and will end up converting via
14919   // f32.
14920   if (VecTy->getElementType()->isHalfTy())
14921     return false;
14922 
14923   // Ensure the number of vector elements is greater than 1.
14924   if (VecTy->getNumElements() < 2)
14925     return false;
14926 
14927   // Ensure the element type is legal.
14928   if (ElSize != 8 && ElSize != 16 && ElSize != 32)
14929     return false;
14930 
14931   // Ensure the total vector size is 64 or a multiple of 128. Types larger than
14932   // 128 will be split into multiple interleaved accesses.
14933   return VecSize == 64 || VecSize % 128 == 0;
14934 }
14935 
14936 /// Lower an interleaved load into a vldN intrinsic.
14937 ///
14938 /// E.g. Lower an interleaved load (Factor = 2):
14939 ///        %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
14940 ///        %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6>  ; Extract even elements
14941 ///        %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7>  ; Extract odd elements
14942 ///
14943 ///      Into:
14944 ///        %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
14945 ///        %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
14946 ///        %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
14947 bool ARMTargetLowering::lowerInterleavedLoad(
14948     LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
14949     ArrayRef<unsigned> Indices, unsigned Factor) const {
14950   assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
14951          "Invalid interleave factor");
14952   assert(!Shuffles.empty() && "Empty shufflevector input");
14953   assert(Shuffles.size() == Indices.size() &&
14954          "Unmatched number of shufflevectors and indices");
14955 
14956   VectorType *VecTy = Shuffles[0]->getType();
14957   Type *EltTy = VecTy->getVectorElementType();
14958 
14959   const DataLayout &DL = LI->getModule()->getDataLayout();
14960 
14961   // Skip if we do not have NEON and skip illegal vector types. We can
14962   // "legalize" wide vector types into multiple interleaved accesses as long as
14963   // the vector types are divisible by 128.
14964   if (!Subtarget->hasNEON() || !isLegalInterleavedAccessType(VecTy, DL))
14965     return false;
14966 
14967   unsigned NumLoads = getNumInterleavedAccesses(VecTy, DL);
14968 
14969   // A pointer vector can not be the return type of the ldN intrinsics. Need to
14970   // load integer vectors first and then convert to pointer vectors.
14971   if (EltTy->isPointerTy())
14972     VecTy =
14973         VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
14974 
14975   IRBuilder<> Builder(LI);
14976 
14977   // The base address of the load.
14978   Value *BaseAddr = LI->getPointerOperand();
14979 
14980   if (NumLoads > 1) {
14981     // If we're going to generate more than one load, reset the sub-vector type
14982     // to something legal.
14983     VecTy = VectorType::get(VecTy->getVectorElementType(),
14984                             VecTy->getVectorNumElements() / NumLoads);
14985 
14986     // We will compute the pointer operand of each load from the original base
14987     // address using GEPs. Cast the base address to a pointer to the scalar
14988     // element type.
14989     BaseAddr = Builder.CreateBitCast(
14990         BaseAddr, VecTy->getVectorElementType()->getPointerTo(
14991                       LI->getPointerAddressSpace()));
14992   }
14993 
14994   assert(isTypeLegal(EVT::getEVT(VecTy)) && "Illegal vldN vector type!");
14995 
14996   Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
14997   Type *Tys[] = {VecTy, Int8Ptr};
14998   static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
14999                                             Intrinsic::arm_neon_vld3,
15000                                             Intrinsic::arm_neon_vld4};
15001   Function *VldnFunc =
15002       Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
15003 
15004   // Holds sub-vectors extracted from the load intrinsic return values. The
15005   // sub-vectors are associated with the shufflevector instructions they will
15006   // replace.
15007   DenseMap<ShuffleVectorInst *, SmallVector<Value *, 4>> SubVecs;
15008 
15009   for (unsigned LoadCount = 0; LoadCount < NumLoads; ++LoadCount) {
15010     // If we're generating more than one load, compute the base address of
15011     // subsequent loads as an offset from the previous.
15012     if (LoadCount > 0)
15013       BaseAddr =
15014           Builder.CreateConstGEP1_32(VecTy->getVectorElementType(), BaseAddr,
15015                                      VecTy->getVectorNumElements() * Factor);
15016 
15017     SmallVector<Value *, 2> Ops;
15018     Ops.push_back(Builder.CreateBitCast(BaseAddr, Int8Ptr));
15019     Ops.push_back(Builder.getInt32(LI->getAlignment()));
15020 
15021     CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
15022 
15023     // Replace uses of each shufflevector with the corresponding vector loaded
15024     // by ldN.
15025     for (unsigned i = 0; i < Shuffles.size(); i++) {
15026       ShuffleVectorInst *SV = Shuffles[i];
15027       unsigned Index = Indices[i];
15028 
15029       Value *SubVec = Builder.CreateExtractValue(VldN, Index);
15030 
15031       // Convert the integer vector to pointer vector if the element is pointer.
15032       if (EltTy->isPointerTy())
15033         SubVec = Builder.CreateIntToPtr(
15034             SubVec, VectorType::get(SV->getType()->getVectorElementType(),
15035                                     VecTy->getVectorNumElements()));
15036 
15037       SubVecs[SV].push_back(SubVec);
15038     }
15039   }
15040 
15041   // Replace uses of the shufflevector instructions with the sub-vectors
15042   // returned by the load intrinsic. If a shufflevector instruction is
15043   // associated with more than one sub-vector, those sub-vectors will be
15044   // concatenated into a single wide vector.
15045   for (ShuffleVectorInst *SVI : Shuffles) {
15046     auto &SubVec = SubVecs[SVI];
15047     auto *WideVec =
15048         SubVec.size() > 1 ? concatenateVectors(Builder, SubVec) : SubVec[0];
15049     SVI->replaceAllUsesWith(WideVec);
15050   }
15051 
15052   return true;
15053 }
15054 
15055 /// Lower an interleaved store into a vstN intrinsic.
15056 ///
15057 /// E.g. Lower an interleaved store (Factor = 3):
15058 ///        %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
15059 ///                                  <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
15060 ///        store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
15061 ///
15062 ///      Into:
15063 ///        %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
15064 ///        %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
15065 ///        %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
15066 ///        call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
15067 ///
15068 /// Note that the new shufflevectors will be removed and we'll only generate one
15069 /// vst3 instruction in CodeGen.
15070 ///
15071 /// Example for a more general valid mask (Factor 3). Lower:
15072 ///        %i.vec = shuffle <32 x i32> %v0, <32 x i32> %v1,
15073 ///                 <4, 32, 16, 5, 33, 17, 6, 34, 18, 7, 35, 19>
15074 ///        store <12 x i32> %i.vec, <12 x i32>* %ptr
15075 ///
15076 ///      Into:
15077 ///        %sub.v0 = shuffle <32 x i32> %v0, <32 x i32> v1, <4, 5, 6, 7>
15078 ///        %sub.v1 = shuffle <32 x i32> %v0, <32 x i32> v1, <32, 33, 34, 35>
15079 ///        %sub.v2 = shuffle <32 x i32> %v0, <32 x i32> v1, <16, 17, 18, 19>
15080 ///        call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
15081 bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
15082                                               ShuffleVectorInst *SVI,
15083                                               unsigned Factor) const {
15084   assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
15085          "Invalid interleave factor");
15086 
15087   VectorType *VecTy = SVI->getType();
15088   assert(VecTy->getVectorNumElements() % Factor == 0 &&
15089          "Invalid interleaved store");
15090 
15091   unsigned LaneLen = VecTy->getVectorNumElements() / Factor;
15092   Type *EltTy = VecTy->getVectorElementType();
15093   VectorType *SubVecTy = VectorType::get(EltTy, LaneLen);
15094 
15095   const DataLayout &DL = SI->getModule()->getDataLayout();
15096 
15097   // Skip if we do not have NEON and skip illegal vector types. We can
15098   // "legalize" wide vector types into multiple interleaved accesses as long as
15099   // the vector types are divisible by 128.
15100   if (!Subtarget->hasNEON() || !isLegalInterleavedAccessType(SubVecTy, DL))
15101     return false;
15102 
15103   unsigned NumStores = getNumInterleavedAccesses(SubVecTy, DL);
15104 
15105   Value *Op0 = SVI->getOperand(0);
15106   Value *Op1 = SVI->getOperand(1);
15107   IRBuilder<> Builder(SI);
15108 
15109   // StN intrinsics don't support pointer vectors as arguments. Convert pointer
15110   // vectors to integer vectors.
15111   if (EltTy->isPointerTy()) {
15112     Type *IntTy = DL.getIntPtrType(EltTy);
15113 
15114     // Convert to the corresponding integer vector.
15115     Type *IntVecTy =
15116         VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
15117     Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
15118     Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
15119 
15120     SubVecTy = VectorType::get(IntTy, LaneLen);
15121   }
15122 
15123   // The base address of the store.
15124   Value *BaseAddr = SI->getPointerOperand();
15125 
15126   if (NumStores > 1) {
15127     // If we're going to generate more than one store, reset the lane length
15128     // and sub-vector type to something legal.
15129     LaneLen /= NumStores;
15130     SubVecTy = VectorType::get(SubVecTy->getVectorElementType(), LaneLen);
15131 
15132     // We will compute the pointer operand of each store from the original base
15133     // address using GEPs. Cast the base address to a pointer to the scalar
15134     // element type.
15135     BaseAddr = Builder.CreateBitCast(
15136         BaseAddr, SubVecTy->getVectorElementType()->getPointerTo(
15137                       SI->getPointerAddressSpace()));
15138   }
15139 
15140   assert(isTypeLegal(EVT::getEVT(SubVecTy)) && "Illegal vstN vector type!");
15141 
15142   auto Mask = SVI->getShuffleMask();
15143 
15144   Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
15145   Type *Tys[] = {Int8Ptr, SubVecTy};
15146   static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
15147                                              Intrinsic::arm_neon_vst3,
15148                                              Intrinsic::arm_neon_vst4};
15149 
15150   for (unsigned StoreCount = 0; StoreCount < NumStores; ++StoreCount) {
15151     // If we generating more than one store, we compute the base address of
15152     // subsequent stores as an offset from the previous.
15153     if (StoreCount > 0)
15154       BaseAddr = Builder.CreateConstGEP1_32(SubVecTy->getVectorElementType(),
15155                                             BaseAddr, LaneLen * Factor);
15156 
15157     SmallVector<Value *, 6> Ops;
15158     Ops.push_back(Builder.CreateBitCast(BaseAddr, Int8Ptr));
15159 
15160     Function *VstNFunc =
15161         Intrinsic::getDeclaration(SI->getModule(), StoreInts[Factor - 2], Tys);
15162 
15163     // Split the shufflevector operands into sub vectors for the new vstN call.
15164     for (unsigned i = 0; i < Factor; i++) {
15165       unsigned IdxI = StoreCount * LaneLen * Factor + i;
15166       if (Mask[IdxI] >= 0) {
15167         Ops.push_back(Builder.CreateShuffleVector(
15168             Op0, Op1, createSequentialMask(Builder, Mask[IdxI], LaneLen, 0)));
15169       } else {
15170         unsigned StartMask = 0;
15171         for (unsigned j = 1; j < LaneLen; j++) {
15172           unsigned IdxJ = StoreCount * LaneLen * Factor + j;
15173           if (Mask[IdxJ * Factor + IdxI] >= 0) {
15174             StartMask = Mask[IdxJ * Factor + IdxI] - IdxJ;
15175             break;
15176           }
15177         }
15178         // Note: If all elements in a chunk are undefs, StartMask=0!
15179         // Note: Filling undef gaps with random elements is ok, since
15180         // those elements were being written anyway (with undefs).
15181         // In the case of all undefs we're defaulting to using elems from 0
15182         // Note: StartMask cannot be negative, it's checked in
15183         // isReInterleaveMask
15184         Ops.push_back(Builder.CreateShuffleVector(
15185             Op0, Op1, createSequentialMask(Builder, StartMask, LaneLen, 0)));
15186       }
15187     }
15188 
15189     Ops.push_back(Builder.getInt32(SI->getAlignment()));
15190     Builder.CreateCall(VstNFunc, Ops);
15191   }
15192   return true;
15193 }
15194 
15195 enum HABaseType {
15196   HA_UNKNOWN = 0,
15197   HA_FLOAT,
15198   HA_DOUBLE,
15199   HA_VECT64,
15200   HA_VECT128
15201 };
15202 
15203 static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
15204                                    uint64_t &Members) {
15205   if (auto *ST = dyn_cast<StructType>(Ty)) {
15206     for (unsigned i = 0; i < ST->getNumElements(); ++i) {
15207       uint64_t SubMembers = 0;
15208       if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
15209         return false;
15210       Members += SubMembers;
15211     }
15212   } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
15213     uint64_t SubMembers = 0;
15214     if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
15215       return false;
15216     Members += SubMembers * AT->getNumElements();
15217   } else if (Ty->isFloatTy()) {
15218     if (Base != HA_UNKNOWN && Base != HA_FLOAT)
15219       return false;
15220     Members = 1;
15221     Base = HA_FLOAT;
15222   } else if (Ty->isDoubleTy()) {
15223     if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
15224       return false;
15225     Members = 1;
15226     Base = HA_DOUBLE;
15227   } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
15228     Members = 1;
15229     switch (Base) {
15230     case HA_FLOAT:
15231     case HA_DOUBLE:
15232       return false;
15233     case HA_VECT64:
15234       return VT->getBitWidth() == 64;
15235     case HA_VECT128:
15236       return VT->getBitWidth() == 128;
15237     case HA_UNKNOWN:
15238       switch (VT->getBitWidth()) {
15239       case 64:
15240         Base = HA_VECT64;
15241         return true;
15242       case 128:
15243         Base = HA_VECT128;
15244         return true;
15245       default:
15246         return false;
15247       }
15248     }
15249   }
15250 
15251   return (Members > 0 && Members <= 4);
15252 }
15253 
15254 /// Return the correct alignment for the current calling convention.
15255 unsigned
15256 ARMTargetLowering::getABIAlignmentForCallingConv(Type *ArgTy,
15257                                                  DataLayout DL) const {
15258   if (!ArgTy->isVectorTy())
15259     return DL.getABITypeAlignment(ArgTy);
15260 
15261   // Avoid over-aligning vector parameters. It would require realigning the
15262   // stack and waste space for no real benefit.
15263   return std::min(DL.getABITypeAlignment(ArgTy), DL.getStackAlignment());
15264 }
15265 
15266 /// Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
15267 /// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
15268 /// passing according to AAPCS rules.
15269 bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
15270     Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
15271   if (getEffectiveCallingConv(CallConv, isVarArg) !=
15272       CallingConv::ARM_AAPCS_VFP)
15273     return false;
15274 
15275   HABaseType Base = HA_UNKNOWN;
15276   uint64_t Members = 0;
15277   bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
15278   LLVM_DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
15279 
15280   bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
15281   return IsHA || IsIntArray;
15282 }
15283 
15284 unsigned ARMTargetLowering::getExceptionPointerRegister(
15285     const Constant *PersonalityFn) const {
15286   // Platforms which do not use SjLj EH may return values in these registers
15287   // via the personality function.
15288   return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0;
15289 }
15290 
15291 unsigned ARMTargetLowering::getExceptionSelectorRegister(
15292     const Constant *PersonalityFn) const {
15293   // Platforms which do not use SjLj EH may return values in these registers
15294   // via the personality function.
15295   return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1;
15296 }
15297 
15298 void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
15299   // Update IsSplitCSR in ARMFunctionInfo.
15300   ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>();
15301   AFI->setIsSplitCSR(true);
15302 }
15303 
15304 void ARMTargetLowering::insertCopiesSplitCSR(
15305     MachineBasicBlock *Entry,
15306     const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
15307   const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
15308   const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
15309   if (!IStart)
15310     return;
15311 
15312   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
15313   MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
15314   MachineBasicBlock::iterator MBBI = Entry->begin();
15315   for (const MCPhysReg *I = IStart; *I; ++I) {
15316     const TargetRegisterClass *RC = nullptr;
15317     if (ARM::GPRRegClass.contains(*I))
15318       RC = &ARM::GPRRegClass;
15319     else if (ARM::DPRRegClass.contains(*I))
15320       RC = &ARM::DPRRegClass;
15321     else
15322       llvm_unreachable("Unexpected register class in CSRsViaCopy!");
15323 
15324     unsigned NewVR = MRI->createVirtualRegister(RC);
15325     // Create copy from CSR to a virtual register.
15326     // FIXME: this currently does not emit CFI pseudo-instructions, it works
15327     // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
15328     // nounwind. If we want to generalize this later, we may need to emit
15329     // CFI pseudo-instructions.
15330     assert(Entry->getParent()->getFunction().hasFnAttribute(
15331                Attribute::NoUnwind) &&
15332            "Function should be nounwind in insertCopiesSplitCSR!");
15333     Entry->addLiveIn(*I);
15334     BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
15335         .addReg(*I);
15336 
15337     // Insert the copy-back instructions right before the terminator.
15338     for (auto *Exit : Exits)
15339       BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
15340               TII->get(TargetOpcode::COPY), *I)
15341           .addReg(NewVR);
15342   }
15343 }
15344 
15345 void ARMTargetLowering::finalizeLowering(MachineFunction &MF) const {
15346   MF.getFrameInfo().computeMaxCallFrameSize(MF);
15347   TargetLoweringBase::finalizeLowering(MF);
15348 }
15349