blob: db2a924a99f9b54b348d20c386aa658d7691ff32 [file] [log] [blame]
Akira Hatanakad1c43ce2012-07-31 22:50:19 +00001//===-- 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"
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000015#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000016#include "Mips16InstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "MipsInstrInfo.h"
Reed Kotler0ff40012013-12-10 14:29:38 +000018#include "MipsRegisterInfo.h"
Eric Christopher4cdb3f92014-07-02 23:29:55 +000019#include "MipsSubtarget.h"
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/Function.h"
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000027#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Target/TargetOptions.h"
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000029
30using namespace llvm;
31
Eric Christopher4cdb3f92014-07-02 23:29:55 +000032Mips16FrameLowering::Mips16FrameLowering(const MipsSubtarget &STI)
33 : MipsFrameLowering(STI, STI.stackAlignment()) {}
34
Quentin Colombet61b305e2015-05-05 17:38:16 +000035void Mips16FrameLowering::emitPrologue(MachineFunction &MF,
36 MachineBasicBlock &MBB) const {
37 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000038 MachineFrameInfo *MFI = MF.getFrameInfo();
Reed Kotlerd019dbf2012-12-20 04:07:42 +000039 const Mips16InstrInfo &TII =
Eric Christopher96e72c62015-01-29 23:27:36 +000040 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000041 MachineBasicBlock::iterator MBBI = MBB.begin();
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
Reed Kotlerd11acc72012-12-20 06:59:37 +000048 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +000049 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Reed Kotlerd11acc72012-12-20 06:59:37 +000050 MachineLocation DstML, SrcML;
51
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000052 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +000053 TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
Reed Kotler3589dd72012-10-28 06:02:37 +000054
Reed Kotlerd11acc72012-12-20 06:59:37 +000055 // emit ".cfi_def_cfa_offset StackSize"
Rafael Espindolab1f25f12014-03-07 06:08:31 +000056 unsigned CFIIndex = MMI.addFrameInst(
57 MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
58 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
59 .addCFIIndex(CFIIndex);
Reed Kotlerd11acc72012-12-20 06:59:37 +000060
Reed Kotler5c29d632013-12-15 20:49:30 +000061 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
62
63 if (CSI.size()) {
Reed Kotler06b3c4f2013-12-15 23:03:35 +000064 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Reed Kotler0ff40012013-12-10 14:29:38 +000065
Reed Kotler06b3c4f2013-12-15 23:03:35 +000066 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
Reed Kotler5c29d632013-12-15 20:49:30 +000067 E = CSI.end(); I != E; ++I) {
Reed Kotler06b3c4f2013-12-15 23:03:35 +000068 int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
69 unsigned Reg = I->getReg();
70 unsigned DReg = MRI->getDwarfRegNum(Reg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +000071 unsigned CFIIndex = MMI.addFrameInst(
72 MCCFIInstruction::createOffset(nullptr, DReg, Offset));
73 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
74 .addCFIIndex(CFIIndex);
Reed Kotler06b3c4f2013-12-15 23:03:35 +000075 }
Reed Kotler5c29d632013-12-15 20:49:30 +000076 }
Reed Kotler3589dd72012-10-28 06:02:37 +000077 if (hasFP(MF))
78 BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
Eric Christopherb45b4812014-04-14 22:21:22 +000079 .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
Reed Kotler3589dd72012-10-28 06:02:37 +000080
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000081}
82
83void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
84 MachineBasicBlock &MBB) const {
85 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
86 MachineFrameInfo *MFI = MF.getFrameInfo();
Reed Kotlerd019dbf2012-12-20 04:07:42 +000087 const Mips16InstrInfo &TII =
Eric Christopher96e72c62015-01-29 23:27:36 +000088 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000089 DebugLoc dl = MBBI->getDebugLoc();
90 uint64_t StackSize = MFI->getStackSize();
91
92 if (!StackSize)
93 return;
94
Reed Kotler3589dd72012-10-28 06:02:37 +000095 if (hasFP(MF))
96 BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
97 .addReg(Mips::S0);
98
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000099 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000100 // assumes stacksize multiple of 8
101 TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000102}
103
104bool Mips16FrameLowering::
105spillCalleeSavedRegisters(MachineBasicBlock &MBB,
106 MachineBasicBlock::iterator MI,
107 const std::vector<CalleeSavedInfo> &CSI,
108 const TargetRegisterInfo *TRI) const {
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000109 MachineFunction *MF = MBB.getParent();
110 MachineBasicBlock *EntryBlock = MF->begin();
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000111
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)
125 && MF->getFrameInfo()->isReturnAddressTaken();
126 if (!IsRAAndRetAddrIsTaken)
127 EntryBlock->addLiveIn(Reg);
128 }
129
130 return true;
131}
132
133bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
134 MachineBasicBlock::iterator MI,
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
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000144 return true;
145}
146
147bool
148Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Reed Kotler27a72292012-10-31 05:21:10 +0000149 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();
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000153}
154
155void Mips16FrameLowering::
156processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
157 RegScavenger *RS) const {
Reed Kotler5c29d632013-12-15 20:49:30 +0000158 const Mips16InstrInfo &TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000159 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
Reed Kotler5c29d632013-12-15 20:49:30 +0000160 const MipsRegisterInfo &RI = TII.getRegisterInfo();
161 const BitVector Reserved = RI.getReservedRegs(MF);
162 bool SaveS2 = Reserved[Mips::S2];
163 if (SaveS2)
164 MF.getRegInfo().setPhysRegUsed(Mips::S2);
165 if (hasFP(MF))
166 MF.getRegInfo().setPhysRegUsed(Mips::S0);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000167}
Akira Hatanakafab89292012-08-02 18:21:47 +0000168
169const MipsFrameLowering *
170llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
171 return new Mips16FrameLowering(ST);
172}