LLVM 20.0.0git
LVSymbol.cpp
Go to the documentation of this file.
1//===-- LVSymbol.cpp ------------------------------------------------------===//
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 implements the LVSymbol class.
10//
11//===----------------------------------------------------------------------===//
12
18
19using namespace llvm;
20using namespace llvm::logicalview;
21
22#define DEBUG_TYPE "Symbol"
23
24namespace {
25const char *const KindCallSiteParameter = "CallSiteParameter";
26const char *const KindConstant = "Constant";
27const char *const KindInherits = "Inherits";
28const char *const KindMember = "Member";
29const char *const KindParameter = "Parameter";
30const char *const KindUndefined = "Undefined";
31const char *const KindUnspecified = "Unspecified";
32const char *const KindVariable = "Variable";
33} // end anonymous namespace
34
35// Return a string representation for the symbol kind.
36const char *LVSymbol::kind() const {
37 const char *Kind = KindUndefined;
38 if (getIsCallSiteParameter())
39 Kind = KindCallSiteParameter;
40 else if (getIsConstant())
41 Kind = KindConstant;
42 else if (getIsInheritance())
43 Kind = KindInherits;
44 else if (getIsMember())
45 Kind = KindMember;
46 else if (getIsParameter())
47 Kind = KindParameter;
48 else if (getIsUnspecified())
49 Kind = KindUnspecified;
50 else if (getIsVariable())
51 Kind = KindVariable;
52 return Kind;
53}
54
55LVSymbolDispatch LVSymbol::Dispatch = {
56 {LVSymbolKind::IsCallSiteParameter, &LVSymbol::getIsCallSiteParameter},
57 {LVSymbolKind::IsConstant, &LVSymbol::getIsConstant},
58 {LVSymbolKind::IsInheritance, &LVSymbol::getIsInheritance},
59 {LVSymbolKind::IsMember, &LVSymbol::getIsMember},
60 {LVSymbolKind::IsParameter, &LVSymbol::getIsParameter},
61 {LVSymbolKind::IsUnspecified, &LVSymbol::getIsUnspecified},
62 {LVSymbolKind::IsVariable, &LVSymbol::getIsVariable}};
63
64// Add a Location Entry.
66 LVAddress HighPC, LVUnsigned SectionOffset,
67 uint64_t LocDescOffset, bool CallSiteLocation) {
68 if (!Locations)
69 Locations = std::make_unique<LVLocations>();
70
71 // Create the location entry.
72 CurrentLocation = getReader().createLocationSymbol();
73 CurrentLocation->setParent(this);
74 CurrentLocation->setAttr(Attr);
75 if (CallSiteLocation)
76 CurrentLocation->setIsCallSite();
77 CurrentLocation->addObject(LowPC, HighPC, SectionOffset, LocDescOffset);
78 Locations->push_back(CurrentLocation);
79
80 // Mark the symbol as having location information.
81 setHasLocation();
82}
83
84// Add a Location Record.
87 if (CurrentLocation)
88 CurrentLocation->addObject(Opcode, Operands);
89}
90
91// Add a Location Entry.
93 uint64_t LocDescOffset) {
94 // Create a Location Entry, with the global information.
96 /*LowPC=*/0, /*HighPC=*/-1,
97 /*SectionOffset=*/0, LocDescOffset);
98
99 // Add records to Location Entry.
101}
102
103LVLocations::iterator LVSymbol::addLocationGap(LVLocations::iterator Pos,
104 LVAddress LowPC,
105 LVAddress HighPC) {
106 // Create a location entry for the gap.
107 LVLocation *Gap = getReader().createLocationSymbol();
108 Gap->setParent(this);
109 Gap->setAttr(dwarf::DW_AT_location);
110 Gap->addObject(LowPC, HighPC,
111 /*section_offset=*/0,
112 /*locdesc_offset=*/0);
113
114 LVLocations::iterator Iter = Locations->insert(Pos, Gap);
115
116 // Add gap to Location Entry.
118
119 // Mark the entry as a gap.
120 Gap->setIsGapEntry();
121
122 return Iter;
123}
124
126 // The symbol has locations records. Fill gaps in the location list.
127 if (!getHasLocation() || !getFillGaps())
128 return;
129
130 // Get the parent range information and add dummy location entries.
132 if (!Ranges)
133 return;
134
135 for (const LVLocation *Entry : *Ranges) {
136 LVAddress ParentLowPC = Entry->getLowerAddress();
137 LVAddress ParentHighPC = Entry->getUpperAddress();
138
139 // Traverse the symbol locations and for each location contained in
140 // the current parent range, insert locations for any existing gap.
142 LVAddress LowPC = 0;
143 LVAddress Marker = ParentLowPC;
144 for (LVLocations::iterator Iter = Locations->begin();
145 Iter != Locations->end(); ++Iter) {
146 Location = *Iter;
147 LowPC = Location->getLowerAddress();
148 if (LowPC != Marker) {
149 // We have a gap at [Marker,LowPC - 1].
150 Iter = addLocationGap(Iter, Marker, LowPC - 1);
151 ++Iter;
152 }
153
154 // Move to the next item in the location list.
155 Marker = Location->getUpperAddress() + 1;
156 }
157
158 // Check any gap at the end.
159 if (Marker < ParentHighPC)
160 // We have a gap at [Marker,ParentHighPC].
161 addLocationGap(Locations->end(), Marker, ParentHighPC);
162 }
163}
164
165// Get all the locations based on the valid function.
167 LVValidLocation ValidLocation, bool RecordInvalid) {
168 if (!Locations)
169 return;
170
171 for (LVLocation *Location : *Locations) {
172 // Add the invalid location object.
173 if (!(Location->*ValidLocation)() && RecordInvalid)
174 LocationList.push_back(Location);
175 }
176
177 // Calculate coverage factor.
179}
180
181void LVSymbol::getLocations(LVLocations &LocationList) const {
182 if (!Locations)
183 return;
184
185 for (LVLocation *Location : *Locations)
186 LocationList.push_back(Location);
187}
188
189// Calculate coverage factor.
191 if (!LVLocation::calculateCoverage(Locations.get(), CoverageFactor,
192 CoveragePercentage)) {
193 LVScope *Parent = getParentScope();
194 if (Parent->getIsInlinedFunction()) {
195 // For symbols representing the inlined function parameters and its
196 // variables, get the outer most parent that contains their location
197 // lower address.
198 // The symbol can have a set of non-contiguous locations. We are using
199 // only the first location entry to get the outermost parent.
200 // If no scope contains the location, assume its enclosing parent.
201 LVScope *Scope =
202 Parent->outermostParent(Locations->front()->getLowerAddress());
203 if (Scope)
204 Parent = Scope;
205 }
206 unsigned CoverageParent = Parent->getCoverageFactor();
207 // Get a percentage rounded to two decimal digits. This avoids
208 // implementation-defined rounding inside printing functions.
209 CoveragePercentage =
210 CoverageParent
211 ? rint((double(CoverageFactor) / CoverageParent) * 100.0 * 100.0) /
212 100.0
213 : 0;
214 // Record invalid coverage entry.
215 if (options().getWarningCoverages() && CoveragePercentage > 100)
217 }
218}
219
221 if (getIsResolvedName())
222 return;
223 setIsResolvedName();
224
226
227 // Resolve any given pattern.
229}
230
232 // The symbols can have the following references to other elements:
233 // A Type:
234 // DW_AT_type -> Type or Scope
235 // DW_AT_import -> Type
236 // A Reference:
237 // DW_AT_specification -> Symbol
238 // DW_AT_abstract_origin -> Symbol
239 // DW_AT_extension -> Symbol
240
241 // Resolve any referenced symbol.
242 LVSymbol *Reference = getReference();
243 if (Reference) {
244 Reference->resolve();
245 // Recursively resolve the symbol names.
247 }
248
249 // Set the file/line information using the Debug Information entry.
251
252 // Resolve symbol type.
253 if (LVElement *Element = getType()) {
254 Element->resolve();
255
256 // In the case of demoted typedefs, use the underlying type.
257 if (Element->getIsTypedefReduced()) {
259 Element->resolve();
260 }
261
262 // If the type is a template parameter, get its type, which can
263 // point to a type or scope, depending on the argument instance.
265 }
266
267 // Resolve the variable associated type.
268 if (!getType() && Reference)
269 setType(Reference->getType());
270}
271
273 // If the symbol have a DW_AT_specification or DW_AT_abstract_origin,
274 // follow the chain to resolve the name from those references.
275 if (getHasReference() && !isNamed())
277
278 return getName();
279}
280
282 const LVSymbols *Targets) {
283 if (!(References && Targets))
284 return;
285
286 LLVM_DEBUG({
287 dbgs() << "\n[LVSymbol::markMissingParents]\n";
288 for (const LVSymbol *Reference : *References)
289 dbgs() << "References: "
290 << "Kind = " << formattedKind(Reference->kind()) << ", "
291 << "Name = " << formattedName(Reference->getName()) << "\n";
292 for (const LVSymbol *Target : *Targets)
293 dbgs() << "Targets : "
294 << "Kind = " << formattedKind(Target->kind()) << ", "
295 << "Name = " << formattedName(Target->getName()) << "\n";
296 });
297
298 for (LVSymbol *Reference : *References) {
299 LLVM_DEBUG({
300 dbgs() << "Search Reference: Name = "
301 << formattedName(Reference->getName()) << "\n";
302 });
303 if (!Reference->findIn(Targets))
304 Reference->markBranchAsMissing();
305 }
306}
307
308LVSymbol *LVSymbol::findIn(const LVSymbols *Targets) const {
309 if (!Targets)
310 return nullptr;
311
312 LLVM_DEBUG({
313 dbgs() << "\n[LVSymbol::findIn]\n"
314 << "Reference: "
315 << "Level = " << getLevel() << ", "
316 << "Kind = " << formattedKind(kind()) << ", "
317 << "Name = " << formattedName(getName()) << "\n";
318 for (const LVSymbol *Target : *Targets)
319 dbgs() << "Target : "
320 << "Level = " << Target->getLevel() << ", "
321 << "Kind = " << formattedKind(Target->kind()) << ", "
322 << "Name = " << formattedName(Target->getName()) << "\n";
323 });
324
325 for (LVSymbol *Target : *Targets)
326 if (equals(Target))
327 return Target;
328
329 return nullptr;
330}
331
332// Check for a match on the arguments of a function.
334 const LVSymbols *Targets) {
335 if (!References && !Targets)
336 return true;
337 if (References && Targets) {
338 LVSymbols ReferenceParams;
339 getParameters(References, &ReferenceParams);
340 LVSymbols TargetParams;
341 getParameters(Targets, &TargetParams);
342 return LVSymbol::equals(&ReferenceParams, &TargetParams);
343 }
344 return false;
345}
346
347// Return the symbols which are parameters.
349 if (Symbols)
350 for (LVSymbol *Symbol : *Symbols)
351 if (Symbol->getIsParameter())
352 Parameters->push_back(Symbol);
353}
354
355bool LVSymbol::equals(const LVSymbol *Symbol) const {
357 return false;
358
359 // Check if any reference is the same.
361 return false;
362
364 return false;
365
366 return true;
367}
368
369bool LVSymbol::equals(const LVSymbols *References, const LVSymbols *Targets) {
370 if (!References && !Targets)
371 return true;
372 if (References && Targets && References->size() == Targets->size()) {
373 for (const LVSymbol *Reference : *References)
374 if (!Reference->findIn(Targets))
375 return false;
376 return true;
377 }
378 return false;
379}
380
383}
384
386 if (Locations)
387 for (const LVLocation *Location : *Locations)
388 Location->printRaw(OS, Full);
389}
390
391void LVSymbol::print(raw_ostream &OS, bool Full) const {
392 if (getIncludeInPrint() && getReader().doPrintSymbol(this)) {
396 }
397}
398
400 // Accessibility depends on the parent (class, structure).
401 uint32_t AccessCode = 0;
402 if (getIsMember() || getIsInheritance())
403 AccessCode = getParentScope()->getIsClass() ? dwarf::DW_ACCESS_private
405
406 const LVSymbol *Symbol = getIsInlined() ? Reference : this;
407 std::string Attributes =
408 Symbol->getIsCallSiteParameter()
409 ? ""
411 Symbol->accessibilityString(AccessCode),
413
414 OS << formattedKind(Symbol->kind()) << " " << Attributes;
415 if (Symbol->getIsUnspecified())
417 else {
418 if (Symbol->getIsInheritance())
422 else {
424 // Print any bitfield information.
425 if (uint32_t Size = getBitSize())
426 OS << ":" << Size;
427 OS << " -> " << Symbol->typeOffsetAsString()
430 }
431 }
432
433 // Print any initial value if any.
434 if (ValueIndex)
435 OS << " = " << formattedName(getValue());
436 OS << "\n";
437
438 if (Full && options().getPrintFormatting()) {
440 printLinkageName(OS, Full, const_cast<LVSymbol *>(this));
441 if (LVSymbol *Reference = getReference())
442 Reference->printReference(OS, Full, const_cast<LVSymbol *>(this));
443
444 // Print location information.
445 LVLocation::print(Locations.get(), OS, Full);
446 }
447}
AMDGPU Kernel Attributes
#define LLVM_DEBUG(...)
Definition: Debug.h:106
uint64_t Size
mir Rename Register Operands
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This is an important base class in LLVM.
Definition: Constant.h:42
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
size_t size() const
Definition: SmallVector.h:78
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Target - Wrapper for Target specific information.
const char * getName() const
getName - Get the target name.
void printItem(LVElement *Element, LVComparePass Pass)
Definition: LVCompare.cpp:362
StringRef virtualityString(uint32_t Virtuality=dwarf::DW_VIRTUALITY_none) const
Definition: LVElement.cpp:170
StringRef typeAsString() const
Definition: LVElement.cpp:68
StringRef externalString() const
Definition: LVElement.cpp:150
void setName(StringRef ElementName) override
Definition: LVElement.cpp:95
void setGenericType(LVElement *Element)
Definition: LVElement.cpp:42
StringRef getName() const override
Definition: LVElement.h:184
LVElement * getType() const
Definition: LVElement.h:297
bool referenceMatch(const LVElement *Element) const
Definition: LVElement.cpp:469
void setFile(LVElement *Reference=nullptr)
Definition: LVElement.cpp:373
void setType(LVElement *Element=nullptr)
Definition: LVElement.h:301
void printLinkageName(raw_ostream &OS, bool Full, LVElement *Parent, LVScope *Scope) const
Definition: LVElement.cpp:555
StringRef getTypeQualifiedName() const
Definition: LVElement.h:312
StringRef accessibilityString(uint32_t Access=dwarf::DW_ACCESS_private) const
Definition: LVElement.cpp:123
bool equals(const LVElement *Element) const
Definition: LVElement.cpp:474
std::string typeOffsetAsString() const
Definition: LVElement.cpp:115
bool isNamed() const override
Definition: LVElement.h:174
void printReference(raw_ostream &OS, bool Full, LVElement *Parent) const
Definition: LVElement.cpp:539
static bool calculateCoverage(LVLocations *Locations, unsigned &Factor, float &Percentage)
Definition: LVLocation.cpp:496
virtual void addObject(LVAddress LowPC, LVAddress HighPC, LVUnsigned SectionOffset, uint64_t LocDescOffset)
Definition: LVLocation.h:150
static void print(LVLocations *Locations, raw_ostream &OS, bool Full=true)
Definition: LVLocation.cpp:621
LVScope * getParentScope() const
Definition: LVObject.h:253
virtual void print(raw_ostream &OS, bool Full=true) const
Definition: LVObject.cpp:160
dwarf::Attribute Attr
Definition: LVObject.h:139
LVLevel getLevel() const
Definition: LVObject.h:242
void setParent(LVScope *Scope)
Definition: LVObject.cpp:89
void setAttr(dwarf::Attribute Attr)
Definition: LVObject.h:233
void resolvePatternMatch(LVLine *Line)
Definition: LVOptions.h:606
void addInvalidCoverage(LVSymbol *Symbol)
Definition: LVScope.cpp:1356
LVScope * outermostParent(LVAddress Address)
Definition: LVScope.cpp:805
unsigned getCoverageFactor() const
Definition: LVScope.h:247
const LVLocations * getRanges() const
Definition: LVScope.h:206
void resolveName() override
Definition: LVSymbol.cpp:220
static void getParameters(const LVSymbols *Symbols, LVSymbols *Parameters)
Definition: LVSymbol.cpp:348
void report(LVComparePass Pass) override
Definition: LVSymbol.cpp:381
void printExtra(raw_ostream &OS, bool Full=true) const override
Definition: LVSymbol.cpp:399
void addLocation(dwarf::Attribute Attr, LVAddress LowPC, LVAddress HighPC, LVUnsigned SectionOffset, uint64_t LocDescOffset, bool CallSiteLocation=false)
Definition: LVSymbol.cpp:65
void resolveReferences() override
Definition: LVSymbol.cpp:231
void print(raw_ostream &OS, bool Full=true) const override
Definition: LVSymbol.cpp:391
void addLocationOperands(LVSmall Opcode, ArrayRef< uint64_t > Operands)
Definition: LVSymbol.cpp:85
size_t getLinkageNameIndex() const override
Definition: LVSymbol.h:112
void getLocations(LVLocations &LocationList, LVValidLocation ValidLocation, bool RecordInvalid=false)
Definition: LVSymbol.cpp:166
static void markMissingParents(const LVSymbols *References, const LVSymbols *Targets)
Definition: LVSymbol.cpp:281
void addLocationConstant(dwarf::Attribute Attr, LVUnsigned Constant, uint64_t LocDescOffset)
Definition: LVSymbol.cpp:92
StringRef getValue() const override
Definition: LVSymbol.h:118
bool equals(const LVSymbol *Symbol) const
Definition: LVSymbol.cpp:355
uint32_t getBitSize() const override
Definition: LVSymbol.h:114
const char * kind() const override
Definition: LVSymbol.cpp:36
void printLocations(raw_ostream &OS, bool Full=true) const
Definition: LVSymbol.cpp:385
static bool parametersMatch(const LVSymbols *References, const LVSymbols *Targets)
Definition: LVSymbol.cpp:333
StringRef resolveReferencesChain()
Definition: LVSymbol.cpp:272
LVSymbol * getReference() const
Definition: LVSymbol.h:96
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ DW_ACCESS_private
Definition: Dwarf.h:185
@ DW_ACCESS_public
Definition: Dwarf.h:183
Attribute
Attributes.
Definition: Dwarf.h:123
@ DW_OP_hi_user
Definition: Dwarf.h:141
LVReader & getReader()
Definition: LVReader.h:333
std::string formattedNames(StringRef Name1, StringRef Name2)
Definition: LVSupport.h:224
LVPatterns & patterns()
Definition: LVOptions.h:642
std::string formattedKind(StringRef Kind)
Definition: LVSupport.h:216
LVScopeCompileUnit * getReaderCompileUnit()
Definition: LVReader.h:337
bool(LVLocation::*)() LVValidLocation
Definition: LVObject.h:92
std::string formattedName(StringRef Name)
Definition: LVSupport.h:220
std::map< LVSymbolKind, LVSymbolGetFunction > LVSymbolDispatch
Definition: LVSymbol.h:33
const LVSmall LVLocationMemberOffset
Definition: LVLocation.h:25
LVOptions & options()
Definition: LVOptions.h:445
std::string formatAttributes(const StringRef First, Args... Others)
Definition: LVSupport.h:123
LVCompare & getComparator()
Definition: LVCompare.h:84
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163