19f610119SJia Liu //===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===//
22f931281SAnton Korobeynikov //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62f931281SAnton Korobeynikov //
7e2489125SAkira Hatanaka //===----------------------------------------------------------------------===//
82f931281SAnton Korobeynikov //
92f931281SAnton Korobeynikov // This file contains the Mips implementation of TargetFrameLowering class.
102f931281SAnton Korobeynikov //
11e2489125SAkira Hatanaka //===----------------------------------------------------------------------===//
122f931281SAnton Korobeynikov 
132f931281SAnton Korobeynikov #include "MipsFrameLowering.h"
14ed0881b2SChandler Carruth #include "MCTargetDesc/MipsBaseInfo.h"
152f931281SAnton Korobeynikov #include "MipsInstrInfo.h"
162f931281SAnton Korobeynikov #include "MipsMachineFunction.h"
17fab89294SAkira Hatanaka #include "MipsTargetMachine.h"
182f931281SAnton Korobeynikov #include "llvm/CodeGen/MachineFrameInfo.h"
192f931281SAnton Korobeynikov #include "llvm/CodeGen/MachineFunction.h"
202f931281SAnton Korobeynikov #include "llvm/CodeGen/MachineInstrBuilder.h"
212f931281SAnton Korobeynikov #include "llvm/CodeGen/MachineModuleInfo.h"
222f931281SAnton Korobeynikov #include "llvm/CodeGen/MachineRegisterInfo.h"
239fb823bbSChandler Carruth #include "llvm/IR/DataLayout.h"
249fb823bbSChandler Carruth #include "llvm/IR/Function.h"
25ed0881b2SChandler Carruth #include "llvm/Target/TargetOptions.h"
262f931281SAnton Korobeynikov 
272f931281SAnton Korobeynikov using namespace llvm;
282f931281SAnton Korobeynikov 
292f931281SAnton Korobeynikov 
30e2489125SAkira Hatanaka //===----------------------------------------------------------------------===//
312f931281SAnton Korobeynikov //
322f931281SAnton Korobeynikov // Stack Frame Processing methods
332f931281SAnton Korobeynikov // +----------------------------+
342f931281SAnton Korobeynikov //
352f931281SAnton Korobeynikov // The stack is allocated decrementing the stack pointer on
362f931281SAnton Korobeynikov // the first instruction of a function prologue. Once decremented,
372f931281SAnton Korobeynikov // all stack references are done thought a positive offset
382f931281SAnton Korobeynikov // from the stack/frame pointer, so the stack is considering
392f931281SAnton Korobeynikov // to grow up! Otherwise terrible hacks would have to be made
402f931281SAnton Korobeynikov // to get this stack ABI compliant :)
412f931281SAnton Korobeynikov //
422f931281SAnton Korobeynikov //  The stack frame required by the ABI (after call):
432f931281SAnton Korobeynikov //  Offset
442f931281SAnton Korobeynikov //
452f931281SAnton Korobeynikov //  0                 ----------
462f931281SAnton Korobeynikov //  4                 Args to pass
472f931281SAnton Korobeynikov //  .                 saved $GP  (used in PIC)
482f931281SAnton Korobeynikov //  .                 Alloca allocations
492f931281SAnton Korobeynikov //  .                 Local Area
502f931281SAnton Korobeynikov //  .                 CPU "Callee Saved" Registers
512f931281SAnton Korobeynikov //  .                 saved FP
522f931281SAnton Korobeynikov //  .                 saved RA
532f931281SAnton Korobeynikov //  .                 FPU "Callee Saved" Registers
542f931281SAnton Korobeynikov //  StackSize         -----------
552f931281SAnton Korobeynikov //
562f931281SAnton Korobeynikov // Offset - offset from sp after stack allocation on function prologue
572f931281SAnton Korobeynikov //
582f931281SAnton Korobeynikov // The sp is the stack pointer subtracted/added from the stack size
592f931281SAnton Korobeynikov // at the Prologue/Epilogue
602f931281SAnton Korobeynikov //
612f931281SAnton Korobeynikov // References to the previous stack (to obtain arguments) are done
622f931281SAnton Korobeynikov // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
632f931281SAnton Korobeynikov //
642f931281SAnton Korobeynikov // Examples:
652f931281SAnton Korobeynikov // - reference to the actual stack frame
662f931281SAnton Korobeynikov //   for any local area var there is smt like : FI >= 0, StackOffset: 4
672f931281SAnton Korobeynikov //     sw REGX, 4(SP)
682f931281SAnton Korobeynikov //
692f931281SAnton Korobeynikov // - reference to previous stack frame
702f931281SAnton Korobeynikov //   suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
712f931281SAnton Korobeynikov //   The emitted instruction will be something like:
722f931281SAnton Korobeynikov //     lw REGX, 16+StackSize(SP)
732f931281SAnton Korobeynikov //
742f931281SAnton Korobeynikov // Since the total stack size is unknown on LowerFormalArguments, all
752f931281SAnton Korobeynikov // stack references (ObjectOffset) created to reference the function
762f931281SAnton Korobeynikov // arguments, are negative numbers. This way, on eliminateFrameIndex it's
772f931281SAnton Korobeynikov // possible to detect those references and the offsets are adjusted to
782f931281SAnton Korobeynikov // their real location.
792f931281SAnton Korobeynikov //
80e2489125SAkira Hatanaka //===----------------------------------------------------------------------===//
812f931281SAnton Korobeynikov 
create(const MipsSubtarget & ST)82e54f10eeSEric Christopher const MipsFrameLowering *MipsFrameLowering::create(const MipsSubtarget &ST) {
83e54f10eeSEric Christopher   if (ST.inMips16Mode())
84fab89294SAkira Hatanaka     return llvm::createMips16FrameLowering(ST);
85fab89294SAkira Hatanaka 
86fab89294SAkira Hatanaka   return llvm::createMipsSEFrameLowering(ST);
87fab89294SAkira Hatanaka }
88fab89294SAkira Hatanaka 
892f931281SAnton Korobeynikov // hasFP - Return true if the specified function should have a dedicated frame
90bb698c7dSVasileios Kalintiris // pointer register.  This is true if the function has variable sized allocas,
91bb698c7dSVasileios Kalintiris // if it needs dynamic stack realignment, if frame pointer elimination is
92bb698c7dSVasileios Kalintiris // disabled, or if the frame address is taken.
hasFP(const MachineFunction & MF) const932f931281SAnton Korobeynikov bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
94941a705bSMatthias Braun   const MachineFrameInfo &MFI = MF.getFrameInfo();
95bb698c7dSVasileios Kalintiris   const TargetRegisterInfo *TRI = STI.getRegisterInfo();
96bb698c7dSVasileios Kalintiris 
9750f02cb2SNick Lewycky   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
98941a705bSMatthias Braun          MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() ||
99*a9968c0aSTomas Matheson          TRI->hasStackRealignment(MF);
100bb698c7dSVasileios Kalintiris }
101bb698c7dSVasileios Kalintiris 
hasBP(const MachineFunction & MF) const102bb698c7dSVasileios Kalintiris bool MipsFrameLowering::hasBP(const MachineFunction &MF) const {
103941a705bSMatthias Braun   const MachineFrameInfo &MFI = MF.getFrameInfo();
104bb698c7dSVasileios Kalintiris   const TargetRegisterInfo *TRI = STI.getRegisterInfo();
105bb698c7dSVasileios Kalintiris 
106*a9968c0aSTomas Matheson   return MFI.hasVarSizedObjects() && TRI->hasStackRealignment(MF);
1072f931281SAnton Korobeynikov }
10897b43d8bSAkira Hatanaka 
109725acb2dSSimon Dardis // Estimate the size of the stack, including the incoming arguments. We need to
110725acb2dSSimon Dardis // account for register spills, local objects, reserved call frame and incoming
111725acb2dSSimon Dardis // arguments. This is required to determine the largest possible positive offset
112725acb2dSSimon Dardis // from $sp so that it can be determined if an emergency spill slot for stack
113725acb2dSSimon Dardis // addresses is required.
estimateStackSize(const MachineFunction & MF) const11497b43d8bSAkira Hatanaka uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const {
115941a705bSMatthias Braun   const MachineFrameInfo &MFI = MF.getFrameInfo();
11696e72c6aSEric Christopher   const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
11797b43d8bSAkira Hatanaka 
118725acb2dSSimon Dardis   int64_t Size = 0;
11997b43d8bSAkira Hatanaka 
120725acb2dSSimon Dardis   // Iterate over fixed sized objects which are incoming arguments.
121941a705bSMatthias Braun   for (int I = MFI.getObjectIndexBegin(); I != 0; ++I)
122725acb2dSSimon Dardis     if (MFI.getObjectOffset(I) > 0)
123725acb2dSSimon Dardis       Size += MFI.getObjectSize(I);
12497b43d8bSAkira Hatanaka 
12597b43d8bSAkira Hatanaka   // Conservatively assume all callee-saved registers will be saved.
126840beec2SCraig Topper   for (const MCPhysReg *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) {
127725acb2dSSimon Dardis     unsigned RegSize = TRI.getSpillSize(*TRI.getMinimalPhysRegClass(*R));
128725acb2dSSimon Dardis     Size = alignTo(Size + RegSize, RegSize);
12997b43d8bSAkira Hatanaka   }
13097b43d8bSAkira Hatanaka 
131725acb2dSSimon Dardis   // Get the size of the rest of the frame objects and any possible reserved
132725acb2dSSimon Dardis   // call frame, accounting for alignment.
133725acb2dSSimon Dardis   return Size + MFI.estimateStackSize(MF);
13497b43d8bSAkira Hatanaka }
135580f067fSVasileios Kalintiris 
136580f067fSVasileios Kalintiris // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
137e1a2e90fSHans Wennborg MachineBasicBlock::iterator MipsFrameLowering::
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const138580f067fSVasileios Kalintiris eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
139580f067fSVasileios Kalintiris                               MachineBasicBlock::iterator I) const {
140580f067fSVasileios Kalintiris   unsigned SP = STI.getABI().IsN64() ? Mips::SP_64 : Mips::SP;
141580f067fSVasileios Kalintiris 
142580f067fSVasileios Kalintiris   if (!hasReservedCallFrame(MF)) {
143580f067fSVasileios Kalintiris     int64_t Amount = I->getOperand(0).getImm();
144580f067fSVasileios Kalintiris     if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
145580f067fSVasileios Kalintiris       Amount = -Amount;
146580f067fSVasileios Kalintiris 
147580f067fSVasileios Kalintiris     STI.getInstrInfo()->adjustStackPtr(SP, Amount, MBB, I);
148580f067fSVasileios Kalintiris   }
149580f067fSVasileios Kalintiris 
150e1a2e90fSHans Wennborg   return MBB.erase(I);
151580f067fSVasileios Kalintiris }
152