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(llvm::Align 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 llvm::Align
clampStackAlignment(bool ShouldClamp
,
41 llvm::Align StackAlign
) {
42 if (!ShouldClamp
|| Align
<= StackAlign
)
44 LLVM_DEBUG(dbgs() << "Warning: requested alignment " << Align
.value()
45 << " exceeds the stack alignment " << StackAlign
.value()
46 << " when stack realignment is off" << '\n');
50 int MachineFrameInfo::CreateStackObject(uint64_t Size
, llvm::Align Alignment
,
52 const AllocaInst
*Alloca
,
54 assert(Size
!= 0 && "Cannot allocate zero size stack objects!");
55 Alignment
= clampStackAlignment(!StackRealignable
, Alignment
, StackAlignment
);
56 Objects
.push_back(StackObject(Size
, Alignment
, 0, false, IsSpillSlot
, Alloca
,
57 !IsSpillSlot
, StackID
));
58 int Index
= (int)Objects
.size() - NumFixedObjects
- 1;
59 assert(Index
>= 0 && "Bad frame index!");
61 ensureMaxAlignment(Alignment
);
65 int MachineFrameInfo::CreateSpillStackObject(uint64_t Size
,
66 llvm::Align Alignment
) {
67 Alignment
= clampStackAlignment(!StackRealignable
, Alignment
, StackAlignment
);
68 CreateStackObject(Size
, Alignment
, true);
69 int Index
= (int)Objects
.size() - NumFixedObjects
- 1;
70 ensureMaxAlignment(Alignment
);
74 int MachineFrameInfo::CreateVariableSizedObject(llvm::Align Alignment
,
75 const AllocaInst
*Alloca
) {
76 HasVarSizedObjects
= true;
77 Alignment
= clampStackAlignment(!StackRealignable
, Alignment
, StackAlignment
);
78 Objects
.push_back(StackObject(0, Alignment
, 0, false, false, Alloca
, true));
79 ensureMaxAlignment(Alignment
);
80 return (int)Objects
.size()-NumFixedObjects
-1;
83 int MachineFrameInfo::CreateFixedObject(uint64_t Size
, int64_t SPOffset
,
84 bool IsImmutable
, bool IsAliased
) {
85 assert(Size
!= 0 && "Cannot allocate zero size fixed stack objects!");
86 // The alignment of the frame index can be determined from its offset from
87 // the incoming frame position. If the frame object is at offset 32 and
88 // the stack is guaranteed to be 16-byte aligned, then we know that the
89 // object is 16-byte aligned. Note that unlike the non-fixed case, if the
90 // stack needs realignment, we can't assume that the stack will in fact be
92 llvm::Align Alignment
= commonAlignment(
93 ForcedRealign
? llvm::Align::None() : StackAlignment
, SPOffset
);
94 Alignment
= clampStackAlignment(!StackRealignable
, Alignment
, StackAlignment
);
95 Objects
.insert(Objects
.begin(),
96 StackObject(Size
, Alignment
, SPOffset
, IsImmutable
,
97 /*IsSpillSlot=*/false, /*Alloca=*/nullptr,
99 return -++NumFixedObjects
;
102 int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size
,
105 llvm::Align Alignment
= commonAlignment(
106 ForcedRealign
? llvm::Align::None() : StackAlignment
, SPOffset
);
107 Alignment
= clampStackAlignment(!StackRealignable
, Alignment
, StackAlignment
);
108 Objects
.insert(Objects
.begin(),
109 StackObject(Size
, Alignment
, SPOffset
, IsImmutable
,
110 /*IsSpillSlot=*/true, /*Alloca=*/nullptr,
111 /*IsAliased=*/false));
112 return -++NumFixedObjects
;
115 BitVector
MachineFrameInfo::getPristineRegs(const MachineFunction
&MF
) const {
116 const TargetRegisterInfo
*TRI
= MF
.getSubtarget().getRegisterInfo();
117 BitVector
BV(TRI
->getNumRegs());
119 // Before CSI is calculated, no registers are considered pristine. They can be
120 // freely used and PEI will make sure they are saved.
121 if (!isCalleeSavedInfoValid())
124 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
125 for (const MCPhysReg
*CSR
= MRI
.getCalleeSavedRegs(); CSR
&& *CSR
;
129 // Saved CSRs are not pristine.
130 for (auto &I
: getCalleeSavedInfo())
131 for (MCSubRegIterator
S(I
.getReg(), TRI
, true); S
.isValid(); ++S
)
137 unsigned MachineFrameInfo::estimateStackSize(const MachineFunction
&MF
) const {
138 const TargetFrameLowering
*TFI
= MF
.getSubtarget().getFrameLowering();
139 const TargetRegisterInfo
*RegInfo
= MF
.getSubtarget().getRegisterInfo();
140 unsigned MaxAlign
= getMaxAlignment();
143 // This code is very, very similar to PEI::calculateFrameObjectOffsets().
144 // It really should be refactored to share code. Until then, changes
145 // should keep in mind that there's tight coupling between the two.
147 for (int i
= getObjectIndexBegin(); i
!= 0; ++i
) {
148 // Only estimate stack size of default stack.
149 if (getStackID(i
) != TargetStackID::Default
)
151 int FixedOff
= -getObjectOffset(i
);
152 if (FixedOff
> Offset
) Offset
= FixedOff
;
154 for (unsigned i
= 0, e
= getObjectIndexEnd(); i
!= e
; ++i
) {
155 // Only estimate stack size of live objects on default stack.
156 if (isDeadObjectIndex(i
) || getStackID(i
) != TargetStackID::Default
)
158 Offset
+= getObjectSize(i
);
159 unsigned Align
= getObjectAlignment(i
);
160 // Adjust to alignment boundary
161 Offset
= (Offset
+Align
-1)/Align
*Align
;
163 MaxAlign
= std::max(Align
, MaxAlign
);
166 if (adjustsStack() && TFI
->hasReservedCallFrame(MF
))
167 Offset
+= getMaxCallFrameSize();
169 // Round up the size to a multiple of the alignment. If the function has
170 // any calls or alloca's, align to the target's StackAlignment value to
171 // ensure that the callee's frame or the alloca data is suitably aligned;
172 // otherwise, for leaf functions, align to the TransientStackAlignment
175 if (adjustsStack() || hasVarSizedObjects() ||
176 (RegInfo
->needsStackRealignment(MF
) && getObjectIndexEnd() != 0))
177 StackAlign
= TFI
->getStackAlignment();
179 StackAlign
= TFI
->getTransientStackAlignment();
181 // If the frame pointer is eliminated, all frame offsets will be relative to
182 // SP not FP. Align to MaxAlign so this works.
183 StackAlign
= std::max(StackAlign
, MaxAlign
);
184 unsigned AlignMask
= StackAlign
- 1;
185 Offset
= (Offset
+ AlignMask
) & ~uint64_t(AlignMask
);
187 return (unsigned)Offset
;
190 void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction
&MF
) {
191 const TargetInstrInfo
&TII
= *MF
.getSubtarget().getInstrInfo();
192 unsigned FrameSetupOpcode
= TII
.getCallFrameSetupOpcode();
193 unsigned FrameDestroyOpcode
= TII
.getCallFrameDestroyOpcode();
194 assert(FrameSetupOpcode
!= ~0u && FrameDestroyOpcode
!= ~0u &&
195 "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
197 MaxCallFrameSize
= 0;
198 for (const MachineBasicBlock
&MBB
: MF
) {
199 for (const MachineInstr
&MI
: MBB
) {
200 unsigned Opcode
= MI
.getOpcode();
201 if (Opcode
== FrameSetupOpcode
|| Opcode
== FrameDestroyOpcode
) {
202 unsigned Size
= TII
.getFrameSize(MI
);
203 MaxCallFrameSize
= std::max(MaxCallFrameSize
, Size
);
205 } else if (MI
.isInlineAsm()) {
206 // Some inline asm's need a stack frame, as indicated by operand 1.
207 unsigned ExtraInfo
= MI
.getOperand(InlineAsm::MIOp_ExtraInfo
).getImm();
208 if (ExtraInfo
& InlineAsm::Extra_IsAlignStack
)
215 void MachineFrameInfo::print(const MachineFunction
&MF
, raw_ostream
&OS
) const{
216 if (Objects
.empty()) return;
218 const TargetFrameLowering
*FI
= MF
.getSubtarget().getFrameLowering();
219 int ValOffset
= (FI
? FI
->getOffsetOfLocalArea() : 0);
221 OS
<< "Frame Objects:\n";
223 for (unsigned i
= 0, e
= Objects
.size(); i
!= e
; ++i
) {
224 const StackObject
&SO
= Objects
[i
];
225 OS
<< " fi#" << (int)(i
-NumFixedObjects
) << ": ";
228 OS
<< "id=" << static_cast<unsigned>(SO
.StackID
) << ' ';
230 if (SO
.Size
== ~0ULL) {
235 OS
<< "variable sized";
237 OS
<< "size=" << SO
.Size
;
238 OS
<< ", align=" << SO
.Alignment
.value();
240 if (i
< NumFixedObjects
)
242 if (i
< NumFixedObjects
|| SO
.SPOffset
!= -1) {
243 int64_t Off
= SO
.SPOffset
- ValOffset
;
244 OS
<< ", at location [SP";
255 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
256 LLVM_DUMP_METHOD
void MachineFrameInfo::dump(const MachineFunction
&MF
) const {