1 //===- ARMISelLowering.h - ARM DAG Lowering Interface -----------*- C++ -*-===//
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 #ifndef LLVM_LIB_TARGET_ARM_ARMISELLOWERING_H
15 #define LLVM_LIB_TARGET_ARM_ARMISELLOWERING_H
16 
17 #include "MCTargetDesc/ARMBaseInfo.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/CodeGen/ISDOpcodes.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/SelectionDAGNodes.h"
24 #include "llvm/CodeGen/TargetLowering.h"
25 #include "llvm/CodeGen/ValueTypes.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/CallingConv.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/IRBuilder.h"
30 #include "llvm/IR/InlineAsm.h"
31 #include "llvm/Support/CodeGen.h"
32 #include "llvm/Support/MachineValueType.h"
33 #include <utility>
34 
35 namespace llvm {
36 
37 class ARMSubtarget;
38 class DataLayout;
39 class FastISel;
40 class FunctionLoweringInfo;
41 class GlobalValue;
42 class InstrItineraryData;
43 class Instruction;
44 class MachineBasicBlock;
45 class MachineInstr;
46 class SelectionDAG;
47 class TargetLibraryInfo;
48 class TargetMachine;
49 class TargetRegisterInfo;
50 class VectorType;
51 
52   namespace ARMISD {
53 
54     // ARM Specific DAG Nodes
55     enum NodeType : unsigned {
56       // Start the numbering where the builtin ops and target ops leave off.
57       FIRST_NUMBER = ISD::BUILTIN_OP_END,
58 
59       Wrapper,      // Wrapper - A wrapper node for TargetConstantPool,
60                     // TargetExternalSymbol, and TargetGlobalAddress.
61       WrapperPIC,   // WrapperPIC - A wrapper node for TargetGlobalAddress in
62                     // PIC mode.
63       WrapperJT,    // WrapperJT - A wrapper node for TargetJumpTable
64 
65       // Add pseudo op to model memcpy for struct byval.
66       COPY_STRUCT_BYVAL,
67 
68       CALL,         // Function call.
69       CALL_PRED,    // Function call that's predicable.
70       CALL_NOLINK,  // Function call with branch not branch-and-link.
71       BRCOND,       // Conditional branch.
72       BR_JT,        // Jumptable branch.
73       BR2_JT,       // Jumptable branch (2 level - jumptable entry is a jump).
74       RET_FLAG,     // Return with a flag operand.
75       INTRET_FLAG,  // Interrupt return with an LR-offset and a flag operand.
76 
77       PIC_ADD,      // Add with a PC operand and a PIC label.
78 
79       ASRL,         // MVE long arithmetic shift right.
80       LSRL,         // MVE long shift right.
81       LSLL,         // MVE long shift left.
82 
83       CMP,          // ARM compare instructions.
84       CMN,          // ARM CMN instructions.
85       CMPZ,         // ARM compare that sets only Z flag.
86       CMPFP,        // ARM VFP compare instruction, sets FPSCR.
87       CMPFPE,       // ARM VFP signalling compare instruction, sets FPSCR.
88       CMPFPw0,      // ARM VFP compare against zero instruction, sets FPSCR.
89       CMPFPEw0,     // ARM VFP signalling compare against zero instruction, sets FPSCR.
90       FMSTAT,       // ARM fmstat instruction.
91 
92       CMOV,         // ARM conditional move instructions.
93       SUBS,         // Flag-setting subtraction.
94 
95       SSAT,         // Signed saturation
96       USAT,         // Unsigned saturation
97 
98       BCC_i64,
99 
100       SRL_FLAG,     // V,Flag = srl_flag X -> srl X, 1 + save carry out.
101       SRA_FLAG,     // V,Flag = sra_flag X -> sra X, 1 + save carry out.
102       RRX,          // V = RRX X, Flag     -> srl X, 1 + shift in carry flag.
103 
104       ADDC,         // Add with carry
105       ADDE,         // Add using carry
106       SUBC,         // Sub with carry
107       SUBE,         // Sub using carry
108       LSLS,         // Shift left producing carry
109 
110       VMOVRRD,      // double to two gprs.
111       VMOVDRR,      // Two gprs to double.
112       VMOVSR,       // move gpr to single, used for f32 literal constructed in a gpr
113 
114       EH_SJLJ_SETJMP,         // SjLj exception handling setjmp.
115       EH_SJLJ_LONGJMP,        // SjLj exception handling longjmp.
116       EH_SJLJ_SETUP_DISPATCH, // SjLj exception handling setup_dispatch.
117 
118       TC_RETURN,    // Tail call return pseudo.
119 
120       THREAD_POINTER,
121 
122       DYN_ALLOC,    // Dynamic allocation on the stack.
123 
124       MEMBARRIER_MCR, // Memory barrier (MCR)
125 
126       PRELOAD,      // Preload
127 
128       WIN__CHKSTK,  // Windows' __chkstk call to do stack probing.
129       WIN__DBZCHK,  // Windows' divide by zero check
130 
131       WLS,          // Low-overhead loops, While Loop Start
132       LOOP_DEC,     // Really a part of LE, performs the sub
133       LE,           // Low-overhead loops, Loop End
134 
135       PREDICATE_CAST, // Predicate cast for MVE i1 types
136       VECTOR_REG_CAST, // Reinterpret the current contents of a vector register
137 
138       VCMP,         // Vector compare.
139       VCMPZ,        // Vector compare to zero.
140       VTST,         // Vector test bits.
141 
142       // Vector shift by vector
143       VSHLs,        // ...left/right by signed
144       VSHLu,        // ...left/right by unsigned
145 
146       // Vector shift by immediate:
147       VSHLIMM,      // ...left
148       VSHRsIMM,     // ...right (signed)
149       VSHRuIMM,     // ...right (unsigned)
150 
151       // Vector rounding shift by immediate:
152       VRSHRsIMM,    // ...right (signed)
153       VRSHRuIMM,    // ...right (unsigned)
154       VRSHRNIMM,    // ...right narrow
155 
156       // Vector saturating shift by immediate:
157       VQSHLsIMM,    // ...left (signed)
158       VQSHLuIMM,    // ...left (unsigned)
159       VQSHLsuIMM,   // ...left (signed to unsigned)
160       VQSHRNsIMM,   // ...right narrow (signed)
161       VQSHRNuIMM,   // ...right narrow (unsigned)
162       VQSHRNsuIMM,  // ...right narrow (signed to unsigned)
163 
164       // Vector saturating rounding shift by immediate:
165       VQRSHRNsIMM,  // ...right narrow (signed)
166       VQRSHRNuIMM,  // ...right narrow (unsigned)
167       VQRSHRNsuIMM, // ...right narrow (signed to unsigned)
168 
169       // Vector shift and insert:
170       VSLIIMM,      // ...left
171       VSRIIMM,      // ...right
172 
173       // Vector get lane (VMOV scalar to ARM core register)
174       // (These are used for 8- and 16-bit element types only.)
175       VGETLANEu,    // zero-extend vector extract element
176       VGETLANEs,    // sign-extend vector extract element
177 
178       // Vector move immediate and move negated immediate:
179       VMOVIMM,
180       VMVNIMM,
181 
182       // Vector move f32 immediate:
183       VMOVFPIMM,
184 
185       // Move H <-> R, clearing top 16 bits
186       VMOVrh,
187       VMOVhr,
188 
189       // Vector duplicate:
190       VDUP,
191       VDUPLANE,
192 
193       // Vector shuffles:
194       VEXT,         // extract
195       VREV64,       // reverse elements within 64-bit doublewords
196       VREV32,       // reverse elements within 32-bit words
197       VREV16,       // reverse elements within 16-bit halfwords
198       VZIP,         // zip (interleave)
199       VUZP,         // unzip (deinterleave)
200       VTRN,         // transpose
201       VTBL1,        // 1-register shuffle with mask
202       VTBL2,        // 2-register shuffle with mask
203       VMOVN,        // MVE vmovn
204 
205       // Vector multiply long:
206       VMULLs,       // ...signed
207       VMULLu,       // ...unsigned
208 
209       // MVE reductions
210       VADDVs,       // sign- or zero-extend the elements of a vector to i32,
211       VADDVu,       //   add them all together, and return an i32 of their sum
212       VADDLVs,      // sign- or zero-extend elements to i64 and sum, returning
213       VADDLVu,      //   the low and high 32-bit halves of the sum
214       VADDLVAs,     // same as VADDLV[su] but also add an input accumulator
215       VADDLVAu,     //   provided as low and high halves
216       VADDLVps,     // same as VADDLVs but with a v4i1 predicate mask
217       VADDLVpu,     // same as VADDLVu but with a v4i1 predicate mask
218       VADDLVAps,    // same as VADDLVps but with a v4i1 predicate mask
219       VADDLVApu,    // same as VADDLVpu but with a v4i1 predicate mask
220       VMLAVs,
221       VMLAVu,
222       VMLALVs,
223       VMLALVu,
224       VMLALVAs,
225       VMLALVAu,
226 
227       SMULWB,       // Signed multiply word by half word, bottom
228       SMULWT,       // Signed multiply word by half word, top
229       UMLAL,        // 64bit Unsigned Accumulate Multiply
230       SMLAL,        // 64bit Signed Accumulate Multiply
231       UMAAL,        // 64-bit Unsigned Accumulate Accumulate Multiply
232       SMLALBB,      // 64-bit signed accumulate multiply bottom, bottom 16
233       SMLALBT,      // 64-bit signed accumulate multiply bottom, top 16
234       SMLALTB,      // 64-bit signed accumulate multiply top, bottom 16
235       SMLALTT,      // 64-bit signed accumulate multiply top, top 16
236       SMLALD,       // Signed multiply accumulate long dual
237       SMLALDX,      // Signed multiply accumulate long dual exchange
238       SMLSLD,       // Signed multiply subtract long dual
239       SMLSLDX,      // Signed multiply subtract long dual exchange
240       SMMLAR,       // Signed multiply long, round and add
241       SMMLSR,       // Signed multiply long, subtract and round
242 
243       // Single Lane QADD8 and QADD16. Only the bottom lane. That's what the b stands for.
244       QADD8b,
245       QSUB8b,
246       QADD16b,
247       QSUB16b,
248 
249       // Operands of the standard BUILD_VECTOR node are not legalized, which
250       // is fine if BUILD_VECTORs are always lowered to shuffles or other
251       // operations, but for ARM some BUILD_VECTORs are legal as-is and their
252       // operands need to be legalized.  Define an ARM-specific version of
253       // BUILD_VECTOR for this purpose.
254       BUILD_VECTOR,
255 
256       // Bit-field insert
257       BFI,
258 
259       // Vector OR with immediate
260       VORRIMM,
261       // Vector AND with NOT of immediate
262       VBICIMM,
263 
264       // Vector bitwise select
265       VBSL,
266 
267       // Pseudo-instruction representing a memory copy using ldm/stm
268       // instructions.
269       MEMCPY,
270 
271       // V8.1MMainline condition select
272       CSINV, // Conditional select invert.
273       CSNEG, // Conditional select negate.
274       CSINC, // Conditional select increment.
275 
276       // Vector load N-element structure to all lanes:
277       VLD1DUP = ISD::FIRST_TARGET_MEMORY_OPCODE,
278       VLD2DUP,
279       VLD3DUP,
280       VLD4DUP,
281 
282       // NEON loads with post-increment base updates:
283       VLD1_UPD,
284       VLD2_UPD,
285       VLD3_UPD,
286       VLD4_UPD,
287       VLD2LN_UPD,
288       VLD3LN_UPD,
289       VLD4LN_UPD,
290       VLD1DUP_UPD,
291       VLD2DUP_UPD,
292       VLD3DUP_UPD,
293       VLD4DUP_UPD,
294 
295       // NEON stores with post-increment base updates:
296       VST1_UPD,
297       VST2_UPD,
298       VST3_UPD,
299       VST4_UPD,
300       VST2LN_UPD,
301       VST3LN_UPD,
302       VST4LN_UPD,
303 
304       // Load/Store of dual registers
305       LDRD,
306       STRD
307     };
308 
309   } // end namespace ARMISD
310 
311   /// Define some predicates that are used for node matching.
312   namespace ARM {
313 
314     bool isBitFieldInvertedMask(unsigned v);
315 
316   } // end namespace ARM
317 
318   //===--------------------------------------------------------------------===//
319   //  ARMTargetLowering - ARM Implementation of the TargetLowering interface
320 
321   class ARMTargetLowering : public TargetLowering {
322   public:
323     explicit ARMTargetLowering(const TargetMachine &TM,
324                                const ARMSubtarget &STI);
325 
326     unsigned getJumpTableEncoding() const override;
327     bool useSoftFloat() const override;
328 
329     SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
330 
331     /// ReplaceNodeResults - Replace the results of node with an illegal result
332     /// type with new values built out of custom code.
333     void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue>&Results,
334                             SelectionDAG &DAG) const override;
335 
336     const char *getTargetNodeName(unsigned Opcode) const override;
337 
338     bool isSelectSupported(SelectSupportKind Kind) const override {
339       // ARM does not support scalar condition selects on vectors.
340       return (Kind != ScalarCondVectorVal);
341     }
342 
343     bool isReadOnly(const GlobalValue *GV) const;
344 
345     /// getSetCCResultType - Return the value type to use for ISD::SETCC.
346     EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
347                            EVT VT) const override;
348 
349     MachineBasicBlock *
350     EmitInstrWithCustomInserter(MachineInstr &MI,
351                                 MachineBasicBlock *MBB) const override;
352 
353     void AdjustInstrPostInstrSelection(MachineInstr &MI,
354                                        SDNode *Node) const override;
355 
356     SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const;
357     SDValue PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const;
358     SDValue PerformCMOVToBFICombine(SDNode *N, SelectionDAG &DAG) const;
359     SDValue PerformIntrinsicCombine(SDNode *N, DAGCombinerInfo &DCI) const;
360     SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
361 
362     bool SimplifyDemandedBitsForTargetNode(SDValue Op,
363                                            const APInt &OriginalDemandedBits,
364                                            const APInt &OriginalDemandedElts,
365                                            KnownBits &Known,
366                                            TargetLoweringOpt &TLO,
367                                            unsigned Depth) const override;
368 
369     bool isDesirableToTransformToIntegerOp(unsigned Opc, EVT VT) const override;
370 
371     /// allowsMisalignedMemoryAccesses - Returns true if the target allows
372     /// unaligned memory accesses of the specified type. Returns whether it
373     /// is "fast" by reference in the second argument.
374     bool allowsMisalignedMemoryAccesses(EVT VT, unsigned AddrSpace,
375                                         unsigned Align,
376                                         MachineMemOperand::Flags Flags,
377                                         bool *Fast) const override;
378 
379     EVT getOptimalMemOpType(const MemOp &Op,
380                             const AttributeList &FuncAttributes) const override;
381 
382     bool isTruncateFree(Type *SrcTy, Type *DstTy) const override;
383     bool isTruncateFree(EVT SrcVT, EVT DstVT) const override;
384     bool isZExtFree(SDValue Val, EVT VT2) const override;
385     bool shouldSinkOperands(Instruction *I,
386                             SmallVectorImpl<Use *> &Ops) const override;
387 
388     bool isFNegFree(EVT VT) const override;
389 
390     bool isVectorLoadExtDesirable(SDValue ExtVal) const override;
391 
392     bool allowTruncateForTailCall(Type *Ty1, Type *Ty2) const override;
393 
394 
395     /// isLegalAddressingMode - Return true if the addressing mode represented
396     /// by AM is legal for this target, for a load/store of the specified type.
397     bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM,
398                                Type *Ty, unsigned AS,
399                                Instruction *I = nullptr) const override;
400 
401     /// getScalingFactorCost - Return the cost of the scaling used in
402     /// addressing mode represented by AM.
403     /// If the AM is supported, the return value must be >= 0.
404     /// If the AM is not supported, the return value must be negative.
405     int getScalingFactorCost(const DataLayout &DL, const AddrMode &AM, Type *Ty,
406                              unsigned AS) const override;
407 
408     bool isLegalT2ScaledAddressingMode(const AddrMode &AM, EVT VT) const;
409 
410     /// Returns true if the addressing mode representing by AM is legal
411     /// for the Thumb1 target, for a load/store of the specified type.
412     bool isLegalT1ScaledAddressingMode(const AddrMode &AM, EVT VT) const;
413 
414     /// isLegalICmpImmediate - Return true if the specified immediate is legal
415     /// icmp immediate, that is the target has icmp instructions which can
416     /// compare a register against the immediate without having to materialize
417     /// the immediate into a register.
418     bool isLegalICmpImmediate(int64_t Imm) const override;
419 
420     /// isLegalAddImmediate - Return true if the specified immediate is legal
421     /// add immediate, that is the target has add instructions which can
422     /// add a register and the immediate without having to materialize
423     /// the immediate into a register.
424     bool isLegalAddImmediate(int64_t Imm) const override;
425 
426     /// getPreIndexedAddressParts - returns true by value, base pointer and
427     /// offset pointer and addressing mode by reference if the node's address
428     /// can be legally represented as pre-indexed load / store address.
429     bool getPreIndexedAddressParts(SDNode *N, SDValue &Base, SDValue &Offset,
430                                    ISD::MemIndexedMode &AM,
431                                    SelectionDAG &DAG) const override;
432 
433     /// getPostIndexedAddressParts - returns true by value, base pointer and
434     /// offset pointer and addressing mode by reference if this node can be
435     /// combined with a load / store to form a post-indexed load / store.
436     bool getPostIndexedAddressParts(SDNode *N, SDNode *Op, SDValue &Base,
437                                     SDValue &Offset, ISD::MemIndexedMode &AM,
438                                     SelectionDAG &DAG) const override;
439 
440     void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known,
441                                        const APInt &DemandedElts,
442                                        const SelectionDAG &DAG,
443                                        unsigned Depth) const override;
444 
445     bool targetShrinkDemandedConstant(SDValue Op, const APInt &Demanded,
446                                       TargetLoweringOpt &TLO) const override;
447 
448 
449     bool ExpandInlineAsm(CallInst *CI) const override;
450 
451     ConstraintType getConstraintType(StringRef Constraint) const override;
452 
453     /// Examine constraint string and operand type and determine a weight value.
454     /// The operand object must already have been set up with the operand type.
455     ConstraintWeight getSingleConstraintMatchWeight(
456       AsmOperandInfo &info, const char *constraint) const override;
457 
458     std::pair<unsigned, const TargetRegisterClass *>
459     getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
460                                  StringRef Constraint, MVT VT) const override;
461 
462     const char *LowerXConstraint(EVT ConstraintVT) const override;
463 
464     /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
465     /// vector.  If it is invalid, don't add anything to Ops. If hasMemory is
466     /// true it means one of the asm constraint of the inline asm instruction
467     /// being processed is 'm'.
468     void LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
469                                       std::vector<SDValue> &Ops,
470                                       SelectionDAG &DAG) const override;
471 
472     unsigned
473     getInlineAsmMemConstraint(StringRef ConstraintCode) const override {
474       if (ConstraintCode == "Q")
475         return InlineAsm::Constraint_Q;
476       else if (ConstraintCode == "o")
477         return InlineAsm::Constraint_o;
478       else if (ConstraintCode.size() == 2) {
479         if (ConstraintCode[0] == 'U') {
480           switch(ConstraintCode[1]) {
481           default:
482             break;
483           case 'm':
484             return InlineAsm::Constraint_Um;
485           case 'n':
486             return InlineAsm::Constraint_Un;
487           case 'q':
488             return InlineAsm::Constraint_Uq;
489           case 's':
490             return InlineAsm::Constraint_Us;
491           case 't':
492             return InlineAsm::Constraint_Ut;
493           case 'v':
494             return InlineAsm::Constraint_Uv;
495           case 'y':
496             return InlineAsm::Constraint_Uy;
497           }
498         }
499       }
500       return TargetLowering::getInlineAsmMemConstraint(ConstraintCode);
501     }
502 
503     const ARMSubtarget* getSubtarget() const {
504       return Subtarget;
505     }
506 
507     /// getRegClassFor - Return the register class that should be used for the
508     /// specified value type.
509     const TargetRegisterClass *
510     getRegClassFor(MVT VT, bool isDivergent = false) const override;
511 
512     /// Returns true if a cast between SrcAS and DestAS is a noop.
513     bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override {
514       // Addrspacecasts are always noops.
515       return true;
516     }
517 
518     bool shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
519                                 unsigned &PrefAlign) const override;
520 
521     /// createFastISel - This method returns a target specific FastISel object,
522     /// or null if the target does not support "fast" ISel.
523     FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
524                              const TargetLibraryInfo *libInfo) const override;
525 
526     Sched::Preference getSchedulingPreference(SDNode *N) const override;
527 
528     bool
529     isShuffleMaskLegal(ArrayRef<int> M, EVT VT) const override;
530     bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override;
531 
532     /// isFPImmLegal - Returns true if the target can instruction select the
533     /// specified FP immediate natively. If false, the legalizer will
534     /// materialize the FP immediate as a load from a constant pool.
535     bool isFPImmLegal(const APFloat &Imm, EVT VT,
536                       bool ForCodeSize = false) const override;
537 
538     bool getTgtMemIntrinsic(IntrinsicInfo &Info,
539                             const CallInst &I,
540                             MachineFunction &MF,
541                             unsigned Intrinsic) const override;
542 
543     /// Returns true if it is beneficial to convert a load of a constant
544     /// to just the constant itself.
545     bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
546                                            Type *Ty) const override;
547 
548     /// Return true if EXTRACT_SUBVECTOR is cheap for this result type
549     /// with this index.
550     bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT,
551                                  unsigned Index) const override;
552 
553     bool shouldFormOverflowOp(unsigned Opcode, EVT VT,
554                               bool MathUsed) const override {
555       // Using overflow ops for overflow checks only should beneficial on ARM.
556       return TargetLowering::shouldFormOverflowOp(Opcode, VT, true);
557     }
558 
559     /// Returns true if an argument of type Ty needs to be passed in a
560     /// contiguous block of registers in calling convention CallConv.
561     bool functionArgumentNeedsConsecutiveRegisters(
562         Type *Ty, CallingConv::ID CallConv, bool isVarArg) const override;
563 
564     /// If a physical register, this returns the register that receives the
565     /// exception address on entry to an EH pad.
566     Register
567     getExceptionPointerRegister(const Constant *PersonalityFn) const override;
568 
569     /// If a physical register, this returns the register that receives the
570     /// exception typeid on entry to a landing pad.
571     Register
572     getExceptionSelectorRegister(const Constant *PersonalityFn) const override;
573 
574     Instruction *makeDMB(IRBuilder<> &Builder, ARM_MB::MemBOpt Domain) const;
575     Value *emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
576                           AtomicOrdering Ord) const override;
577     Value *emitStoreConditional(IRBuilder<> &Builder, Value *Val,
578                                 Value *Addr, AtomicOrdering Ord) const override;
579 
580     void emitAtomicCmpXchgNoStoreLLBalance(IRBuilder<> &Builder) const override;
581 
582     Instruction *emitLeadingFence(IRBuilder<> &Builder, Instruction *Inst,
583                                   AtomicOrdering Ord) const override;
584     Instruction *emitTrailingFence(IRBuilder<> &Builder, Instruction *Inst,
585                                    AtomicOrdering Ord) const override;
586 
587     unsigned getMaxSupportedInterleaveFactor() const override;
588 
589     bool lowerInterleavedLoad(LoadInst *LI,
590                               ArrayRef<ShuffleVectorInst *> Shuffles,
591                               ArrayRef<unsigned> Indices,
592                               unsigned Factor) const override;
593     bool lowerInterleavedStore(StoreInst *SI, ShuffleVectorInst *SVI,
594                                unsigned Factor) const override;
595 
596     bool shouldInsertFencesForAtomic(const Instruction *I) const override;
597     TargetLoweringBase::AtomicExpansionKind
598     shouldExpandAtomicLoadInIR(LoadInst *LI) const override;
599     bool shouldExpandAtomicStoreInIR(StoreInst *SI) const override;
600     TargetLoweringBase::AtomicExpansionKind
601     shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const override;
602     TargetLoweringBase::AtomicExpansionKind
603     shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const override;
604 
605     bool useLoadStackGuardNode() const override;
606 
607     void insertSSPDeclarations(Module &M) const override;
608     Value *getSDagStackGuard(const Module &M) const override;
609     Function *getSSPStackGuardCheck(const Module &M) const override;
610 
611     bool canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
612                                    unsigned &Cost) const override;
613 
614     bool canMergeStoresTo(unsigned AddressSpace, EVT MemVT,
615                           const SelectionDAG &DAG) const override {
616       // Do not merge to larger than i32.
617       return (MemVT.getSizeInBits() <= 32);
618     }
619 
620     bool isCheapToSpeculateCttz() const override;
621     bool isCheapToSpeculateCtlz() const override;
622 
623     bool convertSetCCLogicToBitwiseLogic(EVT VT) const override {
624       return VT.isScalarInteger();
625     }
626 
627     bool supportSwiftError() const override {
628       return true;
629     }
630 
631     bool hasStandaloneRem(EVT VT) const override {
632       return HasStandaloneRem;
633     }
634 
635     bool shouldExpandShift(SelectionDAG &DAG, SDNode *N) const override;
636 
637     CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool isVarArg) const;
638     CCAssignFn *CCAssignFnForReturn(CallingConv::ID CC, bool isVarArg) const;
639 
640     /// Returns true if \p VecTy is a legal interleaved access type. This
641     /// function checks the vector element type and the overall width of the
642     /// vector.
643     bool isLegalInterleavedAccessType(unsigned Factor, VectorType *VecTy,
644                                       const DataLayout &DL) const;
645 
646     bool alignLoopsWithOptSize() const override;
647 
648     /// Returns the number of interleaved accesses that will be generated when
649     /// lowering accesses of the given type.
650     unsigned getNumInterleavedAccesses(VectorType *VecTy,
651                                        const DataLayout &DL) const;
652 
653     void finalizeLowering(MachineFunction &MF) const override;
654 
655     /// Return the correct alignment for the current calling convention.
656     Align getABIAlignmentForCallingConv(Type *ArgTy,
657                                         DataLayout DL) const override;
658 
659     bool isDesirableToCommuteWithShift(const SDNode *N,
660                                        CombineLevel Level) const override;
661 
662     bool shouldFoldConstantShiftPairToMask(const SDNode *N,
663                                            CombineLevel Level) const override;
664 
665     bool preferIncOfAddToSubOfNot(EVT VT) const override;
666 
667   protected:
668     std::pair<const TargetRegisterClass *, uint8_t>
669     findRepresentativeClass(const TargetRegisterInfo *TRI,
670                             MVT VT) const override;
671 
672   private:
673     /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
674     /// make the right decision when generating code for different targets.
675     const ARMSubtarget *Subtarget;
676 
677     const TargetRegisterInfo *RegInfo;
678 
679     const InstrItineraryData *Itins;
680 
681     /// ARMPCLabelIndex - Keep track of the number of ARM PC labels created.
682     unsigned ARMPCLabelIndex;
683 
684     // TODO: remove this, and have shouldInsertFencesForAtomic do the proper
685     // check.
686     bool InsertFencesForAtomic;
687 
688     bool HasStandaloneRem = true;
689 
690     void addTypeForNEON(MVT VT, MVT PromotedLdStVT, MVT PromotedBitwiseVT);
691     void addDRTypeForNEON(MVT VT);
692     void addQRTypeForNEON(MVT VT);
693     std::pair<SDValue, SDValue> getARMXALUOOp(SDValue Op, SelectionDAG &DAG, SDValue &ARMcc) const;
694 
695     using RegsToPassVector = SmallVector<std::pair<unsigned, SDValue>, 8>;
696 
697     void PassF64ArgInRegs(const SDLoc &dl, SelectionDAG &DAG, SDValue Chain,
698                           SDValue &Arg, RegsToPassVector &RegsToPass,
699                           CCValAssign &VA, CCValAssign &NextVA,
700                           SDValue &StackPtr,
701                           SmallVectorImpl<SDValue> &MemOpChains,
702                           ISD::ArgFlagsTy Flags) const;
703     SDValue GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
704                                  SDValue &Root, SelectionDAG &DAG,
705                                  const SDLoc &dl) const;
706 
707     CallingConv::ID getEffectiveCallingConv(CallingConv::ID CC,
708                                             bool isVarArg) const;
709     CCAssignFn *CCAssignFnForNode(CallingConv::ID CC, bool Return,
710                                   bool isVarArg) const;
711     SDValue LowerMemOpCallTo(SDValue Chain, SDValue StackPtr, SDValue Arg,
712                              const SDLoc &dl, SelectionDAG &DAG,
713                              const CCValAssign &VA,
714                              ISD::ArgFlagsTy Flags) const;
715     SDValue LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const;
716     SDValue LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const;
717     SDValue LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op, SelectionDAG &DAG) const;
718     SDValue LowerINTRINSIC_VOID(SDValue Op, SelectionDAG &DAG,
719                                     const ARMSubtarget *Subtarget) const;
720     SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
721                                     const ARMSubtarget *Subtarget) const;
722     SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
723     SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
724     SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
725     SDValue LowerGlobalAddressDarwin(SDValue Op, SelectionDAG &DAG) const;
726     SDValue LowerGlobalAddressELF(SDValue Op, SelectionDAG &DAG) const;
727     SDValue LowerGlobalAddressWindows(SDValue Op, SelectionDAG &DAG) const;
728     SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
729     SDValue LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
730                                             SelectionDAG &DAG) const;
731     SDValue LowerToTLSExecModels(GlobalAddressSDNode *GA,
732                                  SelectionDAG &DAG,
733                                  TLSModel::Model model) const;
734     SDValue LowerGlobalTLSAddressDarwin(SDValue Op, SelectionDAG &DAG) const;
735     SDValue LowerGlobalTLSAddressWindows(SDValue Op, SelectionDAG &DAG) const;
736     SDValue LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG) const;
737     SDValue LowerBR_JT(SDValue Op, SelectionDAG &DAG) const;
738     SDValue LowerSignedALUO(SDValue Op, SelectionDAG &DAG) const;
739     SDValue LowerUnsignedALUO(SDValue Op, SelectionDAG &DAG) const;
740     SDValue LowerSELECT(SDValue Op, SelectionDAG &DAG) const;
741     SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
742     SDValue LowerBRCOND(SDValue Op, SelectionDAG &DAG) const;
743     SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
744     SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const;
745     SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
746     SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
747     SDValue LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) const;
748     SDValue LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) const;
749     SDValue LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) const;
750     SDValue LowerConstantFP(SDValue Op, SelectionDAG &DAG,
751                             const ARMSubtarget *ST) const;
752     SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
753                               const ARMSubtarget *ST) const;
754     SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
755     SDValue LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const;
756     SDValue LowerDivRem(SDValue Op, SelectionDAG &DAG) const;
757     SDValue LowerDIV_Windows(SDValue Op, SelectionDAG &DAG, bool Signed) const;
758     void ExpandDIV_Windows(SDValue Op, SelectionDAG &DAG, bool Signed,
759                            SmallVectorImpl<SDValue> &Results) const;
760     SDValue LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG, bool Signed,
761                                    SDValue &Chain) const;
762     SDValue LowerREM(SDNode *N, SelectionDAG &DAG) const;
763     SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
764     SDValue LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const;
765     SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const;
766     SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const;
767     SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
768     SDValue LowerFSETCC(SDValue Op, SelectionDAG &DAG) const;
769     void lowerABS(SDNode *N, SmallVectorImpl<SDValue> &Results,
770                   SelectionDAG &DAG) const;
771     void LowerLOAD(SDNode *N, SmallVectorImpl<SDValue> &Results,
772                    SelectionDAG &DAG) const;
773 
774     Register getRegisterByName(const char* RegName, LLT VT,
775                                const MachineFunction &MF) const override;
776 
777     SDValue BuildSDIVPow2(SDNode *N, const APInt &Divisor, SelectionDAG &DAG,
778                           SmallVectorImpl<SDNode *> &Created) const override;
779 
780     bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
781                                     EVT VT) const override;
782 
783     SDValue ReconstructShuffle(SDValue Op, SelectionDAG &DAG) const;
784 
785     SDValue LowerCallResult(SDValue Chain, SDValue InFlag,
786                             CallingConv::ID CallConv, bool isVarArg,
787                             const SmallVectorImpl<ISD::InputArg> &Ins,
788                             const SDLoc &dl, SelectionDAG &DAG,
789                             SmallVectorImpl<SDValue> &InVals, bool isThisReturn,
790                             SDValue ThisVal) const;
791 
792     bool supportSplitCSR(MachineFunction *MF) const override {
793       return MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS &&
794           MF->getFunction().hasFnAttribute(Attribute::NoUnwind);
795     }
796 
797     void initializeSplitCSR(MachineBasicBlock *Entry) const override;
798     void insertCopiesSplitCSR(
799       MachineBasicBlock *Entry,
800       const SmallVectorImpl<MachineBasicBlock *> &Exits) const override;
801 
802     SDValue
803     LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
804                          const SmallVectorImpl<ISD::InputArg> &Ins,
805                          const SDLoc &dl, SelectionDAG &DAG,
806                          SmallVectorImpl<SDValue> &InVals) const override;
807 
808     int StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG, const SDLoc &dl,
809                        SDValue &Chain, const Value *OrigArg,
810                        unsigned InRegsParamRecordIdx, int ArgOffset,
811                        unsigned ArgSize) const;
812 
813     void VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
814                               const SDLoc &dl, SDValue &Chain,
815                               unsigned ArgOffset, unsigned TotalArgRegsSaveSize,
816                               bool ForceMutable = false) const;
817 
818     SDValue LowerCall(TargetLowering::CallLoweringInfo &CLI,
819                       SmallVectorImpl<SDValue> &InVals) const override;
820 
821     /// HandleByVal - Target-specific cleanup for ByVal support.
822     void HandleByVal(CCState *, unsigned &, unsigned) const override;
823 
824     /// IsEligibleForTailCallOptimization - Check whether the call is eligible
825     /// for tail call optimization. Targets which want to do tail call
826     /// optimization should implement this function.
827     bool IsEligibleForTailCallOptimization(
828         SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg,
829         bool isCalleeStructRet, bool isCallerStructRet,
830         const SmallVectorImpl<ISD::OutputArg> &Outs,
831         const SmallVectorImpl<SDValue> &OutVals,
832         const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG,
833         const bool isIndirect) const;
834 
835     bool CanLowerReturn(CallingConv::ID CallConv,
836                         MachineFunction &MF, bool isVarArg,
837                         const SmallVectorImpl<ISD::OutputArg> &Outs,
838                         LLVMContext &Context) const override;
839 
840     SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
841                         const SmallVectorImpl<ISD::OutputArg> &Outs,
842                         const SmallVectorImpl<SDValue> &OutVals,
843                         const SDLoc &dl, SelectionDAG &DAG) const override;
844 
845     bool isUsedByReturnOnly(SDNode *N, SDValue &Chain) const override;
846 
847     bool mayBeEmittedAsTailCall(const CallInst *CI) const override;
848 
849     bool shouldConsiderGEPOffsetSplit() const override { return true; }
850 
851     bool isUnsupportedFloatingType(EVT VT) const;
852 
853     SDValue getCMOV(const SDLoc &dl, EVT VT, SDValue FalseVal, SDValue TrueVal,
854                     SDValue ARMcc, SDValue CCR, SDValue Cmp,
855                     SelectionDAG &DAG) const;
856     SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
857                       SDValue &ARMcc, SelectionDAG &DAG, const SDLoc &dl) const;
858     SDValue getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
859                       const SDLoc &dl, bool Signaling = false) const;
860     SDValue duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const;
861 
862     SDValue OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const;
863 
864     void SetupEntryBlockForSjLj(MachineInstr &MI, MachineBasicBlock *MBB,
865                                 MachineBasicBlock *DispatchBB, int FI) const;
866 
867     void EmitSjLjDispatchBlock(MachineInstr &MI, MachineBasicBlock *MBB) const;
868 
869     bool RemapAddSubWithFlags(MachineInstr &MI, MachineBasicBlock *BB) const;
870 
871     MachineBasicBlock *EmitStructByval(MachineInstr &MI,
872                                        MachineBasicBlock *MBB) const;
873 
874     MachineBasicBlock *EmitLowered__chkstk(MachineInstr &MI,
875                                            MachineBasicBlock *MBB) const;
876     MachineBasicBlock *EmitLowered__dbzchk(MachineInstr &MI,
877                                            MachineBasicBlock *MBB) const;
878     void addMVEVectorTypes(bool HasMVEFP);
879     void addAllExtLoads(const MVT From, const MVT To, LegalizeAction Action);
880     void setAllExpand(MVT VT);
881   };
882 
883   enum VMOVModImmType {
884     VMOVModImm,
885     VMVNModImm,
886     MVEVMVNModImm,
887     OtherModImm
888   };
889 
890   namespace ARM {
891 
892     FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
893                              const TargetLibraryInfo *libInfo);
894 
895   } // end namespace ARM
896 
897 } // end namespace llvm
898 
899 #endif // LLVM_LIB_TARGET_ARM_ARMISELLOWERING_H
900