LLVM  8.0.1
Mips16FrameLowering.cpp
Go to the documentation of this file.
1 //===- Mips16FrameLowering.cpp - Mips16 Frame Information -----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the Mips16 implementation of TargetFrameLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "Mips16FrameLowering.h"
16 #include "Mips16InstrInfo.h"
17 #include "MipsInstrInfo.h"
18 #include "MipsRegisterInfo.h"
19 #include "MipsSubtarget.h"
20 #include "llvm/ADT/BitVector.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCDwarf.h"
30 #include "llvm/MC/MCRegisterInfo.h"
34 #include <cassert>
35 #include <cstdint>
36 #include <vector>
37 
38 using namespace llvm;
39 
41  : MipsFrameLowering(STI, STI.getStackAlignment()) {}
42 
44  MachineBasicBlock &MBB) const {
45  MachineFrameInfo &MFI = MF.getFrameInfo();
46  const Mips16InstrInfo &TII =
47  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
49 
50  // Debug location must be unknown since the first debug location is used
51  // to determine the end of the prologue.
52  DebugLoc dl;
53 
54  uint64_t StackSize = MFI.getStackSize();
55 
56  // No need to allocate space on the stack.
57  if (StackSize == 0 && !MFI.adjustsStack()) return;
58 
59  MachineModuleInfo &MMI = MF.getMMI();
61 
62  // Adjust stack.
63  TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
64 
65  // emit ".cfi_def_cfa_offset StackSize"
66  unsigned CFIIndex = MF.addFrameInst(
67  MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
68  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
69  .addCFIIndex(CFIIndex);
70 
71  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
72 
73  if (!CSI.empty()) {
74  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
75 
76  for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
77  E = CSI.end(); I != E; ++I) {
78  int64_t Offset = MFI.getObjectOffset(I->getFrameIdx());
79  unsigned Reg = I->getReg();
80  unsigned DReg = MRI->getDwarfRegNum(Reg, true);
81  unsigned CFIIndex = MF.addFrameInst(
82  MCCFIInstruction::createOffset(nullptr, DReg, Offset));
83  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
84  .addCFIIndex(CFIIndex);
85  }
86  }
87  if (hasFP(MF))
88  BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
89  .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
90 }
91 
93  MachineBasicBlock &MBB) const {
95  MachineFrameInfo &MFI = MF.getFrameInfo();
96  const Mips16InstrInfo &TII =
97  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
98  DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
99  uint64_t StackSize = MFI.getStackSize();
100 
101  if (!StackSize)
102  return;
103 
104  if (hasFP(MF))
105  BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
106  .addReg(Mips::S0);
107 
108  // Adjust stack.
109  // assumes stacksize multiple of 8
110  TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
111 }
112 
116  const std::vector<CalleeSavedInfo> &CSI,
117  const TargetRegisterInfo *TRI) const {
118  MachineFunction *MF = MBB.getParent();
119 
120  //
121  // Registers RA, S0,S1 are the callee saved registers and they
122  // will be saved with the "save" instruction
123  // during emitPrologue
124  //
125  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
126  // Add the callee-saved register as live-in. Do not add if the register is
127  // RA and return address is taken, because it has already been added in
128  // method MipsTargetLowering::lowerRETURNADDR.
129  // It's killed at the spill, unless the register is RA and return address
130  // is taken.
131  unsigned Reg = CSI[i].getReg();
132  bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
134  if (!IsRAAndRetAddrIsTaken)
135  MBB.addLiveIn(Reg);
136  }
137 
138  return true;
139 }
140 
143  std::vector<CalleeSavedInfo> &CSI,
144  const TargetRegisterInfo *TRI) const {
145  //
146  // Registers RA,S0,S1 are the callee saved registers and they will be restored
147  // with the restore instruction during emitEpilogue.
148  // We need to override this virtual function, otherwise llvm will try and
149  // restore the registers on it's on from the stack.
150  //
151 
152  return true;
153 }
154 
155 bool
157  const MachineFrameInfo &MFI = MF.getFrameInfo();
158  // Reserve call frame if the size of the maximum call frame fits into 15-bit
159  // immediate field and there are no variable sized objects on the stack.
160  return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();
161 }
162 
164  BitVector &SavedRegs,
165  RegScavenger *RS) const {
167  const Mips16InstrInfo &TII =
168  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
169  const MipsRegisterInfo &RI = TII.getRegisterInfo();
170  const BitVector Reserved = RI.getReservedRegs(MF);
171  bool SaveS2 = Reserved[Mips::S2];
172  if (SaveS2)
173  SavedRegs.set(Mips::S2);
174  if (hasFP(MF))
175  SavedRegs.set(Mips::S0);
176 }
177 
178 const MipsFrameLowering *
180  return new Mips16FrameLowering(ST);
181 }
Mips16FrameLowering(const MipsSubtarget &STI)
BitVector & set()
Definition: BitVector.h:398
const MipsFrameLowering * createMips16FrameLowering(const MipsSubtarget &ST)
Create MipsFrameLowering objects.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
const MipsSubtarget & STI
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:488
unsigned Reg
const MipsInstrInfo * getInstrInfo() const override
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:34
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:475
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
LLVM_NODISCARD unsigned addFrameInst(const MCCFIInstruction &Inst)
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:295
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.