LLVM  14.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"
15 #include "Mips16InstrInfo.h"
16 #include "MipsInstrInfo.h"
17 #include "MipsRegisterInfo.h"
18 #include "MipsSubtarget.h"
19 #include "llvm/ADT/BitVector.h"
26 #include "llvm/IR/DebugLoc.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCDwarf.h"
29 #include "llvm/MC/MCRegisterInfo.h"
33 #include <cassert>
34 #include <cstdint>
35 #include <vector>
36 
37 using namespace llvm;
38 
40  : MipsFrameLowering(STI, STI.getStackAlignment()) {}
41 
43  MachineBasicBlock &MBB) const {
44  MachineFrameInfo &MFI = MF.getFrameInfo();
45  const Mips16InstrInfo &TII =
46  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
48 
49  // Debug location must be unknown since the first debug location is used
50  // to determine the end of the prologue.
51  DebugLoc dl;
52 
53  uint64_t StackSize = MFI.getStackSize();
54 
55  // No need to allocate space on the stack.
56  if (StackSize == 0 && !MFI.adjustsStack()) return;
57 
58  MachineModuleInfo &MMI = MF.getMMI();
60 
61  // Adjust stack.
62  TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
63 
64  // emit ".cfi_def_cfa_offset StackSize"
65  unsigned CFIIndex =
66  MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, StackSize));
67  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
68  .addCFIIndex(CFIIndex);
69 
70  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
71 
72  if (!CSI.empty()) {
73  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
74 
75  for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
76  E = CSI.end(); I != E; ++I) {
77  int64_t Offset = MFI.getObjectOffset(I->getFrameIdx());
78  unsigned Reg = I->getReg();
79  unsigned DReg = MRI->getDwarfRegNum(Reg, true);
80  unsigned CFIIndex = MF.addFrameInst(
82  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
83  .addCFIIndex(CFIIndex);
84  }
85  }
86  if (hasFP(MF))
87  BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
89 }
90 
92  MachineBasicBlock &MBB) const {
94  MachineFrameInfo &MFI = MF.getFrameInfo();
95  const Mips16InstrInfo &TII =
96  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
97  DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
98  uint64_t StackSize = MFI.getStackSize();
99 
100  if (!StackSize)
101  return;
102 
103  if (hasFP(MF))
104  BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
105  .addReg(Mips::S0);
106 
107  // Adjust stack.
108  // assumes stacksize multiple of 8
109  TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
110 }
111 
115  MachineFunction *MF = MBB.getParent();
116 
117  //
118  // Registers RA, S0,S1 are the callee saved registers and they
119  // will be saved with the "save" instruction
120  // during emitPrologue
121  //
122  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
123  // Add the callee-saved register as live-in. Do not add if the register is
124  // RA and return address is taken, because it has already been added in
125  // method MipsTargetLowering::lowerRETURNADDR.
126  // It's killed at the spill, unless the register is RA and return address
127  // is taken.
128  unsigned Reg = CSI[i].getReg();
129  bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
131  if (!IsRAAndRetAddrIsTaken)
132  MBB.addLiveIn(Reg);
133  }
134 
135  return true;
136 }
137 
141  //
142  // Registers RA,S0,S1 are the callee saved registers and they will be restored
143  // with the restore instruction during emitEpilogue.
144  // We need to override this virtual function, otherwise llvm will try and
145  // restore the registers on it's on from the stack.
146  //
147 
148  return true;
149 }
150 
151 bool
153  const MachineFrameInfo &MFI = MF.getFrameInfo();
154  // Reserve call frame if the size of the maximum call frame fits into 15-bit
155  // immediate field and there are no variable sized objects on the stack.
156  return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();
157 }
158 
160  BitVector &SavedRegs,
161  RegScavenger *RS) const {
163  const Mips16InstrInfo &TII =
164  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
165  const MipsRegisterInfo &RI = TII.getRegisterInfo();
166  const BitVector Reserved = RI.getReservedRegs(MF);
167  bool SaveS2 = Reserved[Mips::S2];
168  if (SaveS2)
169  SavedRegs.set(Mips::S2);
170  if (hasFP(MF))
171  SavedRegs.set(Mips::S0);
172 }
173 
174 const MipsFrameLowering *
176  return new Mips16FrameLowering(ST);
177 }
i
i
Definition: README.txt:29
Mips16FrameLowering.h
llvm::MachineFrameInfo::hasVarSizedObjects
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
Definition: MachineFrameInfo.h:353
MCDwarf.h
MipsBaseInfo.h
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
MachineInstr.h
MathExtras.h
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::Mips16FrameLowering::spillCalleeSavedRegisters
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...
Definition: Mips16FrameLowering.cpp:112
TargetFrameLowering.h
llvm::MachineModuleInfo::getContext
const MCContext & getContext() const
Definition: MachineModuleInfo.h:167
llvm::MCContext::getRegisterInfo
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:425
llvm::BitVector::set
BitVector & set()
Definition: BitVector.h:343
llvm::MachineInstrBuilder::addCFIIndex
const MachineInstrBuilder & addCFIIndex(unsigned CFIIndex) const
Definition: MachineInstrBuilder.h:247
MachineBasicBlock.h
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:233
llvm::MipsSubtarget::getInstrInfo
const MipsInstrInfo * getInstrInfo() const override
Definition: MipsSubtarget.h:382
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1567
llvm::MachineFrameInfo::getMaxCallFrameSize
unsigned getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
Definition: MachineFrameInfo.h:622
llvm::MCCFIInstruction::cfiDefCfaOffset
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int Offset)
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition: MCDwarf.h:502
llvm::MutableArrayRef
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
llvm::MachineFrameInfo::isReturnAddressTaken
bool isReturnAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
Definition: MachineFrameInfo.h:374
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
MCContext.h
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:129
llvm::MachineInstr::FrameSetup
@ FrameSetup
Definition: MachineInstr.h:82
MipsRegisterInfo.h
llvm::MachineModuleInfo
This class contains meta information specific to a module.
Definition: MachineModuleInfo.h:78
llvm::Mips16FrameLowering::determineCalleeSaves
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
Definition: Mips16FrameLowering.cpp:159
BitVector.h
llvm::MachineFrameInfo::getStackSize
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
Definition: MachineFrameInfo.h:553
DebugLoc.h
llvm::MachineFrameInfo::getObjectOffset
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
Definition: MachineFrameInfo.h:494
llvm::BitVector
Definition: BitVector.h:74
llvm::MipsFrameLowering::STI
const MipsSubtarget & STI
Definition: MipsFrameLowering.h:24
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::Mips16InstrInfo
Definition: Mips16InstrInfo.h:27
llvm::Mips16FrameLowering::hasReservedCallFrame
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
Definition: Mips16FrameLowering.cpp:152
llvm::M68kBeads::DReg
@ DReg
Definition: M68kBaseInfo.h:61
llvm::MachineFunction::getMMI
MachineModuleInfo & getMMI() const
Definition: MachineFunction.h:577
llvm::Mips16FrameLowering
Definition: Mips16FrameLowering.h:19
llvm::MachineInstrBuilder::setMIFlag
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
Definition: MachineInstrBuilder.h:278
uint64_t
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
llvm::MipsFrameLowering::hasFP
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register.
Definition: MipsFrameLowering.cpp:93
Mips16InstrInfo.h
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::RegScavenger
Definition: RegisterScavenging.h:34
llvm::MipsRegisterInfo
Definition: MipsRegisterInfo.h:27
MCRegisterInfo.h
llvm::MachineFunction::getFrameInfo
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Definition: MachineFunction.h:642
llvm::MachineBasicBlock::getParent
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Definition: MachineBasicBlock.h:225
MachineModuleInfo.h
llvm::MachineInstrBuilder::addReg
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Definition: MachineInstrBuilder.h:97
llvm::createMips16FrameLowering
const MipsFrameLowering * createMips16FrameLowering(const MipsSubtarget &ST)
Create MipsFrameLowering objects.
Definition: Mips16FrameLowering.cpp:175
RA
SI optimize exec mask operations pre RA
Definition: SIOptimizeExecMaskingPreRA.cpp:71
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::MachineFrameInfo::getCalleeSavedInfo
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
Definition: MachineFrameInfo.h:755
llvm::MachineBasicBlock::getFirstTerminator
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
Definition: MachineBasicBlock.cpp:242
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
MBBI
MachineBasicBlock MachineBasicBlock::iterator MBBI
Definition: AArch64SLSHardening.cpp:75
MachineLocation.h
llvm::MipsFrameLowering
Definition: MipsFrameLowering.h:22
llvm::MipsSubtarget
Definition: MipsSubtarget.h:39
llvm::MCRegisterInfo
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Definition: MCRegisterInfo.h:135
llvm::Mips16FrameLowering::restoreCalleeSavedRegisters
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...
Definition: Mips16FrameLowering.cpp:138
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::MachineBasicBlock::addLiveIn
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
Definition: MachineBasicBlock.h:367
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
MachineFrameInfo.h
llvm::MipsRegisterInfo::getReservedRegs
BitVector getReservedRegs(const MachineFunction &MF) const override
Definition: MipsRegisterInfo.cpp:150
MipsInstrInfo.h
llvm::MachineFunction::addFrameInst
LLVM_NODISCARD unsigned addFrameInst(const MCCFIInstruction &Inst)
Definition: MachineFunction.cpp:285
llvm::MachineFrameInfo
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Definition: MachineFrameInfo.h:107
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:268
MachineInstrBuilder.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:328
llvm::Mips16FrameLowering::emitPrologue
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
Definition: Mips16FrameLowering.cpp:42
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
MipsSubtarget.h
llvm::MCCFIInstruction::createOffset
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int Offset)
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
Definition: MCDwarf.h:526
llvm::TargetFrameLowering::determineCalleeSaves
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
Definition: TargetFrameLoweringImpl.cpp:78
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::MachineFrameInfo::adjustsStack
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
Definition: MachineFrameInfo.h:577
llvm::Mips16FrameLowering::Mips16FrameLowering
Mips16FrameLowering(const MipsSubtarget &STI)
Definition: Mips16FrameLowering.cpp:39
MachineFunction.h
llvm::Mips16FrameLowering::emitEpilogue
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
Definition: Mips16FrameLowering.cpp:91
llvm::MachineInstrBundleIterator< MachineInstr >
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:270