xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Target/M68k/M68kFrameLowering.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 //===- M68kFrameLowering.h - Define frame lowering for M68k -*- 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 /// \file
10 /// This file contains the M68k declaration of TargetFrameLowering class.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_TARGET_M68K_M68KFRAMELOWERING_H
15 #define LLVM_LIB_TARGET_M68K_M68KFRAMELOWERING_H
16 
17 #include "M68k.h"
18 
19 #include "llvm/CodeGen/TargetFrameLowering.h"
20 
21 namespace llvm {
22 class MachineInstrBuilder;
23 class MCCFIInstruction;
24 class M68kSubtarget;
25 class M68kRegisterInfo;
26 struct Align;
27 
28 class M68kFrameLowering : public TargetFrameLowering {
29   // Cached subtarget predicates.
30   const M68kSubtarget &STI;
31   const TargetInstrInfo &TII;
32   const M68kRegisterInfo *TRI;
33 
34   /// Stack slot size in bytes.
35   unsigned SlotSize;
36 
37   unsigned StackPtr;
38 
39   /// If we're forcing a stack realignment we can't rely on just the frame
40   /// info, we need to know the ABI stack alignment as well in case we have a
41   /// call out.  Otherwise just make sure we have some alignment - we'll go
42   /// with the minimum SlotSize.
43   uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;
44 
45   /// Adjusts the stack pointer using LEA, SUB, or ADD.
46   MachineInstrBuilder BuildStackAdjustment(MachineBasicBlock &MBB,
47                                            MachineBasicBlock::iterator MBBI,
48                                            const DebugLoc &DL, int64_t Offset,
49                                            bool InEpilogue) const;
50 
51   /// Aligns the stack pointer by ANDing it with -MaxAlign.
52   void BuildStackAlignAND(MachineBasicBlock &MBB,
53                           MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
54                           unsigned Reg, uint64_t MaxAlign) const;
55 
56   /// Wraps up getting a CFI index and building a MachineInstr for it.
57   void BuildCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
58                 const DebugLoc &DL, const MCCFIInstruction &CFIInst) const;
59 
60   void emitCalleeSavedFrameMoves(MachineBasicBlock &MBB,
61                                  MachineBasicBlock::iterator MBBI,
62                                  const DebugLoc &DL,
63                                  bool IsProlog) const override;
64 
65   unsigned getPSPSlotOffsetFromSP(const MachineFunction &MF) const;
66 
67 public:
68   explicit M68kFrameLowering(const M68kSubtarget &sti, Align Alignment);
69 
70   static const M68kFrameLowering *create(const M68kSubtarget &ST);
71 
72   /// This method is called during prolog/epilog code insertion to eliminate
73   /// call frame setup and destroy pseudo instructions (but only if the Target
74   /// is using them).  It is responsible for eliminating these instructions,
75   /// replacing them with concrete instructions.  This method need only be
76   /// implemented if using call frame setup/destroy pseudo instructions.
77   /// Returns an iterator pointing to the instruction after the replaced one.
78   MachineBasicBlock::iterator
79   eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
80                                 MachineBasicBlock::iterator MI) const override;
81 
82   /// Insert prolog code into the function.
83   void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
84 
85   /// Insert epilog code into the function.
86   void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
87 
88   /// This method determines which of the registers reported by
89   /// TargetRegisterInfo::getCalleeSavedRegs() should actually get saved.
90   /// The default implementation checks populates the \p SavedRegs bitset with
91   /// all registers which are modified in the function, targets may override
92   /// this function to save additional registers.
93   /// This method also sets up the register scavenger ensuring there is a free
94   /// register or a frameindex available.
95   void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
96                             RegScavenger *RS = nullptr) const override;
97 
98   /// Allows target to override spill slot assignment logic.  If implemented,
99   /// assignCalleeSavedSpillSlots() should assign frame slots to all CSI
100   /// entries and return true.  If this method returns false, spill slots will
101   /// be assigned using generic implementation.  assignCalleeSavedSpillSlots()
102   /// may add, delete or rearrange elements of CSI.
103   bool
104   assignCalleeSavedSpillSlots(MachineFunction &MF,
105                               const TargetRegisterInfo *TRI,
106                               std::vector<CalleeSavedInfo> &CSI) const override;
107 
108   /// Issues instruction(s) to spill all callee saved registers and returns
109   /// true if it isn't possible / profitable to do so by issuing a series of
110   /// store instructions via storeRegToStackSlot(). Returns false otherwise.
111   bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
112                                  MachineBasicBlock::iterator MI,
113                                  ArrayRef<CalleeSavedInfo> CSI,
114                                  const TargetRegisterInfo *TRI) const override;
115 
116   /// Issues instruction(s) to restore all callee saved registers and returns
117   /// true if it isn't possible / profitable to do so by issuing a series of
118   /// load instructions via loadRegToStackSlot().  Returns false otherwise.
119   bool
120   restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
121                               MachineBasicBlock::iterator MI,
122                               MutableArrayRef<CalleeSavedInfo> CSI,
123                               const TargetRegisterInfo *TRI) const override;
124 
125   /// Return true if the specified function should have a dedicated frame
126   /// pointer register.  This is true if the function has variable sized
127   /// allocas, if it needs dynamic stack realignment, if frame pointer
128   /// elimination is disabled, or if the frame address is taken.
129   bool hasFP(const MachineFunction &MF) const override;
130 
131   /// Under normal circumstances, when a frame pointer is not required, we
132   /// reserve argument space for call sites in the function immediately on
133   /// entry to the current function. This eliminates the need for add/sub sp
134   /// brackets around call sites. Returns true if the call frame is included as
135   /// part of the stack frame.
136   bool hasReservedCallFrame(const MachineFunction &MF) const override;
137 
138   /// If there is a reserved call frame, the call frame pseudos can be
139   /// simplified.  Having a FP, as in the default implementation, is not
140   /// sufficient here since we can't always use it.  Use a more nuanced
141   /// condition.
142   bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
143 
144   // Do we need to perform FI resolution for this function. Normally, this is
145   // required only when the function has any stack objects. However, FI
146   // resolution actually has another job, not apparent from the title - it
147   // resolves callframe setup/destroy that were not simplified earlier.
148   //
149   // So, this is required for M68k functions that have push sequences even
150   // when there are no stack objects.
151   bool needsFrameIndexResolution(const MachineFunction &MF) const override;
152 
153   /// This method should return the base register and offset used to reference
154   /// a frame index location. The offset is returned directly, and the base
155   /// register is returned via FrameReg.
156   StackOffset getFrameIndexReference(const MachineFunction &MF, int FI,
157                                      Register &FrameReg) const override;
158 
159   /// Check the instruction before/after the passed instruction. If
160   /// it is an ADD/SUB/LEA instruction it is deleted argument and the
161   /// stack adjustment is returned as a positive value for ADD/LEA and
162   /// a negative for SUB.
163   int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
164                      bool doMergeWithPrevious) const;
165 
166   /// Emit a series of instructions to increment / decrement the stack
167   /// pointer by a constant value.
168   void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
169                     int64_t NumBytes, bool InEpilogue) const;
170 };
171 } // namespace llvm
172 
173 #endif
174