1 //==-- AArch64FrameLowering.h - TargetFrameLowering for AArch64 --*- 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 //
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_TARGET_AARCH64_AARCH64FRAMELOWERING_H
14 #define LLVM_LIB_TARGET_AARCH64_AARCH64FRAMELOWERING_H
15 
16 #include "llvm/Support/TypeSize.h"
17 #include "llvm/CodeGen/TargetFrameLowering.h"
18 
19 namespace llvm {
20 
21 class AArch64FrameLowering : public TargetFrameLowering {
22 public:
23   explicit AArch64FrameLowering()
24       : TargetFrameLowering(StackGrowsDown, Align(16), 0, Align(16),
25                             true /*StackRealignable*/) {}
26 
27   void resetCFIToInitialState(MachineBasicBlock &MBB) const override;
28 
29   MachineBasicBlock::iterator
30   eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
31                                 MachineBasicBlock::iterator I) const override;
32 
33   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
34   /// the function.
35   void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
36   void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
37 
38   bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
39 
40   StackOffset getFrameIndexReference(const MachineFunction &MF, int FI,
41                                      Register &FrameReg) const override;
42   StackOffset resolveFrameIndexReference(const MachineFunction &MF, int FI,
43                                          Register &FrameReg, bool PreferFP,
44                                          bool ForSimm) const;
45   StackOffset resolveFrameOffsetReference(const MachineFunction &MF,
46                                           int64_t ObjectOffset, bool isFixed,
47                                           bool isSVE, Register &FrameReg,
48                                           bool PreferFP, bool ForSimm) const;
49   bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
50                                  MachineBasicBlock::iterator MI,
51                                  ArrayRef<CalleeSavedInfo> CSI,
52                                  const TargetRegisterInfo *TRI) const override;
53 
54   bool
55   restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
56                               MachineBasicBlock::iterator MI,
57                               MutableArrayRef<CalleeSavedInfo> CSI,
58                               const TargetRegisterInfo *TRI) const override;
59 
60   /// Can this function use the red zone for local allocations.
61   bool canUseRedZone(const MachineFunction &MF) const;
62 
63   bool hasFP(const MachineFunction &MF) const override;
64   bool hasReservedCallFrame(const MachineFunction &MF) const override;
65 
66   bool assignCalleeSavedSpillSlots(MachineFunction &MF,
67                                    const TargetRegisterInfo *TRI,
68                                    std::vector<CalleeSavedInfo> &CSI,
69                                    unsigned &MinCSFrameIndex,
70                                    unsigned &MaxCSFrameIndex) const override;
71 
72   void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
73                             RegScavenger *RS) const override;
74 
75   /// Returns true if the target will correctly handle shrink wrapping.
76   bool enableShrinkWrapping(const MachineFunction &MF) const override {
77     return true;
78   }
79 
80   bool enableStackSlotScavenging(const MachineFunction &MF) const override;
81   TargetStackID::Value getStackIDForScalableVectors() const override;
82 
83   void processFunctionBeforeFrameFinalized(MachineFunction &MF,
84                                            RegScavenger *RS) const override;
85 
86   void
87   processFunctionBeforeFrameIndicesReplaced(MachineFunction &MF,
88                                             RegScavenger *RS) const override;
89 
90   unsigned getWinEHParentFrameOffset(const MachineFunction &MF) const override;
91 
92   unsigned getWinEHFuncletFrameSize(const MachineFunction &MF) const;
93 
94   StackOffset
95   getFrameIndexReferencePreferSP(const MachineFunction &MF, int FI,
96                                  Register &FrameReg,
97                                  bool IgnoreSPUpdates) const override;
98   StackOffset getNonLocalFrameIndexReference(const MachineFunction &MF,
99                                              int FI) const override;
100   int getSEHFrameIndexOffset(const MachineFunction &MF, int FI) const;
101 
102   bool isSupportedStackID(TargetStackID::Value ID) const override {
103     switch (ID) {
104     default:
105       return false;
106     case TargetStackID::Default:
107     case TargetStackID::ScalableVector:
108     case TargetStackID::NoAlloc:
109       return true;
110     }
111   }
112 
113   bool isStackIdSafeForLocalArea(unsigned StackId) const override {
114     // We don't support putting SVE objects into the pre-allocated local
115     // frame block at the moment.
116     return StackId != TargetStackID::ScalableVector;
117   }
118 
119   void
120   orderFrameObjects(const MachineFunction &MF,
121                     SmallVectorImpl<int> &ObjectsToAllocate) const override;
122 
123 private:
124   /// Returns true if a homogeneous prolog or epilog code can be emitted
125   /// for the size optimization. If so, HOM_Prolog/HOM_Epilog pseudo
126   /// instructions are emitted in place. When Exit block is given, this check is
127   /// for epilog.
128   bool homogeneousPrologEpilog(MachineFunction &MF,
129                                MachineBasicBlock *Exit = nullptr) const;
130 
131   /// Returns true if CSRs should be paired.
132   bool producePairRegisters(MachineFunction &MF) const;
133 
134   bool shouldCombineCSRLocalStackBump(MachineFunction &MF,
135                                       uint64_t StackBumpBytes) const;
136 
137   int64_t estimateSVEStackObjectOffsets(MachineFrameInfo &MF) const;
138   int64_t assignSVEStackObjectOffsets(MachineFrameInfo &MF,
139                                       int &MinCSFrameIndex,
140                                       int &MaxCSFrameIndex) const;
141   bool shouldCombineCSRLocalStackBumpInEpilogue(MachineBasicBlock &MBB,
142                                                 unsigned StackBumpBytes) const;
143   void emitCalleeSavedGPRLocations(MachineBasicBlock &MBB,
144                                    MachineBasicBlock::iterator MBBI) const;
145   void emitCalleeSavedSVELocations(MachineBasicBlock &MBB,
146                                    MachineBasicBlock::iterator MBBI) const;
147   void emitCalleeSavedGPRRestores(MachineBasicBlock &MBB,
148                                   MachineBasicBlock::iterator MBBI) const;
149   void emitCalleeSavedSVERestores(MachineBasicBlock &MBB,
150                                   MachineBasicBlock::iterator MBBI) const;
151 
152   /// Emit target zero call-used regs.
153   void emitZeroCallUsedRegs(BitVector RegsToZero,
154                             MachineBasicBlock &MBB) const override;
155 };
156 
157 } // End llvm namespace
158 
159 #endif
160