LLVM 24.0.0git
Mips16FrameLowering.cpp
Go to the documentation of this file.
1//===- Mips16FrameLowering.cpp - Mips16 Frame Information -----------------===//
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 contains the Mips16 implementation of TargetFrameLowering class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Mips16FrameLowering.h"
14#include "Mips16InstrInfo.h"
15#include "MipsInstrInfo.h"
16#include "MipsRegisterInfo.h"
17#include "MipsSubtarget.h"
18#include "llvm/ADT/BitVector.h"
27#include "llvm/IR/DebugLoc.h"
29#include <cstdint>
30#include <vector>
31
32using namespace llvm;
33
36
38 MachineBasicBlock &MBB) const {
40 const Mips16InstrInfo &TII =
41 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
43
44 // Debug location must be unknown since the first debug location is used
45 // to determine the end of the prologue.
46 DebugLoc dl;
47
48 uint64_t StackSize = MFI.getStackSize();
49
50 // No need to allocate space on the stack.
51 if (StackSize == 0 && !MFI.adjustsStack()) return;
52
53 // Adjust stack.
54 TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
55
57 CFIBuilder.buildDefCFAOffset(StackSize);
58
59 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
60
61 if (!CSI.empty()) {
62 for (const CalleeSavedInfo &I : CSI)
63 CFIBuilder.buildOffset(I.getReg(), MFI.getObjectOffset(I.getFrameIdx()));
64 }
65
66 if (hasFP(MF))
67 BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::R16)
70}
71
73 MachineBasicBlock &MBB) const {
74 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
76 const Mips16InstrInfo &TII =
77 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
78 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
79 uint64_t StackSize = MFI.getStackSize();
80
81 if (!StackSize)
82 return;
83
84 if (hasFP(MF))
85 BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
86 .addReg(Mips::R16);
87
88 // Adjust stack.
89 // assumes stacksize multiple of 8
90 TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
91}
92
96 MachineFunction *MF = MBB.getParent();
97
98 //
99 // Registers RA, S0,S1 are the callee saved registers and they
100 // will be saved with the "save" instruction
101 // during emitPrologue
102 //
103 for (const CalleeSavedInfo &I : CSI) {
104 // Add the callee-saved register as live-in. Do not add if the register is
105 // RA and return address is taken, because it has already been added in
106 // method MipsTargetLowering::lowerRETURNADDR.
107 // It's killed at the spill, unless the register is RA and return address
108 // is taken.
109 MCRegister Reg = I.getReg();
110 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
112 if (!IsRAAndRetAddrIsTaken)
113 MBB.addLiveIn(Reg);
114 }
115
116 return true;
117}
118
122 //
123 // Registers RA,S0,S1 are the callee saved registers and they will be restored
124 // with the restore instruction during emitEpilogue.
125 // We need to override this virtual function, otherwise llvm will try and
126 // restore the registers on it's on from the stack.
127 //
128
129 return true;
130}
131
132bool
134 const MachineFrameInfo &MFI = MF.getFrameInfo();
135 // Reserve call frame if the size of the maximum call frame fits into 15-bit
136 // immediate field and there are no variable sized objects on the stack.
137 return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();
138}
139
141 BitVector &SavedRegs,
142 RegScavenger *RS) const {
144 const Mips16InstrInfo &TII =
145 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
146 const MipsRegisterInfo &RI = TII.getRegisterInfo();
147 const BitVector Reserved = RI.getReservedRegs(MF);
148 bool SaveS2 = Reserved[Mips::R18];
149 if (SaveS2)
150 SavedRegs.set(Mips::R18);
151 if (hasFP(MF))
152 SavedRegs.set(Mips::R16);
153}
154
155const MipsFrameLowering *
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file implements the BitVector class.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
Helper class for creating CFI instructions and inserting them into MIR.
void buildDefCFAOffset(int64_t Offset, MCSymbol *Label=nullptr) const
void buildOffset(MCRegister Reg, int64_t Offset) const
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
A debug info location.
Definition DebugLoc.h:126
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
bool isReturnAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, ArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
Mips16FrameLowering(const MipsSubtarget &STI)
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, MutableArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
MipsFrameLowering(const MipsSubtarget &sti, Align Alignment)
const MipsSubtarget & STI
BitVector getReservedRegs(const MachineFunction &MF) const override
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
constexpr MCPhysReg SP
constexpr MCPhysReg RA
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
const MipsFrameLowering * createMips16FrameLowering(const MipsSubtarget &ST)
Create MipsFrameLowering objects.