LLVM  10.0.0svn
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 = MF.addFrameInst(
66  MCCFIInstruction::createDefCfaOffset(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(
81  MCCFIInstruction::createOffset(nullptr, DReg, Offset));
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)
88  .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
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  const std::vector<CalleeSavedInfo> &CSI,
116  const TargetRegisterInfo *TRI) const {
117  MachineFunction *MF = MBB.getParent();
118 
119  //
120  // Registers RA, S0,S1 are the callee saved registers and they
121  // will be saved with the "save" instruction
122  // during emitPrologue
123  //
124  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
125  // Add the callee-saved register as live-in. Do not add if the register is
126  // RA and return address is taken, because it has already been added in
127  // method MipsTargetLowering::lowerRETURNADDR.
128  // It's killed at the spill, unless the register is RA and return address
129  // is taken.
130  unsigned Reg = CSI[i].getReg();
131  bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
133  if (!IsRAAndRetAddrIsTaken)
134  MBB.addLiveIn(Reg);
135  }
136 
137  return true;
138 }
139 
142  std::vector<CalleeSavedInfo> &CSI,
143  const TargetRegisterInfo *TRI) const {
144  //
145  // Registers RA,S0,S1 are the callee saved registers and they will be restored
146  // with the restore instruction during emitEpilogue.
147  // We need to override this virtual function, otherwise llvm will try and
148  // restore the registers on it's on from the stack.
149  //
150 
151  return true;
152 }
153 
154 bool
156  const MachineFrameInfo &MFI = MF.getFrameInfo();
157  // Reserve call frame if the size of the maximum call frame fits into 15-bit
158  // immediate field and there are no variable sized objects on the stack.
159  return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();
160 }
161 
163  BitVector &SavedRegs,
164  RegScavenger *RS) const {
166  const Mips16InstrInfo &TII =
167  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
168  const MipsRegisterInfo &RI = TII.getRegisterInfo();
169  const BitVector Reserved = RI.getReservedRegs(MF);
170  bool SaveS2 = Reserved[Mips::S2];
171  if (SaveS2)
172  SavedRegs.set(Mips::S2);
173  if (hasFP(MF))
174  SavedRegs.set(Mips::S0);
175 }
176 
177 const MipsFrameLowering *
179  return new Mips16FrameLowering(ST);
180 }
Mips16FrameLowering(const MipsSubtarget &STI)
BitVector & set()
Definition: BitVector.h:397
const MipsFrameLowering * createMips16FrameLowering(const MipsSubtarget &ST)
Create MipsFrameLowering objects.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const MipsSubtarget & STI
LLVM_NODISCARD unsigned addFrameInst(const MCCFIInstruction &Inst)
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
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:507
unsigned Reg
const MipsInstrInfo * getInstrInfo() const override
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:33
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
MachineModuleInfo & getMMI() const
static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset)
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition: MCDwarf.h:494
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
SI optimize exec mask operations pre RA
const HexagonInstrInfo * TII
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
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 ...
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
void restoreFrame(unsigned SP, int64_t FrameSize, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register...
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
const MCContext & getContext() const
BitVector getReservedRegs(const MachineFunction &MF) const override
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
unsigned const MachineRegisterInfo * MRI
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
const MipsRegisterInfo & getRegisterInfo() const override
getRegisterInfo - TargetInstrInfo is a superset of MRegister info.
unsigned getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call...
void makeFrame(unsigned SP, int64_t FrameSize, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required, we reserve argument space for call sites in the function immediately on entry to the current function.
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
bool isReturnAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
#define I(x, y, z)
Definition: MD5.cpp:58
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:312
IRTranslator LLVM IR MI
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
This class contains meta information specific to a module.