blob: c01d03a1ddf6d8bb13f8b4dfa4984da0d5c4cb3a [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"
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineModuleInfo.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/Function.h"
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000026#include "llvm/Support/CommandLine.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
31void Mips16FrameLowering::emitPrologue(MachineFunction &MF) const {
32 MachineBasicBlock &MBB = MF.front();
33 MachineFrameInfo *MFI = MF.getFrameInfo();
Reed Kotlerd019dbf2012-12-20 04:07:42 +000034 const Mips16InstrInfo &TII =
35 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000036 MachineBasicBlock::iterator MBBI = MBB.begin();
37 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
38 uint64_t StackSize = MFI->getStackSize();
39
40 // No need to allocate space on the stack.
41 if (StackSize == 0 && !MFI->adjustsStack()) return;
42
Reed Kotlerd11acc72012-12-20 06:59:37 +000043 MachineModuleInfo &MMI = MF.getMMI();
Bill Wendlingbc07a892013-06-18 07:20:20 +000044 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
Reed Kotlerd11acc72012-12-20 06:59:37 +000045 MachineLocation DstML, SrcML;
46
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000047 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +000048 TII.makeFrame(Mips::SP, StackSize, MBB, MBBI);
Reed Kotler3589dd72012-10-28 06:02:37 +000049
Reed Kotlerd11acc72012-12-20 06:59:37 +000050 // emit ".cfi_def_cfa_offset StackSize"
Rafael Espindolab1f25f12014-03-07 06:08:31 +000051 unsigned CFIIndex = MMI.addFrameInst(
52 MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
53 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
54 .addCFIIndex(CFIIndex);
Reed Kotlerd11acc72012-12-20 06:59:37 +000055
Reed Kotler5c29d632013-12-15 20:49:30 +000056 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
57
58 if (CSI.size()) {
Reed Kotler06b3c4f2013-12-15 23:03:35 +000059 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Reed Kotler0ff40012013-12-10 14:29:38 +000060
Reed Kotler06b3c4f2013-12-15 23:03:35 +000061 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
Reed Kotler5c29d632013-12-15 20:49:30 +000062 E = CSI.end(); I != E; ++I) {
Reed Kotler06b3c4f2013-12-15 23:03:35 +000063 int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
64 unsigned Reg = I->getReg();
65 unsigned DReg = MRI->getDwarfRegNum(Reg, true);
Rafael Espindolab1f25f12014-03-07 06:08:31 +000066 unsigned CFIIndex = MMI.addFrameInst(
67 MCCFIInstruction::createOffset(nullptr, DReg, Offset));
68 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
69 .addCFIIndex(CFIIndex);
Reed Kotler06b3c4f2013-12-15 23:03:35 +000070 }
Reed Kotler5c29d632013-12-15 20:49:30 +000071 }
Reed Kotler3589dd72012-10-28 06:02:37 +000072 if (hasFP(MF))
73 BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
Eric Christopherb45b4812014-04-14 22:21:22 +000074 .addReg(Mips::SP).setMIFlag(MachineInstr::FrameSetup);
Reed Kotler3589dd72012-10-28 06:02:37 +000075
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000076}
77
78void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
79 MachineBasicBlock &MBB) const {
80 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
81 MachineFrameInfo *MFI = MF.getFrameInfo();
Reed Kotlerd019dbf2012-12-20 04:07:42 +000082 const Mips16InstrInfo &TII =
83 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000084 DebugLoc dl = MBBI->getDebugLoc();
85 uint64_t StackSize = MFI->getStackSize();
86
87 if (!StackSize)
88 return;
89
Reed Kotler3589dd72012-10-28 06:02:37 +000090 if (hasFP(MF))
91 BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
92 .addReg(Mips::S0);
93
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000094 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +000095 // assumes stacksize multiple of 8
96 TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000097}
98
99bool Mips16FrameLowering::
100spillCalleeSavedRegisters(MachineBasicBlock &MBB,
101 MachineBasicBlock::iterator MI,
102 const std::vector<CalleeSavedInfo> &CSI,
103 const TargetRegisterInfo *TRI) const {
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000104 MachineFunction *MF = MBB.getParent();
105 MachineBasicBlock *EntryBlock = MF->begin();
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000106
107 //
108 // Registers RA, S0,S1 are the callee saved registers and they
109 // will be saved with the "save" instruction
110 // during emitPrologue
111 //
112 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
113 // Add the callee-saved register as live-in. Do not add if the register is
114 // RA and return address is taken, because it has already been added in
115 // method MipsTargetLowering::LowerRETURNADDR.
116 // It's killed at the spill, unless the register is RA and return address
117 // is taken.
118 unsigned Reg = CSI[i].getReg();
119 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
120 && MF->getFrameInfo()->isReturnAddressTaken();
121 if (!IsRAAndRetAddrIsTaken)
122 EntryBlock->addLiveIn(Reg);
123 }
124
125 return true;
126}
127
128bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
129 MachineBasicBlock::iterator MI,
130 const std::vector<CalleeSavedInfo> &CSI,
131 const TargetRegisterInfo *TRI) const {
132 //
133 // Registers RA,S0,S1 are the callee saved registers and they will be restored
134 // with the restore instruction during emitEpilogue.
135 // We need to override this virtual function, otherwise llvm will try and
136 // restore the registers on it's on from the stack.
137 //
138
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000139 return true;
140}
141
Eli Bendersky8da87162013-02-21 20:05:00 +0000142// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
143void Mips16FrameLowering::
144eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
145 MachineBasicBlock::iterator I) const {
146 if (!hasReservedCallFrame(MF)) {
147 int64_t Amount = I->getOperand(0).getImm();
148
149 if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
150 Amount = -Amount;
151
152 const Mips16InstrInfo &TII =
153 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
154
155 TII.adjustStackPtr(Mips::SP, Amount, MBB, I);
156 }
157
158 MBB.erase(I);
159}
160
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000161bool
162Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Reed Kotler27a72292012-10-31 05:21:10 +0000163 const MachineFrameInfo *MFI = MF.getFrameInfo();
164 // Reserve call frame if the size of the maximum call frame fits into 15-bit
165 // immediate field and there are no variable sized objects on the stack.
166 return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000167}
168
169void Mips16FrameLowering::
170processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
171 RegScavenger *RS) const {
Reed Kotler5c29d632013-12-15 20:49:30 +0000172 const Mips16InstrInfo &TII =
173 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
174 const MipsRegisterInfo &RI = TII.getRegisterInfo();
175 const BitVector Reserved = RI.getReservedRegs(MF);
176 bool SaveS2 = Reserved[Mips::S2];
177 if (SaveS2)
178 MF.getRegInfo().setPhysRegUsed(Mips::S2);
179 if (hasFP(MF))
180 MF.getRegInfo().setPhysRegUsed(Mips::S0);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000181}
Akira Hatanakafab89292012-08-02 18:21:47 +0000182
183const MipsFrameLowering *
184llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
185 return new Mips16FrameLowering(ST);
186}