blob: d9a7487bf71d6df8ca86ab5be1de41302bd6fee3 [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();
Reed Kotler27210d22012-12-20 06:59:37 +000043 MachineLocation DstML, SrcML;
44
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000045 // Adjust stack.
Reed Kotlercef95f72012-12-20 04:07:42 +000046 TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
Reed Kotlerf99998a2012-10-28 06:02:37 +000047
Reed Kotler27210d22012-12-20 06:59:37 +000048 // emit ".cfi_def_cfa_offset StackSize"
49 MCSymbol *AdjustSPLabel = MMI.getContext().CreateTempSymbol();
50 BuildMI(MBB, MBBI, dl,
51 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(AdjustSPLabel);
52 DstML = MachineLocation(MachineLocation::VirtualFP);
53 SrcML = MachineLocation(MachineLocation::VirtualFP, -StackSize);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +000054 MMI.addFrameMove(AdjustSPLabel, DstML, SrcML);
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);
59 DstML = MachineLocation(MachineLocation::VirtualFP, -8);
60 SrcML = MachineLocation(Mips::S1);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +000061 MMI.addFrameMove(CSLabel, DstML, SrcML);
Reed Kotler27210d22012-12-20 06:59:37 +000062
63 DstML = MachineLocation(MachineLocation::VirtualFP, -12);
64 SrcML = MachineLocation(Mips::S0);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +000065 MMI.addFrameMove(CSLabel, DstML, SrcML);
Reed Kotler27210d22012-12-20 06:59:37 +000066
67 DstML = MachineLocation(MachineLocation::VirtualFP, -4);
68 SrcML = MachineLocation(Mips::RA);
Rafael Espindolad84ccfa2013-05-11 02:38:11 +000069 MMI.addFrameMove(CSLabel, DstML, SrcML);
Reed Kotler27210d22012-12-20 06:59:37 +000070
Reed Kotlerf99998a2012-10-28 06:02:37 +000071 if (hasFP(MF))
72 BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
73 .addReg(Mips::SP);
74
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000075}
76
77void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
78 MachineBasicBlock &MBB) const {
79 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
80 MachineFrameInfo *MFI = MF.getFrameInfo();
Reed Kotlercef95f72012-12-20 04:07:42 +000081 const Mips16InstrInfo &TII =
82 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000083 DebugLoc dl = MBBI->getDebugLoc();
84 uint64_t StackSize = MFI->getStackSize();
85
86 if (!StackSize)
87 return;
88
Reed Kotlerf99998a2012-10-28 06:02:37 +000089 if (hasFP(MF))
90 BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
91 .addReg(Mips::S0);
92
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000093 // Adjust stack.
Reed Kotlercef95f72012-12-20 04:07:42 +000094 // assumes stacksize multiple of 8
95 TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000096}
97
98bool Mips16FrameLowering::
99spillCalleeSavedRegisters(MachineBasicBlock &MBB,
100 MachineBasicBlock::iterator MI,
101 const std::vector<CalleeSavedInfo> &CSI,
102 const TargetRegisterInfo *TRI) const {
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +0000103 MachineFunction *MF = MBB.getParent();
104 MachineBasicBlock *EntryBlock = MF->begin();
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +0000105
106 //
107 // Registers RA, S0,S1 are the callee saved registers and they
108 // will be saved with the "save" instruction
109 // during emitPrologue
110 //
111 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
112 // Add the callee-saved register as live-in. Do not add if the register is
113 // RA and return address is taken, because it has already been added in
114 // method MipsTargetLowering::LowerRETURNADDR.
115 // It's killed at the spill, unless the register is RA and return address
116 // is taken.
117 unsigned Reg = CSI[i].getReg();
118 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
119 && MF->getFrameInfo()->isReturnAddressTaken();
120 if (!IsRAAndRetAddrIsTaken)
121 EntryBlock->addLiveIn(Reg);
122 }
123
124 return true;
125}
126
127bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
128 MachineBasicBlock::iterator MI,
129 const std::vector<CalleeSavedInfo> &CSI,
130 const TargetRegisterInfo *TRI) const {
131 //
132 // Registers RA,S0,S1 are the callee saved registers and they will be restored
133 // with the restore instruction during emitEpilogue.
134 // We need to override this virtual function, otherwise llvm will try and
135 // restore the registers on it's on from the stack.
136 //
137
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000138 return true;
139}
140
Eli Bendersky700ed802013-02-21 20:05:00 +0000141// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
142void Mips16FrameLowering::
143eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
144 MachineBasicBlock::iterator I) const {
145 if (!hasReservedCallFrame(MF)) {
146 int64_t Amount = I->getOperand(0).getImm();
147
148 if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
149 Amount = -Amount;
150
151 const Mips16InstrInfo &TII =
152 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
153
154 TII.adjustStackPtr(Mips::SP, Amount, MBB, I);
155 }
156
157 MBB.erase(I);
158}
159
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000160bool
161Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Reed Kotler94411252012-10-31 05:21:10 +0000162 const MachineFrameInfo *MFI = MF.getFrameInfo();
163 // Reserve call frame if the size of the maximum call frame fits into 15-bit
164 // immediate field and there are no variable sized objects on the stack.
165 return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000166}
167
168void Mips16FrameLowering::
169processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
170 RegScavenger *RS) const {
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +0000171 MF.getRegInfo().setPhysRegUsed(Mips::RA);
172 MF.getRegInfo().setPhysRegUsed(Mips::S0);
173 MF.getRegInfo().setPhysRegUsed(Mips::S1);
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000174}
Akira Hatanakaaf266262012-08-02 18:21:47 +0000175
176const MipsFrameLowering *
177llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
178 return new Mips16FrameLowering(ST);
179}