blob: e0c51ed58731427f71ea9f65739c16cfcb513bab [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"
Reed Kotlercef95f72012-12-20 04:07:42 +000015#include "Mips16InstrInfo.h"
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000016#include "MCTargetDesc/MipsBaseInfo.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"
Micah Villmow3574eca2012-10-08 16:38:25 +000023#include "llvm/DataLayout.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/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
42 // Adjust stack.
Reed Kotlercef95f72012-12-20 04:07:42 +000043 TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
Reed Kotlerf99998a2012-10-28 06:02:37 +000044
45 if (hasFP(MF))
46 BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
47 .addReg(Mips::SP);
48
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000049}
50
51void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
52 MachineBasicBlock &MBB) const {
53 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
54 MachineFrameInfo *MFI = MF.getFrameInfo();
Reed Kotlercef95f72012-12-20 04:07:42 +000055 const Mips16InstrInfo &TII =
56 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000057 DebugLoc dl = MBBI->getDebugLoc();
58 uint64_t StackSize = MFI->getStackSize();
59
60 if (!StackSize)
61 return;
62
Reed Kotlerf99998a2012-10-28 06:02:37 +000063 if (hasFP(MF))
64 BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
65 .addReg(Mips::S0);
66
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000067 // Adjust stack.
Reed Kotlercef95f72012-12-20 04:07:42 +000068 // assumes stacksize multiple of 8
69 TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000070}
71
72bool Mips16FrameLowering::
73spillCalleeSavedRegisters(MachineBasicBlock &MBB,
74 MachineBasicBlock::iterator MI,
75 const std::vector<CalleeSavedInfo> &CSI,
76 const TargetRegisterInfo *TRI) const {
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +000077 MachineFunction *MF = MBB.getParent();
78 MachineBasicBlock *EntryBlock = MF->begin();
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +000079
80 //
81 // Registers RA, S0,S1 are the callee saved registers and they
82 // will be saved with the "save" instruction
83 // during emitPrologue
84 //
85 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
86 // Add the callee-saved register as live-in. Do not add if the register is
87 // RA and return address is taken, because it has already been added in
88 // method MipsTargetLowering::LowerRETURNADDR.
89 // It's killed at the spill, unless the register is RA and return address
90 // is taken.
91 unsigned Reg = CSI[i].getReg();
92 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
93 && MF->getFrameInfo()->isReturnAddressTaken();
94 if (!IsRAAndRetAddrIsTaken)
95 EntryBlock->addLiveIn(Reg);
96 }
97
98 return true;
99}
100
101bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
102 MachineBasicBlock::iterator MI,
103 const std::vector<CalleeSavedInfo> &CSI,
104 const TargetRegisterInfo *TRI) const {
105 //
106 // Registers RA,S0,S1 are the callee saved registers and they will be restored
107 // with the restore instruction during emitEpilogue.
108 // We need to override this virtual function, otherwise llvm will try and
109 // restore the registers on it's on from the stack.
110 //
111
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000112 return true;
113}
114
115bool
116Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Reed Kotler94411252012-10-31 05:21:10 +0000117 const MachineFrameInfo *MFI = MF.getFrameInfo();
118 // Reserve call frame if the size of the maximum call frame fits into 15-bit
119 // immediate field and there are no variable sized objects on the stack.
120 return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000121}
122
123void Mips16FrameLowering::
124processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
125 RegScavenger *RS) const {
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +0000126 MF.getRegInfo().setPhysRegUsed(Mips::RA);
127 MF.getRegInfo().setPhysRegUsed(Mips::S0);
128 MF.getRegInfo().setPhysRegUsed(Mips::S1);
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000129}
Akira Hatanakaaf266262012-08-02 18:21:47 +0000130
131const MipsFrameLowering *
132llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
133 return new Mips16FrameLowering(ST);
134}