LLVM  3.7.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"
29 
30 using namespace llvm;
31 
33  : MipsFrameLowering(STI, STI.stackAlignment()) {}
34 
36  MachineBasicBlock &MBB) const {
37  assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
38  MachineFrameInfo *MFI = MF.getFrameInfo();
39  const Mips16InstrInfo &TII =
40  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
42  DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
43  uint64_t StackSize = MFI->getStackSize();
44 
45  // No need to allocate space on the stack.
46  if (StackSize == 0 && !MFI->adjustsStack()) return;
47 
48  MachineModuleInfo &MMI = MF.getMMI();
49  const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
50  MachineLocation DstML, SrcML;
51 
52  // Adjust stack.
53  TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
54 
55  // emit ".cfi_def_cfa_offset StackSize"
56  unsigned CFIIndex = MMI.addFrameInst(
57  MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
58  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
59  .addCFIIndex(CFIIndex);
60 
61  const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
62 
63  if (CSI.size()) {
64  const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
65 
66  for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
67  E = CSI.end(); I != E; ++I) {
68  int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
69  unsigned Reg = I->getReg();
70  unsigned DReg = MRI->getDwarfRegNum(Reg, true);
71  unsigned CFIIndex = MMI.addFrameInst(
72  MCCFIInstruction::createOffset(nullptr, DReg, Offset));
73  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
74  .addCFIIndex(CFIIndex);
75  }
76  }
77  if (hasFP(MF))
78  BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
79  .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
80 
81 }
82 
84  MachineBasicBlock &MBB) const {
86  MachineFrameInfo *MFI = MF.getFrameInfo();
87  const Mips16InstrInfo &TII =
88  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
89  DebugLoc dl = MBBI->getDebugLoc();
90  uint64_t StackSize = MFI->getStackSize();
91 
92  if (!StackSize)
93  return;
94 
95  if (hasFP(MF))
96  BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
97  .addReg(Mips::S0);
98 
99  // Adjust stack.
100  // assumes stacksize multiple of 8
101  TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
102 }
103 
107  const std::vector<CalleeSavedInfo> &CSI,
108  const TargetRegisterInfo *TRI) const {
109  MachineFunction *MF = MBB.getParent();
110  MachineBasicBlock *EntryBlock = MF->begin();
111 
112  //
113  // Registers RA, S0,S1 are the callee saved registers and they
114  // will be saved with the "save" instruction
115  // during emitPrologue
116  //
117  for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
118  // Add the callee-saved register as live-in. Do not add if the register is
119  // RA and return address is taken, because it has already been added in
120  // method MipsTargetLowering::LowerRETURNADDR.
121  // It's killed at the spill, unless the register is RA and return address
122  // is taken.
123  unsigned Reg = CSI[i].getReg();
124  bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
126  if (!IsRAAndRetAddrIsTaken)
127  EntryBlock->addLiveIn(Reg);
128  }
129 
130  return true;
131 }
132 
135  const std::vector<CalleeSavedInfo> &CSI,
136  const TargetRegisterInfo *TRI) const {
137  //
138  // Registers RA,S0,S1 are the callee saved registers and they will be restored
139  // with the restore instruction during emitEpilogue.
140  // We need to override this virtual function, otherwise llvm will try and
141  // restore the registers on it's on from the stack.
142  //
143 
144  return true;
145 }
146 
147 bool
149  const MachineFrameInfo *MFI = MF.getFrameInfo();
150  // Reserve call frame if the size of the maximum call frame fits into 15-bit
151  // immediate field and there are no variable sized objects on the stack.
152  return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
153 }
154 
156  BitVector &SavedRegs,
157  RegScavenger *RS) const {
159  const Mips16InstrInfo &TII =
160  *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
161  const MipsRegisterInfo &RI = TII.getRegisterInfo();
162  const BitVector Reserved = RI.getReservedRegs(MF);
163  bool SaveS2 = Reserved[Mips::S2];
164  if (SaveS2)
165  SavedRegs.set(Mips::S2);
166  if (hasFP(MF))
167  SavedRegs.set(Mips::S0);
168 }
169 
170 const MipsFrameLowering *
172  return new Mips16FrameLowering(ST);
173 }
Mips16FrameLowering(const MipsSubtarget &STI)
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
BitVector & set()
Definition: BitVector.h:218
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:381
const MipsInstrInfo * getInstrInfo() const override
void addLiveIn(unsigned Reg)
Adds the specified register as a live in.
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:368
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...
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...
unsigned LLVM_ATTRIBUTE_UNUSED_RESULT addFrameInst(const MCCFIInstruction &Inst)
BitVector getReservedRegs(const MachineFunction &MF) const override
iterator getLastNonDebugInstr()
getLastNonDebugInstr - returns an iterator to the last non-debug instruction in the basic block...
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...
bundle_iterator< MachineInstr, instr_iterator > iterator
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
BuildMI - Builder interface.
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...
MachineFrameInfo * getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:227
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 ...
MachineModuleInfo & getMMI() const
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
MachineModuleInfo - This class contains meta information specific to a module.