blob: d17c1259e576878c752fa00ed8d7891a7940e473 [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 Carruthd04a8d42012-12-03 16:50:05 +000016#include "MipsInstrInfo.h"
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineModuleInfo.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
Micah Villmow3574eca2012-10-08 16:38:25 +000022#include "llvm/DataLayout.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Function.h"
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000024#include "llvm/Support/CommandLine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Target/TargetOptions.h"
Akira Hatanakacdb3ba72012-07-31 22:50:19 +000026
27using namespace llvm;
28
29void Mips16FrameLowering::emitPrologue(MachineFunction &MF) const {
30 MachineBasicBlock &MBB = MF.front();
31 MachineFrameInfo *MFI = MF.getFrameInfo();
32 const MipsInstrInfo &TII =
33 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
34 MachineBasicBlock::iterator MBBI = MBB.begin();
35 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
36 uint64_t StackSize = MFI->getStackSize();
37
38 // No need to allocate space on the stack.
39 if (StackSize == 0 && !MFI->adjustsStack()) return;
40
41 // Adjust stack.
42 if (isInt<16>(-StackSize))
43 BuildMI(MBB, MBBI, dl, TII.get(Mips::SaveRaF16)).addImm(StackSize);
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();
55 const MipsInstrInfo &TII =
56 *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
57 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.
68 if (isInt<16>(StackSize))
69 // assumes stacksize multiple of 8
70 BuildMI(MBB, MBBI, dl, TII.get(Mips::RestoreRaF16)).addImm(StackSize);
71}
72
73bool Mips16FrameLowering::
74spillCalleeSavedRegisters(MachineBasicBlock &MBB,
75 MachineBasicBlock::iterator MI,
76 const std::vector<CalleeSavedInfo> &CSI,
77 const TargetRegisterInfo *TRI) const {
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +000078 MachineFunction *MF = MBB.getParent();
79 MachineBasicBlock *EntryBlock = MF->begin();
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +000080
81 //
82 // Registers RA, S0,S1 are the callee saved registers and they
83 // will be saved with the "save" instruction
84 // during emitPrologue
85 //
86 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
87 // Add the callee-saved register as live-in. Do not add if the register is
88 // RA and return address is taken, because it has already been added in
89 // method MipsTargetLowering::LowerRETURNADDR.
90 // It's killed at the spill, unless the register is RA and return address
91 // is taken.
92 unsigned Reg = CSI[i].getReg();
93 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
94 && MF->getFrameInfo()->isReturnAddressTaken();
95 if (!IsRAAndRetAddrIsTaken)
96 EntryBlock->addLiveIn(Reg);
97 }
98
99 return true;
100}
101
102bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
103 MachineBasicBlock::iterator MI,
104 const std::vector<CalleeSavedInfo> &CSI,
105 const TargetRegisterInfo *TRI) const {
106 //
107 // Registers RA,S0,S1 are the callee saved registers and they will be restored
108 // with the restore instruction during emitEpilogue.
109 // We need to override this virtual function, otherwise llvm will try and
110 // restore the registers on it's on from the stack.
111 //
112
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000113 return true;
114}
115
116bool
117Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Reed Kotler94411252012-10-31 05:21:10 +0000118 const MachineFrameInfo *MFI = MF.getFrameInfo();
119 // Reserve call frame if the size of the maximum call frame fits into 15-bit
120 // immediate field and there are no variable sized objects on the stack.
121 return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000122}
123
124void Mips16FrameLowering::
125processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
126 RegScavenger *RS) const {
Akira Hatanaka0fdf3b02012-09-21 01:08:16 +0000127 MF.getRegInfo().setPhysRegUsed(Mips::RA);
128 MF.getRegInfo().setPhysRegUsed(Mips::S0);
129 MF.getRegInfo().setPhysRegUsed(Mips::S1);
Akira Hatanakacdb3ba72012-07-31 22:50:19 +0000130}
Akira Hatanakaaf266262012-08-02 18:21:47 +0000131
132const MipsFrameLowering *
133llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
134 return new Mips16FrameLowering(ST);
135}