xref: /openbsd-src/gnu/llvm/llvm/lib/Target/Mips/MipsFrameLowering.cpp (revision a0747c9f67a4ae71ccb71e62a28d1ea19e06a63c)
1 //===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the Mips implementation of TargetFrameLowering class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "MipsFrameLowering.h"
14 #include "MCTargetDesc/MipsBaseInfo.h"
15 #include "MipsInstrInfo.h"
16 #include "MipsMachineFunction.h"
17 #include "MipsReturnProtectorLowering.h"
18 #include "MipsTargetMachine.h"
19 #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"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/Target/TargetOptions.h"
27 
28 using namespace llvm;
29 
30 
31 //===----------------------------------------------------------------------===//
32 //
33 // Stack Frame Processing methods
34 // +----------------------------+
35 //
36 // The stack is allocated decrementing the stack pointer on
37 // the first instruction of a function prologue. Once decremented,
38 // all stack references are done thought a positive offset
39 // from the stack/frame pointer, so the stack is considering
40 // to grow up! Otherwise terrible hacks would have to be made
41 // to get this stack ABI compliant :)
42 //
43 //  The stack frame required by the ABI (after call):
44 //  Offset
45 //
46 //  0                 ----------
47 //  4                 Args to pass
48 //  .                 saved $GP  (used in PIC)
49 //  .                 Alloca allocations
50 //  .                 Local Area
51 //  .                 CPU "Callee Saved" Registers
52 //  .                 saved FP
53 //  .                 saved RA
54 //  .                 FPU "Callee Saved" Registers
55 //  StackSize         -----------
56 //
57 // Offset - offset from sp after stack allocation on function prologue
58 //
59 // The sp is the stack pointer subtracted/added from the stack size
60 // at the Prologue/Epilogue
61 //
62 // References to the previous stack (to obtain arguments) are done
63 // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
64 //
65 // Examples:
66 // - reference to the actual stack frame
67 //   for any local area var there is smt like : FI >= 0, StackOffset: 4
68 //     sw REGX, 4(SP)
69 //
70 // - reference to previous stack frame
71 //   suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
72 //   The emitted instruction will be something like:
73 //     lw REGX, 16+StackSize(SP)
74 //
75 // Since the total stack size is unknown on LowerFormalArguments, all
76 // stack references (ObjectOffset) created to reference the function
77 // arguments, are negative numbers. This way, on eliminateFrameIndex it's
78 // possible to detect those references and the offsets are adjusted to
79 // their real location.
80 //
81 //===----------------------------------------------------------------------===//
82 
create(const MipsSubtarget & ST)83 const MipsFrameLowering *MipsFrameLowering::create(const MipsSubtarget &ST) {
84   if (ST.inMips16Mode())
85     return llvm::createMips16FrameLowering(ST);
86 
87   return llvm::createMipsSEFrameLowering(ST);
88 }
89 
90 // hasFP - Return true if the specified function should have a dedicated frame
91 // pointer register.  This is true if the function has variable sized allocas,
92 // if it needs dynamic stack realignment, if frame pointer elimination is
93 // disabled, or if the frame address is taken.
hasFP(const MachineFunction & MF) const94 bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
95   const MachineFrameInfo &MFI = MF.getFrameInfo();
96   const TargetRegisterInfo *TRI = STI.getRegisterInfo();
97 
98   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
99          MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() ||
100          TRI->hasStackRealignment(MF);
101 }
102 
hasBP(const MachineFunction & MF) const103 bool MipsFrameLowering::hasBP(const MachineFunction &MF) const {
104   const MachineFrameInfo &MFI = MF.getFrameInfo();
105   const TargetRegisterInfo *TRI = STI.getRegisterInfo();
106 
107   return MFI.hasVarSizedObjects() && TRI->hasStackRealignment(MF);
108 }
109 
110 // Estimate the size of the stack, including the incoming arguments. We need to
111 // account for register spills, local objects, reserved call frame and incoming
112 // arguments. This is required to determine the largest possible positive offset
113 // from $sp so that it can be determined if an emergency spill slot for stack
114 // addresses is required.
estimateStackSize(const MachineFunction & MF) const115 uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const {
116   const MachineFrameInfo &MFI = MF.getFrameInfo();
117   const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
118 
119   int64_t Size = 0;
120 
121   // Iterate over fixed sized objects which are incoming arguments.
122   for (int I = MFI.getObjectIndexBegin(); I != 0; ++I)
123     if (MFI.getObjectOffset(I) > 0)
124       Size += MFI.getObjectSize(I);
125 
126   // Account for saving return protector register
127   if (MFI.getReturnProtectorNeeded())
128     Size += TRI.getSpillSize(*TRI.getMinimalPhysRegClass(Mips::T9_64));
129 
130   // Conservatively assume all callee-saved registers will be saved.
131   for (const MCPhysReg *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) {
132     unsigned RegSize = TRI.getSpillSize(*TRI.getMinimalPhysRegClass(*R));
133     Size = alignTo(Size + RegSize, RegSize);
134   }
135 
136   // Get the size of the rest of the frame objects and any possible reserved
137   // call frame, accounting for alignment.
138   return Size + MFI.estimateStackSize(MF);
139 }
140 
141 // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
142 MachineBasicBlock::iterator MipsFrameLowering::
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const143 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
144                               MachineBasicBlock::iterator I) const {
145   unsigned SP = STI.getABI().IsN64() ? Mips::SP_64 : Mips::SP;
146 
147   if (!hasReservedCallFrame(MF)) {
148     int64_t Amount = I->getOperand(0).getImm();
149     if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
150       Amount = -Amount;
151 
152     STI.getInstrInfo()->adjustStackPtr(SP, Amount, MBB, I);
153   }
154 
155   return MBB.erase(I);
156 }
157 
getReturnProtector() const158 const ReturnProtectorLowering *MipsFrameLowering::getReturnProtector() const {
159   return &RPL;
160 }
161