blob: 8b8b0194eba4333f24ad1f09630954e6319386f1 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00002//
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//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00009//
Anton Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the Mips implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
Akira Hatanakae2489122011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000013
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "MipsFrameLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/MipsBaseInfo.h"
Craig Topperb25fda92012-03-17 18:46:09 +000016#include "MipsAnalyzeImmediate.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000017#include "MipsInstrInfo.h"
18#include "MipsMachineFunction.h"
Akira Hatanakafab89292012-08-02 18:21:47 +000019#include "MipsTargetMachine.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/Function.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000027#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000029
30using namespace llvm;
31
32
Akira Hatanakae2489122011-04-15 21:51:11 +000033//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000034//
35// Stack Frame Processing methods
36// +----------------------------+
37//
38// The stack is allocated decrementing the stack pointer on
39// the first instruction of a function prologue. Once decremented,
40// all stack references are done thought a positive offset
41// from the stack/frame pointer, so the stack is considering
42// to grow up! Otherwise terrible hacks would have to be made
43// to get this stack ABI compliant :)
44//
45// The stack frame required by the ABI (after call):
46// Offset
47//
48// 0 ----------
49// 4 Args to pass
50// . saved $GP (used in PIC)
51// . Alloca allocations
52// . Local Area
53// . CPU "Callee Saved" Registers
54// . saved FP
55// . saved RA
56// . FPU "Callee Saved" Registers
57// StackSize -----------
58//
59// Offset - offset from sp after stack allocation on function prologue
60//
61// The sp is the stack pointer subtracted/added from the stack size
62// at the Prologue/Epilogue
63//
64// References to the previous stack (to obtain arguments) are done
65// with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
66//
67// Examples:
68// - reference to the actual stack frame
69// for any local area var there is smt like : FI >= 0, StackOffset: 4
70// sw REGX, 4(SP)
71//
72// - reference to previous stack frame
73// suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
74// The emitted instruction will be something like:
75// lw REGX, 16+StackSize(SP)
76//
77// Since the total stack size is unknown on LowerFormalArguments, all
78// stack references (ObjectOffset) created to reference the function
79// arguments, are negative numbers. This way, on eliminateFrameIndex it's
80// possible to detect those references and the offsets are adjusted to
81// their real location.
82//
Akira Hatanakae2489122011-04-15 21:51:11 +000083//===----------------------------------------------------------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000084
Eric Christophere54f10e2014-07-18 23:33:47 +000085const MipsFrameLowering *MipsFrameLowering::create(const MipsSubtarget &ST) {
86 if (ST.inMips16Mode())
Akira Hatanakafab89292012-08-02 18:21:47 +000087 return llvm::createMips16FrameLowering(ST);
88
89 return llvm::createMipsSEFrameLowering(ST);
90}
91
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000092// hasFP - Return true if the specified function should have a dedicated frame
Akira Hatanakae2489122011-04-15 21:51:11 +000093// pointer register. This is true if the function has variable sized allocas or
94// if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000095bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000096 const MachineFrameInfo *MFI = MF.getFrameInfo();
Nick Lewycky50f02cb2011-12-02 22:16:29 +000097 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
98 MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000099}
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000100
101uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const {
102 const MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +0000103 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000104
105 int64_t Offset = 0;
106
107 // Iterate over fixed sized objects.
108 for (int I = MFI->getObjectIndexBegin(); I != 0; ++I)
109 Offset = std::max(Offset, -MFI->getObjectOffset(I));
110
111 // Conservatively assume all callee-saved registers will be saved.
Craig Topper840beec2014-04-04 05:16:06 +0000112 for (const MCPhysReg *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) {
Akira Hatanaka97b43d8b2012-11-02 21:10:22 +0000113 unsigned Size = TRI.getMinimalPhysRegClass(*R)->getSize();
114 Offset = RoundUpToAlignment(Offset + Size, Size);
115 }
116
117 unsigned MaxAlign = MFI->getMaxAlignment();
118
119 // Check that MaxAlign is not zero if there is a stack object that is not a
120 // callee-saved spill.
121 assert(!MFI->getObjectIndexEnd() || MaxAlign);
122
123 // Iterate over other objects.
124 for (unsigned I = 0, E = MFI->getObjectIndexEnd(); I != E; ++I)
125 Offset = RoundUpToAlignment(Offset + MFI->getObjectSize(I), MaxAlign);
126
127 // Call frame.
128 if (MFI->adjustsStack() && hasReservedCallFrame(MF))
129 Offset = RoundUpToAlignment(Offset + MFI->getMaxCallFrameSize(),
130 std::max(MaxAlign, getStackAlignment()));
131
132 return RoundUpToAlignment(Offset, getStackAlignment());
133}