blob: cb59e2ddb1c6a2bfc145a8497d7e271388a2c24d [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"
David Blaikie1be62f02017-11-03 22:32:11 +000033#include "llvm/CodeGen/TargetFrameLowering.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000034#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
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000063 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +000064 TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
Reed Kotler3589dd72012-10-28 06:02:37 +000065
Reed Kotlerd11acc72012-12-20 06:59:37 +000066 // emit ".cfi_def_cfa_offset StackSize"
Matthias Braunf23ef432016-11-30 23:48:42 +000067 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +000068 MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
69 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
70 .addCFIIndex(CFIIndex);
Reed Kotlerd11acc72012-12-20 06:59:37 +000071
Matthias Braun941a7052016-07-28 18:40:00 +000072 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Reed Kotler5c29d632013-12-15 20:49:30 +000073
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000074 if (!CSI.empty()) {
Matthias Braun941a7052016-07-28 18:40:00 +000075 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Reed Kotler0ff40012013-12-10 14:29:38 +000076
Reed Kotler06b3c4f2013-12-15 23:03:35 +000077 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
Reed Kotler5c29d632013-12-15 20:49:30 +000078 E = CSI.end(); I != E; ++I) {
Matthias Braun941a7052016-07-28 18:40:00 +000079 int64_t Offset = MFI.getObjectOffset(I->getFrameIdx());
Reed Kotler06b3c4f2013-12-15 23:03:35 +000080 unsigned Reg = I->getReg();
81 unsigned DReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +000082 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +000083 MCCFIInstruction::createOffset(nullptr, DReg, Offset));
84 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
85 .addCFIIndex(CFIIndex);
Reed Kotler06b3c4f2013-12-15 23:03:35 +000086 }
Reed Kotler5c29d632013-12-15 20:49:30 +000087 }
Reed Kotler3589dd72012-10-28 06:02:37 +000088 if (hasFP(MF))
89 BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
Eric Christopherb45b4812014-04-14 22:21:22 +000090 .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000091}
92
93void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
94 MachineBasicBlock &MBB) const {
95 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Matthias Braun941a7052016-07-28 18:40:00 +000096 MachineFrameInfo &MFI = MF.getFrameInfo();
Reed Kotlerd019dbf2012-12-20 04:07:42 +000097 const Mips16InstrInfo &TII =
Eric Christopher96e72c62015-01-29 23:27:36 +000098 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000099 DebugLoc dl = MBBI->getDebugLoc();
Matthias Braun941a7052016-07-28 18:40:00 +0000100 uint64_t StackSize = MFI.getStackSize();
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000101
102 if (!StackSize)
103 return;
104
Reed Kotler3589dd72012-10-28 06:02:37 +0000105 if (hasFP(MF))
106 BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
107 .addReg(Mips::S0);
108
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000109 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000110 // assumes stacksize multiple of 8
111 TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000112}
113
114bool Mips16FrameLowering::
115spillCalleeSavedRegisters(MachineBasicBlock &MBB,
116 MachineBasicBlock::iterator MI,
117 const std::vector<CalleeSavedInfo> &CSI,
118 const TargetRegisterInfo *TRI) const {
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000119 MachineFunction *MF = MBB.getParent();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000120 MachineBasicBlock *EntryBlock = &MF->front();
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000121
122 //
123 // Registers RA, S0,S1 are the callee saved registers and they
124 // will be saved with the "save" instruction
125 // during emitPrologue
126 //
127 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
128 // Add the callee-saved register as live-in. Do not add if the register is
129 // RA and return address is taken, because it has already been added in
Daniel Sanders94ed30a2016-07-26 14:46:11 +0000130 // method MipsTargetLowering::lowerRETURNADDR.
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000131 // It's killed at the spill, unless the register is RA and return address
132 // is taken.
133 unsigned Reg = CSI[i].getReg();
134 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
Matthias Braun941a7052016-07-28 18:40:00 +0000135 && MF->getFrameInfo().isReturnAddressTaken();
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000136 if (!IsRAAndRetAddrIsTaken)
137 EntryBlock->addLiveIn(Reg);
138 }
139
140 return true;
141}
142
143bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
144 MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +0000145 std::vector<CalleeSavedInfo> &CSI,
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000146 const TargetRegisterInfo *TRI) const {
147 //
148 // Registers RA,S0,S1 are the callee saved registers and they will be restored
149 // with the restore instruction during emitEpilogue.
150 // We need to override this virtual function, otherwise llvm will try and
151 // restore the registers on it's on from the stack.
152 //
153
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000154 return true;
155}
156
157bool
158Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000159 const MachineFrameInfo &MFI = MF.getFrameInfo();
Reed Kotler27a72292012-10-31 05:21:10 +0000160 // Reserve call frame if the size of the maximum call frame fits into 15-bit
161 // immediate field and there are no variable sized objects on the stack.
Matthias Braun941a7052016-07-28 18:40:00 +0000162 return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000163}
164
Matthias Braun02564862015-07-14 17:17:13 +0000165void Mips16FrameLowering::determineCalleeSaves(MachineFunction &MF,
166 BitVector &SavedRegs,
167 RegScavenger *RS) const {
168 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Reed Kotler5c29d632013-12-15 20:49:30 +0000169 const Mips16InstrInfo &TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000170 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
Reed Kotler5c29d632013-12-15 20:49:30 +0000171 const MipsRegisterInfo &RI = TII.getRegisterInfo();
172 const BitVector Reserved = RI.getReservedRegs(MF);
173 bool SaveS2 = Reserved[Mips::S2];
174 if (SaveS2)
Matthias Braun02564862015-07-14 17:17:13 +0000175 SavedRegs.set(Mips::S2);
Reed Kotler5c29d632013-12-15 20:49:30 +0000176 if (hasFP(MF))
Matthias Braun02564862015-07-14 17:17:13 +0000177 SavedRegs.set(Mips::S0);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000178}
Akira Hatanakafab89292012-08-02 18:21:47 +0000179
180const MipsFrameLowering *
181llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
182 return new Mips16FrameLowering(ST);
183}