blob: e7ceca9612a92e730a392b4011bd3247b4e48b36 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetOptions.h"
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000028
29using namespace llvm;
30
Eric Christopher4cdb3f92014-07-02 23:29:55 +000031Mips16FrameLowering::Mips16FrameLowering(const MipsSubtarget &STI)
32 : MipsFrameLowering(STI, STI.stackAlignment()) {}
33
Quentin Colombet61b305e2015-05-05 17:38:16 +000034void Mips16FrameLowering::emitPrologue(MachineFunction &MF,
35 MachineBasicBlock &MBB) const {
36 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Matthias Braun941a7052016-07-28 18:40:00 +000037 MachineFrameInfo &MFI = MF.getFrameInfo();
Tim Northover775aaeb2015-11-05 21:54:58 +000038 const Mips16InstrInfo &TII =
39 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
40 MachineBasicBlock::iterator MBBI = MBB.begin();
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
Matthias Braun941a7052016-07-28 18:40:00 +000046 uint64_t StackSize = MFI.getStackSize();
Tim Northover775aaeb2015-11-05 21:54:58 +000047
48 // No need to allocate space on the stack.
Matthias Braun941a7052016-07-28 18:40:00 +000049 if (StackSize == 0 && !MFI.adjustsStack()) return;
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000050
Reed Kotlerd11acc72012-12-20 06:59:37 +000051 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +000052 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Reed Kotlerd11acc72012-12-20 06:59:37 +000053 MachineLocation DstML, SrcML;
54
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000055 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +000056 TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
Reed Kotler3589dd72012-10-28 06:02:37 +000057
Reed Kotlerd11acc72012-12-20 06:59:37 +000058 // emit ".cfi_def_cfa_offset StackSize"
Matthias Braunf23ef432016-11-30 23:48:42 +000059 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +000060 MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
61 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
62 .addCFIIndex(CFIIndex);
Reed Kotlerd11acc72012-12-20 06:59:37 +000063
Matthias Braun941a7052016-07-28 18:40:00 +000064 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Reed Kotler5c29d632013-12-15 20:49:30 +000065
66 if (CSI.size()) {
Matthias Braun941a7052016-07-28 18:40:00 +000067 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
Reed Kotler0ff40012013-12-10 14:29:38 +000068
Reed Kotler06b3c4f2013-12-15 23:03:35 +000069 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
Reed Kotler5c29d632013-12-15 20:49:30 +000070 E = CSI.end(); I != E; ++I) {
Matthias Braun941a7052016-07-28 18:40:00 +000071 int64_t Offset = MFI.getObjectOffset(I->getFrameIdx());
Reed Kotler06b3c4f2013-12-15 23:03:35 +000072 unsigned Reg = I->getReg();
73 unsigned DReg = MRI->getDwarfRegNum(Reg, true);
Matthias Braunf23ef432016-11-30 23:48:42 +000074 unsigned CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +000075 MCCFIInstruction::createOffset(nullptr, DReg, Offset));
76 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
77 .addCFIIndex(CFIIndex);
Reed Kotler06b3c4f2013-12-15 23:03:35 +000078 }
Reed Kotler5c29d632013-12-15 20:49:30 +000079 }
Reed Kotler3589dd72012-10-28 06:02:37 +000080 if (hasFP(MF))
81 BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
Eric Christopherb45b4812014-04-14 22:21:22 +000082 .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
Reed Kotler3589dd72012-10-28 06:02:37 +000083
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000084}
85
86void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
87 MachineBasicBlock &MBB) const {
88 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Matthias Braun941a7052016-07-28 18:40:00 +000089 MachineFrameInfo &MFI = MF.getFrameInfo();
Reed Kotlerd019dbf2012-12-20 04:07:42 +000090 const Mips16InstrInfo &TII =
Eric Christopher96e72c62015-01-29 23:27:36 +000091 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000092 DebugLoc dl = MBBI->getDebugLoc();
Matthias Braun941a7052016-07-28 18:40:00 +000093 uint64_t StackSize = MFI.getStackSize();
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000094
95 if (!StackSize)
96 return;
97
Reed Kotler3589dd72012-10-28 06:02:37 +000098 if (hasFP(MF))
99 BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
100 .addReg(Mips::S0);
101
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000102 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +0000103 // assumes stacksize multiple of 8
104 TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000105}
106
107bool Mips16FrameLowering::
108spillCalleeSavedRegisters(MachineBasicBlock &MBB,
109 MachineBasicBlock::iterator MI,
110 const std::vector<CalleeSavedInfo> &CSI,
111 const TargetRegisterInfo *TRI) const {
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000112 MachineFunction *MF = MBB.getParent();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000113 MachineBasicBlock *EntryBlock = &MF->front();
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000114
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
Daniel Sanders94ed30a2016-07-26 14:46:11 +0000123 // method MipsTargetLowering::lowerRETURNADDR.
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000124 // 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)
Matthias Braun941a7052016-07-28 18:40:00 +0000128 && MF->getFrameInfo().isReturnAddressTaken();
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000129 if (!IsRAAndRetAddrIsTaken)
130 EntryBlock->addLiveIn(Reg);
131 }
132
133 return true;
134}
135
136bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
137 MachineBasicBlock::iterator MI,
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
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000147 return true;
148}
149
150bool
151Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +0000152 const MachineFrameInfo &MFI = MF.getFrameInfo();
Reed Kotler27a72292012-10-31 05:21:10 +0000153 // 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.
Matthias Braun941a7052016-07-28 18:40:00 +0000155 return isInt<15>(MFI.getMaxCallFrameSize()) && !MFI.hasVarSizedObjects();
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000156}
157
Matthias Braun02564862015-07-14 17:17:13 +0000158void Mips16FrameLowering::determineCalleeSaves(MachineFunction &MF,
159 BitVector &SavedRegs,
160 RegScavenger *RS) const {
161 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Reed Kotler5c29d632013-12-15 20:49:30 +0000162 const Mips16InstrInfo &TII =
Eric Christopher96e72c62015-01-29 23:27:36 +0000163 *static_cast<const Mips16InstrInfo *>(STI.getInstrInfo());
Reed Kotler5c29d632013-12-15 20:49:30 +0000164 const MipsRegisterInfo &RI = TII.getRegisterInfo();
165 const BitVector Reserved = RI.getReservedRegs(MF);
166 bool SaveS2 = Reserved[Mips::S2];
167 if (SaveS2)
Matthias Braun02564862015-07-14 17:17:13 +0000168 SavedRegs.set(Mips::S2);
Reed Kotler5c29d632013-12-15 20:49:30 +0000169 if (hasFP(MF))
Matthias Braun02564862015-07-14 17:17:13 +0000170 SavedRegs.set(Mips::S0);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000171}
Akira Hatanakafab89292012-08-02 18:21:47 +0000172
173const MipsFrameLowering *
174llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
175 return new Mips16FrameLowering(ST);
176}