1 //===-- MachineFrameInfo.cpp ---------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 /// \file Implements MachineFrameInfo that manages the stack frame.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/MachineFrameInfo.h"
15 #include "llvm/ADT/BitVector.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/TargetFrameLowering.h"
19 #include "llvm/CodeGen/TargetInstrInfo.h"
20 #include "llvm/CodeGen/TargetRegisterInfo.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/Config/llvm-config.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "codegen"
31 void MachineFrameInfo::ensureMaxAlignment(unsigned Align
) {
32 if (!StackRealignable
)
33 assert(Align
<= StackAlignment
&&
34 "For targets without stack realignment, Align is out of limit!");
35 if (MaxAlignment
< Align
) MaxAlignment
= Align
;
38 /// Clamp the alignment if requested and emit a warning.
39 static inline unsigned clampStackAlignment(bool ShouldClamp
, unsigned Align
,
40 unsigned StackAlign
) {
41 if (!ShouldClamp
|| Align
<= StackAlign
)
43 LLVM_DEBUG(dbgs() << "Warning: requested alignment " << Align
44 << " exceeds the stack alignment " << StackAlign
45 << " when stack realignment is off" << '\n');
49 int MachineFrameInfo::CreateStackObject(uint64_t Size
, unsigned Alignment
,
51 const AllocaInst
*Alloca
,
53 assert(Size
!= 0 && "Cannot allocate zero size stack objects!");
54 Alignment
= clampStackAlignment(!StackRealignable
, Alignment
, StackAlignment
);
55 Objects
.push_back(StackObject(Size
, Alignment
, 0, false, IsSpillSlot
, Alloca
,
56 !IsSpillSlot
, StackID
));
57 int Index
= (int)Objects
.size() - NumFixedObjects
- 1;
58 assert(Index
>= 0 && "Bad frame index!");
60 ensureMaxAlignment(Alignment
);
64 int MachineFrameInfo::CreateSpillStackObject(uint64_t Size
,
66 Alignment
= clampStackAlignment(!StackRealignable
, Alignment
, StackAlignment
);
67 CreateStackObject(Size
, Alignment
, true);
68 int Index
= (int)Objects
.size() - NumFixedObjects
- 1;
69 ensureMaxAlignment(Alignment
);
73 int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment
,
74 const AllocaInst
*Alloca
) {
75 HasVarSizedObjects
= true;
76 Alignment
= clampStackAlignment(!StackRealignable
, Alignment
, StackAlignment
);
77 Objects
.push_back(StackObject(0, Alignment
, 0, false, false, Alloca
, true));
78 ensureMaxAlignment(Alignment
);
79 return (int)Objects
.size()-NumFixedObjects
-1;
82 int MachineFrameInfo::CreateFixedObject(uint64_t Size
, int64_t SPOffset
,
83 bool IsImmutable
, bool IsAliased
) {
84 assert(Size
!= 0 && "Cannot allocate zero size fixed stack objects!");
85 // The alignment of the frame index can be determined from its offset from
86 // the incoming frame position. If the frame object is at offset 32 and
87 // the stack is guaranteed to be 16-byte aligned, then we know that the
88 // object is 16-byte aligned. Note that unlike the non-fixed case, if the
89 // stack needs realignment, we can't assume that the stack will in fact be
91 unsigned Alignment
= MinAlign(SPOffset
, ForcedRealign
? 1 : StackAlignment
);
92 Alignment
= clampStackAlignment(!StackRealignable
, Alignment
, StackAlignment
);
93 Objects
.insert(Objects
.begin(),
94 StackObject(Size
, Alignment
, SPOffset
, IsImmutable
,
95 /*IsSpillSlot=*/false, /*Alloca=*/nullptr,
97 return -++NumFixedObjects
;
100 int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size
,
103 unsigned Alignment
= MinAlign(SPOffset
, ForcedRealign
? 1 : StackAlignment
);
104 Alignment
= clampStackAlignment(!StackRealignable
, Alignment
, StackAlignment
);
105 Objects
.insert(Objects
.begin(),
106 StackObject(Size
, Alignment
, SPOffset
, IsImmutable
,
107 /*IsSpillSlot=*/true, /*Alloca=*/nullptr,
108 /*IsAliased=*/false));
109 return -++NumFixedObjects
;
112 BitVector
MachineFrameInfo::getPristineRegs(const MachineFunction
&MF
) const {
113 const TargetRegisterInfo
*TRI
= MF
.getSubtarget().getRegisterInfo();
114 BitVector
BV(TRI
->getNumRegs());
116 // Before CSI is calculated, no registers are considered pristine. They can be
117 // freely used and PEI will make sure they are saved.
118 if (!isCalleeSavedInfoValid())
121 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
122 for (const MCPhysReg
*CSR
= MRI
.getCalleeSavedRegs(); CSR
&& *CSR
;
126 // Saved CSRs are not pristine.
127 for (auto &I
: getCalleeSavedInfo())
128 for (MCSubRegIterator
S(I
.getReg(), TRI
, true); S
.isValid(); ++S
)
134 unsigned MachineFrameInfo::estimateStackSize(const MachineFunction
&MF
) const {
135 const TargetFrameLowering
*TFI
= MF
.getSubtarget().getFrameLowering();
136 const TargetRegisterInfo
*RegInfo
= MF
.getSubtarget().getRegisterInfo();
137 unsigned MaxAlign
= getMaxAlignment();
140 // This code is very, very similar to PEI::calculateFrameObjectOffsets().
141 // It really should be refactored to share code. Until then, changes
142 // should keep in mind that there's tight coupling between the two.
144 for (int i
= getObjectIndexBegin(); i
!= 0; ++i
) {
145 // Only estimate stack size of default stack.
146 if (getStackID(i
) != TargetStackID::Default
)
148 int FixedOff
= -getObjectOffset(i
);
149 if (FixedOff
> Offset
) Offset
= FixedOff
;
151 for (unsigned i
= 0, e
= getObjectIndexEnd(); i
!= e
; ++i
) {
152 // Only estimate stack size of live objects on default stack.
153 if (isDeadObjectIndex(i
) || getStackID(i
) != TargetStackID::Default
)
155 Offset
+= getObjectSize(i
);
156 unsigned Align
= getObjectAlignment(i
);
157 // Adjust to alignment boundary
158 Offset
= (Offset
+Align
-1)/Align
*Align
;
160 MaxAlign
= std::max(Align
, MaxAlign
);
163 if (adjustsStack() && TFI
->hasReservedCallFrame(MF
))
164 Offset
+= getMaxCallFrameSize();
166 // Round up the size to a multiple of the alignment. If the function has
167 // any calls or alloca's, align to the target's StackAlignment value to
168 // ensure that the callee's frame or the alloca data is suitably aligned;
169 // otherwise, for leaf functions, align to the TransientStackAlignment
172 if (adjustsStack() || hasVarSizedObjects() ||
173 (RegInfo
->needsStackRealignment(MF
) && getObjectIndexEnd() != 0))
174 StackAlign
= TFI
->getStackAlignment();
176 StackAlign
= TFI
->getTransientStackAlignment();
178 // If the frame pointer is eliminated, all frame offsets will be relative to
179 // SP not FP. Align to MaxAlign so this works.
180 StackAlign
= std::max(StackAlign
, MaxAlign
);
181 unsigned AlignMask
= StackAlign
- 1;
182 Offset
= (Offset
+ AlignMask
) & ~uint64_t(AlignMask
);
184 return (unsigned)Offset
;
187 void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction
&MF
) {
188 const TargetInstrInfo
&TII
= *MF
.getSubtarget().getInstrInfo();
189 unsigned FrameSetupOpcode
= TII
.getCallFrameSetupOpcode();
190 unsigned FrameDestroyOpcode
= TII
.getCallFrameDestroyOpcode();
191 assert(FrameSetupOpcode
!= ~0u && FrameDestroyOpcode
!= ~0u &&
192 "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
194 MaxCallFrameSize
= 0;
195 for (const MachineBasicBlock
&MBB
: MF
) {
196 for (const MachineInstr
&MI
: MBB
) {
197 unsigned Opcode
= MI
.getOpcode();
198 if (Opcode
== FrameSetupOpcode
|| Opcode
== FrameDestroyOpcode
) {
199 unsigned Size
= TII
.getFrameSize(MI
);
200 MaxCallFrameSize
= std::max(MaxCallFrameSize
, Size
);
202 } else if (MI
.isInlineAsm()) {
203 // Some inline asm's need a stack frame, as indicated by operand 1.
204 unsigned ExtraInfo
= MI
.getOperand(InlineAsm::MIOp_ExtraInfo
).getImm();
205 if (ExtraInfo
& InlineAsm::Extra_IsAlignStack
)
212 void MachineFrameInfo::print(const MachineFunction
&MF
, raw_ostream
&OS
) const{
213 if (Objects
.empty()) return;
215 const TargetFrameLowering
*FI
= MF
.getSubtarget().getFrameLowering();
216 int ValOffset
= (FI
? FI
->getOffsetOfLocalArea() : 0);
218 OS
<< "Frame Objects:\n";
220 for (unsigned i
= 0, e
= Objects
.size(); i
!= e
; ++i
) {
221 const StackObject
&SO
= Objects
[i
];
222 OS
<< " fi#" << (int)(i
-NumFixedObjects
) << ": ";
225 OS
<< "id=" << static_cast<unsigned>(SO
.StackID
) << ' ';
227 if (SO
.Size
== ~0ULL) {
232 OS
<< "variable sized";
234 OS
<< "size=" << SO
.Size
;
235 OS
<< ", align=" << SO
.Alignment
;
237 if (i
< NumFixedObjects
)
239 if (i
< NumFixedObjects
|| SO
.SPOffset
!= -1) {
240 int64_t Off
= SO
.SPOffset
- ValOffset
;
241 OS
<< ", at location [SP";
252 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
253 LLVM_DUMP_METHOD
void MachineFrameInfo::dump(const MachineFunction
&MF
) const {