LLVM  4.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"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Function.h"
28 
29 using namespace llvm;
30 
32  : MipsFrameLowering(STI, STI.stackAlignment()) {}
33 
35  MachineBasicBlock &MBB) const {
36  assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
37  MachineFrameInfo &MFI = MF.getFrameInfo();
38  const Mips16InstrInfo &TII =
39  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
41 
42  // Debug location must be unknown since the first debug location is used
43  // to determine the end of the prologue.
44  DebugLoc dl;
45 
46  uint64_t StackSize = MFI.getStackSize();
47 
48  // No need to allocate space on the stack.
49  if (StackSize == 0 && !MFI.adjustsStack()) return;
50 
51  MachineModuleInfo &MMI = MF.getMMI();
53  MachineLocation DstML, SrcML;
54 
55  // Adjust stack.
56  TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
57 
58  // emit ".cfi_def_cfa_offset StackSize"
59  unsigned CFIIndex = MF.addFrameInst(
60  MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
61  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
62  .addCFIIndex(CFIIndex);
63 
64  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
65 
66  if (CSI.size()) {
67  const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
68 
69  for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
70  E = CSI.end(); I != E; ++I) {
71  int64_t Offset = MFI.getObjectOffset(I->getFrameIdx());
72  unsigned Reg = I->getReg();
73  unsigned DReg = MRI->getDwarfRegNum(Reg, true);
74  unsigned CFIIndex = MF.addFrameInst(
75  MCCFIInstruction::createOffset(nullptr, DReg, Offset));
76  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
77  .addCFIIndex(CFIIndex);
78  }
79  }
80  if (hasFP(MF))
81  BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
82  .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
83 
84 }
85 
87  MachineBasicBlock &MBB) const {
89  MachineFrameInfo &MFI = MF.getFrameInfo();
90  const Mips16InstrInfo &TII =
91  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
92  DebugLoc dl = MBBI->getDebugLoc();
93  uint64_t StackSize = MFI.getStackSize();
94 
95  if (!StackSize)
96  return;
97 
98  if (hasFP(MF))
99  BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
100  .addReg(Mips::S0);
101 
102  // Adjust stack.
103  // assumes stacksize multiple of 8
104  TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
105 }
106 
110  const std::vector<CalleeSavedInfo> &CSI,
111  const TargetRegisterInfo *TRI) const {
112  MachineFunction *MF = MBB.getParent();
113  MachineBasicBlock *EntryBlock = &MF->front();
114 
115  //
116  // Registers RA, S0,S1 are the callee saved registers and they
117  // will be saved with the "save" instruction
118  // during emitPrologue
119  //
120  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
121  // Add the callee-saved register as live-in. Do not add if the register is
122  // RA and return address is taken, because it has already been added in
123  // method MipsTargetLowering::lowerRETURNADDR.
124  // It's killed at the spill, unless the register is RA and return address
125  // is taken.
126  unsigned Reg = CSI[i].getReg();
127  bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
129  if (!IsRAAndRetAddrIsTaken)
130  EntryBlock->addLiveIn(Reg);
131  }
132 
133  return true;
134 }
135 
138  const std::vector<CalleeSavedInfo> &CSI,
139  const TargetRegisterInfo *TRI) const {
140  //
141  // Registers RA,S0,S1 are the callee saved registers and they will be restored
142  // with the restore instruction during emitEpilogue.
143  // We need to override this virtual function, otherwise llvm will try and
144  // restore the registers on it's on from the stack.
145  //
146 
147  return true;
148 }
149 
150 bool
152  const MachineFrameInfo &MFI = MF.getFrameInfo();
153  // Reserve call frame if the size of the maximum call frame fits into 15-bit
154  // immediate field and there are no variable sized objects on the stack.
155  return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();
156 }
157 
159  BitVector &SavedRegs,
160  RegScavenger *RS) const {
162  const Mips16InstrInfo &TII =
163  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
164  const MipsRegisterInfo &RI = TII.getRegisterInfo();
165  const BitVector Reserved = RI.getReservedRegs(MF);
166  bool SaveS2 = Reserved[Mips::S2];
167  if (SaveS2)
168  SavedRegs.set(Mips::S2);
169  if (hasFP(MF))
170  SavedRegs.set(Mips::S0);
171 }
172 
173 const MipsFrameLowering *
175  return new Mips16FrameLowering(ST);
176 }
Mips16FrameLowering(const MipsSubtarget &STI)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
BitVector & set()
Definition: BitVector.h:219
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.
size_t i
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:396
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:383
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...
MachineBasicBlock * MBB
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
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
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.
uint32_t Offset
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...
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:245
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:54
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.