blob: e180c49236823acda673f8ddf21d05ea0b0fa638 [file] [log] [blame]
Akira Hatanakacdb3ba72012-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 Hatanakacdb3ba72012-07-31 22:50:19 +000015#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruth58a2cbe2013-01-02 10:22:59 +000016#include "Mips16InstrInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "MipsInstrInfo.h"
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Function.h"
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000025#include "llvm/Support/CommandLine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Target/TargetOptions.h"
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000027
28using namespace llvm;
29
30void Mips16FrameLowering::emitPrologue(MachineFunction &MF) const {
31 MachineBasicBlock &MBB = MF.front();
32 MachineFrameInfo *MFI = MF.getFrameInfo();
Reed Kotlercef95f72012-12-20 04:07:42 +000033 const Mips16InstrInfo &TII =
34 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000035 MachineBasicBlock::iterator MBBI = MBB.begin();
36 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
37 uint64_t StackSize = MFI->getStackSize();
38
39 // No need to allocate space on the stack.
40 if (StackSize == 0 && !MFI->adjustsStack()) return;
41
Reed Kotler27210d22012-12-20 06:59:37 +000042 MachineModuleInfo &MMI = MF.getMMI();
Rafael Espindola6b67ffd2013-05-16 21:02:15 +000043 const MCRegisterInfo &MRI = MMI.getContext().getRegisterInfo();
Reed Kotler27210d22012-12-20 06:59:37 +000044 MachineLocation DstML, SrcML;
45
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000046 // Adjust stack.
Reed Kotlercef95f72012-12-20 04:07:42 +000047 TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
Reed Kotlerf99998a2012-10-28 06:02:37 +000048
Reed Kotler27210d22012-12-20 06:59:37 +000049 // emit ".cfi_def_cfa_offset StackSize"
50 MCSymbol *AdjustSPLabel = MMI.getContext().CreateTempSymbol();
51 BuildMI(MBB, MBBI, dl,
52 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(AdjustSPLabel);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +000053 MMI.addFrameInst(
54 MCCFIInstruction::createDefCfaOffset(AdjustSPLabel, -StackSize));
Reed Kotler27210d22012-12-20 06:59:37 +000055
56 MCSymbol *CSLabel = MMI.getContext().CreateTempSymbol();
57 BuildMI(MBB, MBBI, dl,
58 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(CSLabel);
Rafael Espindola6b67ffd2013-05-16 21:02:15 +000059 unsigned S1 = MRI.getDwarfRegNum(Mips::S1, true);
60 MMI.addFrameInst(MCCFIInstruction::createOffset(CSLabel, S1, -8));
Reed Kotler27210d22012-12-20 06:59:37 +000061
Rafael Espindola6b67ffd2013-05-16 21:02:15 +000062 unsigned S0 = MRI.getDwarfRegNum(Mips::S0, true);
63 MMI.addFrameInst(MCCFIInstruction::createOffset(CSLabel, S0, -12));
Reed Kotler27210d22012-12-20 06:59:37 +000064
Rafael Espindola6b67ffd2013-05-16 21:02:15 +000065 unsigned RA = MRI.getDwarfRegNum(Mips::RA, true);
66 MMI.addFrameInst(MCCFIInstruction::createOffset(CSLabel, RA, -4));
Reed Kotler27210d22012-12-20 06:59:37 +000067
Reed Kotlerf99998a2012-10-28 06:02:37 +000068 if (hasFP(MF))
69 BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
70 .addReg(Mips::SP);
71
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000072}
73
74void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
75 MachineBasicBlock &MBB) const {
76 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
77 MachineFrameInfo *MFI = MF.getFrameInfo();
Reed Kotlercef95f72012-12-20 04:07:42 +000078 const Mips16InstrInfo &TII =
79 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000080 DebugLoc dl = MBBI->getDebugLoc();
81 uint64_t StackSize = MFI->getStackSize();
82
83 if (!StackSize)
84 return;
85
Reed Kotlerf99998a2012-10-28 06:02:37 +000086 if (hasFP(MF))
87 BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
88 .addReg(Mips::S0);
89
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000090 // Adjust stack.
Reed Kotlercef95f72012-12-20 04:07:42 +000091 // assumes stacksize multiple of 8
92 TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000093}
94
95bool Mips16FrameLowering::
96spillCalleeSavedRegisters(MachineBasicBlock &MBB,
97 MachineBasicBlock::iterator MI,
98 const std::vector<CalleeSavedInfo> &CSI,
99 const TargetRegisterInfo *TRI) const {
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +0000100 MachineFunction *MF = MBB.getParent();
101 MachineBasicBlock *EntryBlock = MF->begin();
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +0000102
103 //
104 // Registers RA, S0,S1 are the callee saved registers and they
105 // will be saved with the "save" instruction
106 // during emitPrologue
107 //
108 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
109 // Add the callee-saved register as live-in. Do not add if the register is
110 // RA and return address is taken, because it has already been added in
111 // method MipsTargetLowering::LowerRETURNADDR.
112 // It's killed at the spill, unless the register is RA and return address
113 // is taken.
114 unsigned Reg = CSI[i].getReg();
115 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
116 && MF->getFrameInfo()->isReturnAddressTaken();
117 if (!IsRAAndRetAddrIsTaken)
118 EntryBlock->addLiveIn(Reg);
119 }
120
121 return true;
122}
123
124bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
125 MachineBasicBlock::iterator MI,
126 const std::vector<CalleeSavedInfo> &CSI,
127 const TargetRegisterInfo *TRI) const {
128 //
129 // Registers RA,S0,S1 are the callee saved registers and they will be restored
130 // with the restore instruction during emitEpilogue.
131 // We need to override this virtual function, otherwise llvm will try and
132 // restore the registers on it's on from the stack.
133 //
134
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000135 return true;
136}
137
Eli Bendersky700ed802013-02-21 20:05:00 +0000138// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
139void Mips16FrameLowering::
140eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
141 MachineBasicBlock::iterator I) const {
142 if (!hasReservedCallFrame(MF)) {
143 int64_t Amount = I->getOperand(0).getImm();
144
145 if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
146 Amount = -Amount;
147
148 const Mips16InstrInfo &TII =
149 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
150
151 TII.adjustStackPtr(Mips::SP, Amount, MBB, I);
152 }
153
154 MBB.erase(I);
155}
156
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000157bool
158Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Reed Kotler94411252012-10-31 05:21:10 +0000159 const MachineFrameInfo *MFI = MF.getFrameInfo();
160 // 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.
162 return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000163}
164
165void Mips16FrameLowering::
166processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
167 RegScavenger *RS) const {
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +0000168 MF.getRegInfo().setPhysRegUsed(Mips::RA);
169 MF.getRegInfo().setPhysRegUsed(Mips::S0);
170 MF.getRegInfo().setPhysRegUsed(Mips::S1);
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000171}
Akira Hatanakaaf266262012-08-02 18:21:47 +0000172
173const MipsFrameLowering *
174llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
175 return new Mips16FrameLowering(ST);
176}