LLVM  5.0.0
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.stackAlignment()) {}
42 
44  MachineBasicBlock &MBB) const {
45  assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
46  MachineFrameInfo &MFI = MF.getFrameInfo();
47  const Mips16InstrInfo &TII =
48  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
50 
51  // Debug location must be unknown since the first debug location is used
52  // to determine the end of the prologue.
53  DebugLoc dl;
54 
55  uint64_t StackSize = MFI.getStackSize();
56 
57  // No need to allocate space on the stack.
58  if (StackSize == 0 && !MFI.adjustsStack()) return;
59 
60  MachineModuleInfo &MMI = MF.getMMI();
62  MachineLocation DstML, SrcML;
63 
64  // Adjust stack.
65  TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
66 
67  // emit ".cfi_def_cfa_offset StackSize"
68  unsigned CFIIndex = MF.addFrameInst(
69  MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
70  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
71  .addCFIIndex(CFIIndex);
72 
73  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
74 
75  if (!CSI.empty()) {
76  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
77 
78  for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
79  E = CSI.end(); I != E; ++I) {
80  int64_t Offset = MFI.getObjectOffset(I->getFrameIdx());
81  unsigned Reg = I->getReg();
82  unsigned DReg = MRI->getDwarfRegNum(Reg, true);
83  unsigned CFIIndex = MF.addFrameInst(
84  MCCFIInstruction::createOffset(nullptr, DReg, Offset));
85  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
86  .addCFIIndex(CFIIndex);
87  }
88  }
89  if (hasFP(MF))
90  BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
91  .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
92 }
93 
95  MachineBasicBlock &MBB) const {
97  MachineFrameInfo &MFI = MF.getFrameInfo();
98  const Mips16InstrInfo &TII =
99  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
100  DebugLoc dl = MBBI->getDebugLoc();
101  uint64_t StackSize = MFI.getStackSize();
102 
103  if (!StackSize)
104  return;
105 
106  if (hasFP(MF))
107  BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
108  .addReg(Mips::S0);
109 
110  // Adjust stack.
111  // assumes stacksize multiple of 8
112  TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
113 }
114 
118  const std::vector<CalleeSavedInfo> &CSI,
119  const TargetRegisterInfo *TRI) const {
120  MachineFunction *MF = MBB.getParent();
121  MachineBasicBlock *EntryBlock = &MF->front();
122 
123  //
124  // Registers RA, S0,S1 are the callee saved registers and they
125  // will be saved with the "save" instruction
126  // during emitPrologue
127  //
128  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
129  // Add the callee-saved register as live-in. Do not add if the register is
130  // RA and return address is taken, because it has already been added in
131  // method MipsTargetLowering::lowerRETURNADDR.
132  // It's killed at the spill, unless the register is RA and return address
133  // is taken.
134  unsigned Reg = CSI[i].getReg();
135  bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
137  if (!IsRAAndRetAddrIsTaken)
138  EntryBlock->addLiveIn(Reg);
139  }
140 
141  return true;
142 }
143 
146  const std::vector<CalleeSavedInfo> &CSI,
147  const TargetRegisterInfo *TRI) const {
148  //
149  // Registers RA,S0,S1 are the callee saved registers and they will be restored
150  // with the restore instruction during emitEpilogue.
151  // We need to override this virtual function, otherwise llvm will try and
152  // restore the registers on it's on from the stack.
153  //
154 
155  return true;
156 }
157 
158 bool
160  const MachineFrameInfo &MFI = MF.getFrameInfo();
161  // Reserve call frame if the size of the maximum call frame fits into 15-bit
162  // immediate field and there are no variable sized objects on the stack.
163  return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();
164 }
165 
167  BitVector &SavedRegs,
168  RegScavenger *RS) const {
170  const Mips16InstrInfo &TII =
171  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
172  const MipsRegisterInfo &RI = TII.getRegisterInfo();
173  const BitVector Reserved = RI.getReservedRegs(MF);
174  bool SaveS2 = Reserved[Mips::S2];
175  if (SaveS2)
176  SavedRegs.set(Mips::S2);
177  if (hasFP(MF))
178  SavedRegs.set(Mips::S0);
179 }
180 
181 const MipsFrameLowering *
183  return new Mips16FrameLowering(ST);
184 }
Mips16FrameLowering(const MipsSubtarget &STI)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
BitVector & set()
Definition: BitVector.h:398
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
const MipsFrameLowering * createMips16FrameLowering(const MipsSubtarget &ST)
Create MipsFrameLowering objects.
const MipsSubtarget & STI
void makeFrame(unsigned SP, int64_t FrameSize, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const
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:404
const MipsInstrInfo * getInstrInfo() const override
bool isReturnAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
A debug info location.
Definition: DebugLoc.h:34
static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset)
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition: MCDwarf.h:391
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
const HexagonInstrInfo * TII
Reg
All possible values of the reg field in the ModR/M byte.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
LLVM_NODISCARD unsigned addFrameInst(const MCCFIInstruction &Inst)
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...
const MachineBasicBlock & front() const
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...
BitVector getReservedRegs(const MachineFunction &MF) const override
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
void restoreFrame(unsigned SP, int64_t FrameSize, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const
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.
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.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
#define E
Definition: LargeTest.cpp:27
const MCContext & getContext() const
unsigned getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call...
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:285
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.
#define I(x, y, z)
Definition: MD5.cpp:58
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
MachineModuleInfo & getMMI() const
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
This class contains meta information specific to a module.