blob: fa629476e9f440fefaa6daf0ec7cf3b64d96fc85 [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"
51 MCSymbol *AdjustSPLabel = MMI.getContext().CreateTempSymbol();
52 BuildMI(MBB, MBBI, dl,
53 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(AdjustSPLabel);
Rafael Espindolab08d2c22013-05-16 21:02:15 +000054 MMI.addFrameInst(
55 MCCFIInstruction::createDefCfaOffset(AdjustSPLabel, -StackSize));
Reed Kotlerd11acc72012-12-20 06:59:37 +000056
Reed Kotler5c29d632013-12-15 20:49:30 +000057 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
58
59 if (CSI.size()) {
Reed Kotler06b3c4f2013-12-15 23:03:35 +000060 MCSymbol *CSLabel = MMI.getContext().CreateTempSymbol();
61 BuildMI(MBB, MBBI, dl,
62 TII.get(TargetOpcode::PROLOG_LABEL)).addSym(CSLabel);
Reed Kotler0ff40012013-12-10 14:29:38 +000063
64
Reed Kotler06b3c4f2013-12-15 23:03:35 +000065 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
Reed Kotler0ff40012013-12-10 14:29:38 +000066
Reed Kotler06b3c4f2013-12-15 23:03:35 +000067 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
Reed Kotler5c29d632013-12-15 20:49:30 +000068 E = CSI.end(); I != E; ++I) {
Reed Kotler06b3c4f2013-12-15 23:03:35 +000069 int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
70 unsigned Reg = I->getReg();
71 unsigned DReg = MRI->getDwarfRegNum(Reg, true);
72 MMI.addFrameInst(MCCFIInstruction::createOffset(CSLabel, DReg, Offset));
73 }
Reed Kotler5c29d632013-12-15 20:49:30 +000074 }
Reed Kotler3589dd72012-10-28 06:02:37 +000075 if (hasFP(MF))
76 BuildMI(MBB, MBBI, dl, TII.get(Mips::MoveR3216), Mips::S0)
77 .addReg(Mips::SP);
78
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000079}
80
81void Mips16FrameLowering::emitEpilogue(MachineFunction &MF,
82 MachineBasicBlock &MBB) const {
83 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
84 MachineFrameInfo *MFI = MF.getFrameInfo();
Reed Kotlerd019dbf2012-12-20 04:07:42 +000085 const Mips16InstrInfo &TII =
86 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000087 DebugLoc dl = MBBI->getDebugLoc();
88 uint64_t StackSize = MFI->getStackSize();
89
90 if (!StackSize)
91 return;
92
Reed Kotler3589dd72012-10-28 06:02:37 +000093 if (hasFP(MF))
94 BuildMI(MBB, MBBI, dl, TII.get(Mips::Move32R16), Mips::SP)
95 .addReg(Mips::S0);
96
Akira Hatanakad1c43ce2012-07-31 22:50:19 +000097 // Adjust stack.
Reed Kotlerd019dbf2012-12-20 04:07:42 +000098 // assumes stacksize multiple of 8
99 TII.restoreFrame(Mips::SP, StackSize, MBB, MBBI);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000100}
101
102bool Mips16FrameLowering::
103spillCalleeSavedRegisters(MachineBasicBlock &MBB,
104 MachineBasicBlock::iterator MI,
105 const std::vector<CalleeSavedInfo> &CSI,
106 const TargetRegisterInfo *TRI) const {
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000107 MachineFunction *MF = MBB.getParent();
108 MachineBasicBlock *EntryBlock = MF->begin();
Akira Hatanakacd04e2b2012-09-21 01:08:16 +0000109
110 //
111 // Registers RA, S0,S1 are the callee saved registers and they
112 // will be saved with the "save" instruction
113 // during emitPrologue
114 //
115 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
116 // Add the callee-saved register as live-in. Do not add if the register is
117 // RA and return address is taken, because it has already been added in
118 // method MipsTargetLowering::LowerRETURNADDR.
119 // It's killed at the spill, unless the register is RA and return address
120 // is taken.
121 unsigned Reg = CSI[i].getReg();
122 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA)
123 && MF->getFrameInfo()->isReturnAddressTaken();
124 if (!IsRAAndRetAddrIsTaken)
125 EntryBlock->addLiveIn(Reg);
126 }
127
128 return true;
129}
130
131bool Mips16FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
132 MachineBasicBlock::iterator MI,
133 const std::vector<CalleeSavedInfo> &CSI,
134 const TargetRegisterInfo *TRI) const {
135 //
136 // Registers RA,S0,S1 are the callee saved registers and they will be restored
137 // with the restore instruction during emitEpilogue.
138 // We need to override this virtual function, otherwise llvm will try and
139 // restore the registers on it's on from the stack.
140 //
141
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000142 return true;
143}
144
Eli Bendersky8da87162013-02-21 20:05:00 +0000145// Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
146void Mips16FrameLowering::
147eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
148 MachineBasicBlock::iterator I) const {
149 if (!hasReservedCallFrame(MF)) {
150 int64_t Amount = I->getOperand(0).getImm();
151
152 if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
153 Amount = -Amount;
154
155 const Mips16InstrInfo &TII =
156 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
157
158 TII.adjustStackPtr(Mips::SP, Amount, MBB, I);
159 }
160
161 MBB.erase(I);
162}
163
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000164bool
165Mips16FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Reed Kotler27a72292012-10-31 05:21:10 +0000166 const MachineFrameInfo *MFI = MF.getFrameInfo();
167 // Reserve call frame if the size of the maximum call frame fits into 15-bit
168 // immediate field and there are no variable sized objects on the stack.
169 return isInt<15>(MFI->getMaxCallFrameSize()) && !MFI->hasVarSizedObjects();
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000170}
171
172void Mips16FrameLowering::
173processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
174 RegScavenger *RS) const {
Reed Kotler5c29d632013-12-15 20:49:30 +0000175 const Mips16InstrInfo &TII =
176 *static_cast<const Mips16InstrInfo*>(MF.getTarget().getInstrInfo());
177 const MipsRegisterInfo &RI = TII.getRegisterInfo();
178 const BitVector Reserved = RI.getReservedRegs(MF);
179 bool SaveS2 = Reserved[Mips::S2];
180 if (SaveS2)
181 MF.getRegInfo().setPhysRegUsed(Mips::S2);
182 if (hasFP(MF))
183 MF.getRegInfo().setPhysRegUsed(Mips::S0);
Akira Hatanakad1c43ce2012-07-31 22:50:19 +0000184}
Akira Hatanakafab89292012-08-02 18:21:47 +0000185
186const MipsFrameLowering *
187llvm::createMips16FrameLowering(const MipsSubtarget &ST) {
188 return new Mips16FrameLowering(ST);
189}