blob: 00f890168e65e518459ce01aa55df0b597b1fca4 [file] [log] [blame]
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001//===- Mips16FrameLowering.cpp - Mips16 Frame Information -----------------===//
Akira Hatanakad1c43ce2012-07-31 22:50:19 +00002//
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
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000014#include "Mips16FrameLowering.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +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"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000020#include "llvm/ADT/BitVector.h"
21#include "llvm/CodeGen/MachineBasicBlock.h"
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000024#include "llvm/CodeGen/MachineInstr.h"
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000027#include "llvm/IR/DebugLoc.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000028#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCDwarf.h"
30#include "llvm/MC/MCRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000031#include "llvm/MC/MachineLocation.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000032#include "llvm/Support/MathExtras.h"
33#include "llvm/Target/TargetFrameLowering.h"
34#include <cassert>
35#include <cstdint>
36#include <vector>
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000037
38using namespace llvm;
39
Eric Christopher4cdb3f92014-07-02 23:29:55 +000040Mips16FrameLowering::Mips16FrameLowering(const MipsSubtarget &STI)
John Baldwin1255b162017-08-14 21:49:38 +000041 : MipsFrameLowering(STI, STI.getStackAlignment()) {}
Eric Christopher4cdb3f92014-07-02 23:29:55 +000042
Quentin Colombet61b305e2015-05-05 17:38:16 +000043void Mips16FrameLowering::emitPrologue(MachineFunction &MF,
44 MachineBasicBlock &MBB) const {
45 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Matthias Braun941a7052016-07-28 18:40:00 +000046 MachineFrameInfo &MFI = MF.getFrameInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +000047 const Mips16InstrInfo &TII =
48 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
49 MachineBasicBlock::iterator MBBI = MBB.begin();
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
Matthias Braun941a7052016-07-28 18:40:00 +000055 uint64_t StackSize = MFI.getStackSize();
Tim Northover775aaeb2015-11-05 21:54:58 +000056
57 // No need to allocate space on the stack.
Matthias Braun941a7052016-07-28 18:40:00 +000058 if (StackSize == 0 && !MFI.adjustsStack()) return;
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000059
Reed Kotlerd11acc72012-12-20 06:59:37 +000060 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +000061 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Reed Kotlerd11acc72012-12-20 06:59:37 +000062 MachineLocation DstML, SrcML;
63
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000064 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +000065 TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
Reed Kotler3589dd72012-10-28 06:02:37 +000066
Reed Kotlerd11acc72012-12-20 06:59:37 +000067 // emit ".cfi_def_cfa_offset StackSize"
Matthias Braunf23ef432016-11-30 23:48:42 +000068 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +000069 MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
70 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
71 .addCFIIndex(CFIIndex);
Reed Kotlerd11acc72012-12-20 06:59:37 +000072
Matthias Braun941a7052016-07-28 18:40:00 +000073 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Reed Kotler5c29d632013-12-15 20:49:30 +000074
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000075 if (!CSI.empty()) {
Matthias Braun941a7052016-07-28 18:40:00 +000076 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Reed Kotler0ff40012013-12-10 14:29:38 +000077
Reed Kotler06b3c4f2013-12-15 23:03:35 +000078 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
Reed Kotler5c29d632013-12-15 20:49:30 +000079 E = CSI.end(); I != E; ++I) {
Matthias Braun941a7052016-07-28 18:40:00 +000080 int64_t Offset = MFI.getObjectOffset(I->getFrameIdx());
Reed Kotler06b3c4f2013-12-15 23:03:35 +000081 unsigned Reg = I->getReg();
82 unsigned DReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +000083 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +000084 MCCFIInstruction::createOffset(nullptr, DReg, Offset));
85 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
86 .addCFIIndex(CFIIndex);
Reed Kotler06b3c4f2013-12-15 23:03:35 +000087 }
Reed Kotler5c29d632013-12-15 20:49:30 +000088 }
Reed Kotler3589dd72012-10-28 06:02:37 +000089 if (hasFP(MF))
90 BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
Eric Christopherb45b4812014-04-14 22:21:22 +000091 .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000092}
93
94void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
95 MachineBasicBlock &MBB) const {
96 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Matthias Braun941a7052016-07-28 18:40:00 +000097 MachineFrameInfo &MFI = MF.getFrameInfo();
Reed Kotlerd019dbf2012-12-20 04:07:42 +000098 const Mips16InstrInfo &TII =
Eric Christopher96e72c62015-01-29 23:27:36 +000099 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000100 DebugLoc dl = MBBI->getDebugLoc();
Matthias Braun941a7052016-07-28 18:40:00 +0000101 uint64_t StackSize = MFI.getStackSize();
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000102
103 if (!StackSize)
104 return;
105
Reed Kotler3589dd72012-10-28 06:02:37 +0000106 if (hasFP(MF))
107 BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
108 .addReg(Mips::S0);
109
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000110 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000111 // assumes stacksize multiple of 8
112 TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000113}
114
115bool Mips16FrameLowering::
116spillCalleeSavedRegisters(MachineBasicBlock &MBB,
117 MachineBasicBlock::iterator MI,
118 const std::vector<CalleeSavedInfo> &CSI,
119 const TargetRegisterInfo *TRI) const {
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000120 MachineFunction *MF = MBB.getParent();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000121 MachineBasicBlock *EntryBlock = &MF->front();
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000122
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
Daniel Sanders94ed30a2016-07-26 14:46:11 +0000131 // method MipsTargetLowering::lowerRETURNADDR.
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000132 // 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)
Matthias Braun941a7052016-07-28 18:40:00 +0000136 && MF->getFrameInfo().isReturnAddressTaken();
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000137 if (!IsRAAndRetAddrIsTaken)
138 EntryBlock->addLiveIn(Reg);
139 }
140
141 return true;
142}
143
144bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
145 MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +0000146 std::vector<CalleeSavedInfo> &CSI,
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000147 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
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000155 return true;
156}
157
158bool
159Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000160 const MachineFrameInfo &MFI = MF.getFrameInfo();
Reed Kotler27a72292012-10-31 05:21:10 +0000161 // 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.
Matthias Braun941a7052016-07-28 18:40:00 +0000163 return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000164}
165
Matthias Braun02564862015-07-14 17:17:13 +0000166void Mips16FrameLowering::determineCalleeSaves(MachineFunction &MF,
167 BitVector &SavedRegs,
168 RegScavenger *RS) const {
169 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Reed Kotler5c29d632013-12-15 20:49:30 +0000170 const Mips16InstrInfo &TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000171 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
Reed Kotler5c29d632013-12-15 20:49:30 +0000172 const MipsRegisterInfo &RI = TII.getRegisterInfo();
173 const BitVector Reserved = RI.getReservedRegs(MF);
174 bool SaveS2 = Reserved[Mips::S2];
175 if (SaveS2)
Matthias Braun02564862015-07-14 17:17:13 +0000176 SavedRegs.set(Mips::S2);
Reed Kotler5c29d632013-12-15 20:49:30 +0000177 if (hasFP(MF))
Matthias Braun02564862015-07-14 17:17:13 +0000178 SavedRegs.set(Mips::S0);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000179}
Akira Hatanakafab89292012-08-02 18:21:47 +0000180
181const MipsFrameLowering *
182llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
183 return new Mips16FrameLowering(ST);
184}