LLVM 20.0.0git
StackFrameLayoutAnalysisPass.cpp
Go to the documentation of this file.
1//===-- StackFrameLayoutAnalysisPass.cpp
2//------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// StackFrameLayoutAnalysisPass implementation. Outputs information about the
11// layout of the stack frame, using the remarks interface. On the CLI it prints
12// a textual representation of the stack frame. When possible it prints the
13// values that occupy a stack slot using any available debug information. Since
14// output is remarks based, it is also available in a machine readable file
15// format, such as YAML.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/ADT/SetVector.h"
25#include "llvm/CodeGen/Passes.h"
31#include "llvm/IR/PrintPasses.h"
33#include "llvm/Support/Debug.h"
36
37#include <sstream>
38
39using namespace llvm;
40
41#define DEBUG_TYPE "stack-frame-layout"
42
43namespace {
44
45/// StackFrameLayoutAnalysisPass - This is a pass to dump the stack frame of a
46/// MachineFunction.
47///
48struct StackFrameLayoutAnalysisPass : public MachineFunctionPass {
50 static char ID;
51
52 enum SlotType {
53 Spill, // a Spill slot
54 StackProtector, // Stack Protector slot
55 Variable, // a slot used to store a local data (could be a tmp)
56 Invalid // It's an error for a slot to have this type
57 };
58
59 struct SlotData {
60 int Slot;
61 int Size;
62 int Align;
63 int Offset;
64 SlotType SlotTy;
65 bool Scalable;
66
67 SlotData(const MachineFrameInfo &MFI, const int ValOffset, const int Idx)
68 : Slot(Idx), Size(MFI.getObjectSize(Idx)),
70 Offset(MFI.getObjectOffset(Idx) - ValOffset), SlotTy(Invalid),
71 Scalable(false) {
74 SlotTy = SlotType::Spill;
75 else if (Idx == MFI.getStackProtectorIndex())
76 SlotTy = SlotType::StackProtector;
77 else
78 SlotTy = SlotType::Variable;
79 }
80
81 // We use this to sort in reverse order, so that the layout is displayed
82 // correctly. Scalable slots are sorted to the end of the list.
83 bool operator<(const SlotData &Rhs) const {
84 return std::make_tuple(!Scalable, Offset) >
85 std::make_tuple(!Rhs.Scalable, Rhs.Offset);
86 }
87 };
88
89 StackFrameLayoutAnalysisPass() : MachineFunctionPass(ID) {}
90
91 StringRef getPassName() const override {
92 return "Stack Frame Layout Analysis";
93 }
94
95 void getAnalysisUsage(AnalysisUsage &AU) const override {
96 AU.setPreservesAll();
99 }
100
101 bool runOnMachineFunction(MachineFunction &MF) override {
102 // TODO: We should implement a similar filter for remarks:
103 // -Rpass-func-filter=<regex>
105 return false;
106
107 LLVMContext &Ctx = MF.getFunction().getContext();
109 return false;
110
113 &MF.front());
114 Rem << ("\nFunction: " + MF.getName()).str();
115 emitStackFrameLayoutRemarks(MF, Rem);
116 getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE().emit(Rem);
117 return false;
118 }
119
120 std::string getTypeString(SlotType Ty) {
121 switch (Ty) {
122 case SlotType::Spill:
123 return "Spill";
124 case SlotType::StackProtector:
125 return "Protector";
126 case SlotType::Variable:
127 return "Variable";
128 default:
129 llvm_unreachable("bad slot type for stack layout");
130 }
131 }
132
133 void emitStackSlotRemark(const MachineFunction &MF, const SlotData &D,
135 // To make it easy to understand the stack layout from the CLI, we want to
136 // print each slot like the following:
137 //
138 // Offset: [SP+8], Type: Spill, Align: 8, Size: 16
139 // foo @ /path/to/file.c:25
140 // bar @ /path/to/file.c:35
141 //
142 // Which prints the size, alignment, and offset from the SP at function
143 // entry.
144 //
145 // But we also want the machine readable remarks data to be nicely
146 // organized. So we print some additional data as strings for the CLI
147 // output, but maintain more structured data for the YAML.
148 //
149 // For example we store the Offset in YAML as:
150 // ...
151 // - Offset: -8
152 //
153 // But we print it to the CLI as
154 // Offset: [SP-8]
155
156 // Negative offsets will print a leading `-`, so only add `+`
157 std::string Prefix =
158 formatv("\nOffset: [SP{0}", (D.Offset < 0) ? "" : "+").str();
159 Rem << Prefix << ore::NV("Offset", D.Offset)
160 << "], Type: " << ore::NV("Type", getTypeString(D.SlotTy))
161 << ", Align: " << ore::NV("Align", D.Align)
162 << ", Size: " << ore::NV("Size", ElementCount::get(D.Size, D.Scalable));
163 }
164
165 void emitSourceLocRemark(const MachineFunction &MF, const DILocalVariable *N,
167 std::string Loc =
168 formatv("{0} @ {1}:{2}", N->getName(), N->getFilename(), N->getLine())
169 .str();
170 Rem << "\n " << ore::NV("DataLoc", Loc);
171 }
172
173 void emitStackFrameLayoutRemarks(MachineFunction &MF,
175 const MachineFrameInfo &MFI = MF.getFrameInfo();
176 if (!MFI.hasStackObjects())
177 return;
178
179 // ValOffset is the offset to the local area from the SP at function entry.
180 // To display the true offset from SP, we need to subtract ValOffset from
181 // MFI's ObjectOffset.
183 const int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
184
185 LLVM_DEBUG(dbgs() << "getStackProtectorIndex =="
186 << MFI.getStackProtectorIndex() << "\n");
187
188 std::vector<SlotData> SlotInfo;
189
190 const unsigned int NumObj = MFI.getNumObjects();
191 SlotInfo.reserve(NumObj);
192 // initialize slot info
193 for (int Idx = MFI.getObjectIndexBegin(), EndIdx = MFI.getObjectIndexEnd();
194 Idx != EndIdx; ++Idx) {
195 if (MFI.isDeadObjectIndex(Idx))
196 continue;
197 SlotInfo.emplace_back(MFI, ValOffset, Idx);
198 }
199
200 // sort the ordering, to match the actual layout in memory
201 llvm::sort(SlotInfo);
202
203 SlotDbgMap SlotMap = genSlotDbgMapping(MF);
204
205 for (const SlotData &Info : SlotInfo) {
206 emitStackSlotRemark(MF, Info, Rem);
207 for (const DILocalVariable *N : SlotMap[Info.Slot])
208 emitSourceLocRemark(MF, N, Rem);
209 }
210 }
211
212 // We need to generate a mapping of slots to the values that are stored to
213 // them. This information is lost by the time we need to print out the frame,
214 // so we reconstruct it here by walking the CFG, and generating the mapping.
215 SlotDbgMap genSlotDbgMapping(MachineFunction &MF) {
216 SlotDbgMap SlotDebugMap;
217
218 // add variables to the map
221 SlotDebugMap[DI.getStackSlot()].insert(DI.Var);
222
223 // Then add all the spills that have debug data
224 for (MachineBasicBlock &MBB : MF) {
225 for (MachineInstr &MI : MBB) {
226 for (MachineMemOperand *MO : MI.memoperands()) {
227 if (!MO->isStore())
228 continue;
229 auto *FI = dyn_cast_or_null<FixedStackPseudoSourceValue>(
230 MO->getPseudoValue());
231 if (!FI)
232 continue;
233 int FrameIdx = FI->getFrameIndex();
235 MI.collectDebugValues(Dbg);
236
237 for (MachineInstr *MI : Dbg)
238 SlotDebugMap[FrameIdx].insert(MI->getDebugVariable());
239 }
240 }
241 }
242
243 return SlotDebugMap;
244 }
245};
246
247char StackFrameLayoutAnalysisPass::ID = 0;
248} // namespace
249
250char &llvm::StackFrameLayoutAnalysisPassID = StackFrameLayoutAnalysisPass::ID;
251INITIALIZE_PASS(StackFrameLayoutAnalysisPass, "stack-frame-layout",
252 "Stack Frame Layout", false, false)
253
254namespace llvm {
255/// Returns a newly-created StackFrameLayout pass.
257 return new StackFrameLayoutAnalysisPass();
258}
259
260} // namespace llvm
MachineBasicBlock & MBB
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
IRTranslator LLVM IR MI
static std::string getTypeString(Type *T)
Definition: LLParser.cpp:71
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file implements a set that has insertion order iteration characteristics.
#define DEBUG_TYPE
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition: TypeSize.h:317
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1830
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:358
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
int getStackProtectorIndex() const
Return the index for the stack protector object.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
unsigned getNumObjects() const
Return the number of objects.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
bool hasStackObjects() const
Return true if there are any stack objects in this function.
uint8_t getStackID(int ObjectIdx) const
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
int getObjectIndexBegin() const
Return the minimum frame object index.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Description of the location of a variable whose Address is valid and unchanging during function execu...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
auto getInStackSlotVariableDbgInfo()
Returns the collection of variables for which we have debug info and that have been assigned a stack ...
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & front() const
Representation of each machine instruction.
Definition: MachineInstr.h:69
A description of a memory reference used in the backend.
Diagnostic information for optimization analysis remarks.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Information about stack frame layout on the target.
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
virtual const TargetFrameLowering * getFrameLowering() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:361
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
MachineFunctionPass * createStackFrameLayoutAnalysisPass()
StackFramePrinter pass - This pass prints out the machine function's stack frame to the given stream ...
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool isFunctionInPrintList(StringRef FunctionName)
char & StackFrameLayoutAnalysisPassID
StackFramePrinter - This pass prints the stack frame layout and variable mappings.
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
virtual bool isAnalysisRemarkEnabled(StringRef PassName) const
Return true if analysis remarks are enabled, override to provide different implementation.