LLVM 22.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 llvm::append_range(LocationList, *Locations);
186}
187
188// Calculate coverage factor.
190 if (!LVLocation::calculateCoverage(Locations.get(), CoverageFactor,
191 CoveragePercentage)) {
192 LVScope *Parent = getParentScope();
193 if (Parent->getIsInlinedFunction()) {
194 // For symbols representing the inlined function parameters and its
195 // variables, get the outer most parent that contains their location
196 // lower address.
197 // The symbol can have a set of non-contiguous locations. We are using
198 // only the first location entry to get the outermost parent.
199 // If no scope contains the location, assume its enclosing parent.
200 LVScope *Scope =
201 Parent->outermostParent(Locations->front()->getLowerAddress());
202 if (Scope)
203 Parent = Scope;
204 }
205 unsigned CoverageParent = Parent->getCoverageFactor();
206 // Get a percentage rounded to two decimal digits. This avoids
207 // implementation-defined rounding inside printing functions.
208 CoveragePercentage =
209 CoverageParent
210 ? rint((double(CoverageFactor) / CoverageParent) * 100.0 * 100.0) /
211 100.0
212 : 0;
213 // Record invalid coverage entry.
214 if (options().getWarningCoverages() && CoveragePercentage > 100)
216 }
217}
218
220 if (getIsResolvedName())
221 return;
222 setIsResolvedName();
223
225
226 // Resolve any given pattern.
228}
229
231 // The symbols can have the following references to other elements:
232 // A Type:
233 // DW_AT_type -> Type or Scope
234 // DW_AT_import -> Type
235 // A Reference:
236 // DW_AT_specification -> Symbol
237 // DW_AT_abstract_origin -> Symbol
238 // DW_AT_extension -> Symbol
239
240 // Resolve any referenced symbol.
241 LVSymbol *Reference = getReference();
242 if (Reference) {
243 Reference->resolve();
244 // Recursively resolve the symbol names.
246 }
247
248 // Set the file/line information using the Debug Information entry.
250
251 // Resolve symbol type.
252 if (LVElement *Element = getType()) {
253 Element->resolve();
254
255 // In the case of demoted typedefs, use the underlying type.
256 if (Element->getIsTypedefReduced()) {
258 Element->resolve();
259 }
260
261 // If the type is a template parameter, get its type, which can
262 // point to a type or scope, depending on the argument instance.
264 }
265
266 // Resolve the variable associated type.
267 if (!getType() && Reference)
268 setType(Reference->getType());
269}
270
272 // If the symbol have a DW_AT_specification or DW_AT_abstract_origin,
273 // follow the chain to resolve the name from those references.
274 if (getHasReference() && !isNamed())
276
277 return getName();
278}
279
281 const LVSymbols *Targets) {
282 if (!(References && Targets))
283 return;
284
285 LLVM_DEBUG({
286 dbgs() << "\n[LVSymbol::markMissingParents]\n";
287 for (const LVSymbol *Reference : *References)
288 dbgs() << "References: "
289 << "Kind = " << formattedKind(Reference->kind()) << ", "
290 << "Name = " << formattedName(Reference->getName()) << "\n";
291 for (const LVSymbol *Target : *Targets)
292 dbgs() << "Targets : "
293 << "Kind = " << formattedKind(Target->kind()) << ", "
294 << "Name = " << formattedName(Target->getName()) << "\n";
295 });
296
297 for (LVSymbol *Reference : *References) {
298 LLVM_DEBUG({
299 dbgs() << "Search Reference: Name = "
300 << formattedName(Reference->getName()) << "\n";
301 });
302 if (!Reference->findIn(Targets))
303 Reference->markBranchAsMissing();
304 }
305}
306
307LVSymbol *LVSymbol::findIn(const LVSymbols *Targets) const {
308 if (!Targets)
309 return nullptr;
310
311 LLVM_DEBUG({
312 dbgs() << "\n[LVSymbol::findIn]\n"
313 << "Reference: "
314 << "Level = " << getLevel() << ", "
315 << "Kind = " << formattedKind(kind()) << ", "
316 << "Name = " << formattedName(getName()) << "\n";
317 for (const LVSymbol *Target : *Targets)
318 dbgs() << "Target : "
319 << "Level = " << Target->getLevel() << ", "
320 << "Kind = " << formattedKind(Target->kind()) << ", "
321 << "Name = " << formattedName(Target->getName()) << "\n";
322 });
323
324 for (LVSymbol *Target : *Targets)
325 if (equals(Target))
326 return Target;
327
328 return nullptr;
329}
330
331// Check for a match on the arguments of a function.
333 const LVSymbols *Targets) {
334 if (!References && !Targets)
335 return true;
336 if (References && Targets) {
337 LVSymbols ReferenceParams;
338 getParameters(References, &ReferenceParams);
339 LVSymbols TargetParams;
340 getParameters(Targets, &TargetParams);
341 return LVSymbol::equals(&ReferenceParams, &TargetParams);
342 }
343 return false;
344}
345
346// Return the symbols which are parameters.
348 if (Symbols)
349 for (LVSymbol *Symbol : *Symbols)
350 if (Symbol->getIsParameter())
351 Parameters->push_back(Symbol);
352}
353
354bool LVSymbol::equals(const LVSymbol *Symbol) const {
356 return false;
357
358 // Check if any reference is the same.
360 return false;
361
363 return false;
364
365 return true;
366}
367
368bool LVSymbol::equals(const LVSymbols *References, const LVSymbols *Targets) {
369 if (!References && !Targets)
370 return true;
371 if (References && Targets && References->size() == Targets->size()) {
372 for (const LVSymbol *Reference : *References)
373 if (!Reference->findIn(Targets))
374 return false;
375 return true;
376 }
377 return false;
378}
379
382}
383
385 if (Locations)
386 for (const LVLocation *Location : *Locations)
387 Location->printRaw(OS, Full);
388}
389
390void LVSymbol::print(raw_ostream &OS, bool Full) const {
391 if (getIncludeInPrint() && getReader().doPrintSymbol(this)) {
395 }
396}
397
399 // Accessibility depends on the parent (class, structure).
400 uint32_t AccessCode = 0;
401 if (getIsMember() || getIsInheritance())
402 AccessCode = getParentScope()->getIsClass() ? dwarf::DW_ACCESS_private
404
405 const LVSymbol *Symbol = getIsInlined() ? Reference : this;
406 std::string Attributes =
407 Symbol->getIsCallSiteParameter()
408 ? ""
410 Symbol->accessibilityString(AccessCode),
412
413 OS << formattedKind(Symbol->kind()) << " " << Attributes;
414 if (Symbol->getIsUnspecified())
416 else {
417 if (Symbol->getIsInheritance())
421 else {
423 // Print any bitfield information.
424 if (uint32_t Size = getBitSize())
425 OS << ":" << Size;
426 OS << " -> " << Symbol->typeOffsetAsString()
429 }
430 }
431
432 // Print any initial value if any.
433 if (ValueIndex)
434 OS << " = " << formattedName(getValue());
435 OS << "\n";
436
437 if (Full && options().getPrintFormatting()) {
439 printLinkageName(OS, Full, const_cast<LVSymbol *>(this));
440 if (LVSymbol *Reference = getReference())
441 Reference->printReference(OS, Full, const_cast<LVSymbol *>(this));
442
443 // Print location information.
444 LVLocation::print(Locations.get(), OS, Full);
445 }
446}
AMDGPU Kernel Attributes
mir Rename Register Operands
raw_pwrite_stream & OS
#define LLVM_DEBUG(...)
Definition: Debug.h:119
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:43
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:99
size_t size() const
Definition: SmallVector.h:79
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Target - Wrapper for Target specific information.
const char * getName() const
getName - Get the target name.
LLVM_ABI 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:192
LVElement * getType() const
Definition: LVElement.h:311
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:315
void printLinkageName(raw_ostream &OS, bool Full, LVElement *Parent, LVScope *Scope) const
Definition: LVElement.cpp:555
StringRef getTypeQualifiedName() const
Definition: LVElement.h:326
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:182
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:151
static void print(LVLocations *Locations, raw_ostream &OS, bool Full=true)
Definition: LVLocation.cpp:621
LVScope * getParentScope() const
Definition: LVObject.h:254
virtual void print(raw_ostream &OS, bool Full=true) const
Definition: LVObject.cpp:156
dwarf::Attribute Attr
Definition: LVObject.h:146
LVLevel getLevel() const
Definition: LVObject.h:243
void setParent(LVScope *Scope)
Definition: LVObject.cpp:87
void setAttr(dwarf::Attribute Attr)
Definition: LVObject.h:234
void resolvePatternMatch(LVLine *Line)
Definition: LVOptions.h:609
void addInvalidCoverage(LVSymbol *Symbol)
Definition: LVScope.cpp:1367
LVScope * outermostParent(LVAddress Address)
Definition: LVScope.cpp:812
unsigned getCoverageFactor() const
Definition: LVScope.h:253
const LVLocations * getRanges() const
Definition: LVScope.h:212
void resolveName() override
Definition: LVSymbol.cpp:219
static void getParameters(const LVSymbols *Symbols, LVSymbols *Parameters)
Definition: LVSymbol.cpp:347
void report(LVComparePass Pass) override
Definition: LVSymbol.cpp:380
void printExtra(raw_ostream &OS, bool Full=true) const override
Definition: LVSymbol.cpp:398
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:230
void print(raw_ostream &OS, bool Full=true) const override
Definition: LVSymbol.cpp:390
void addLocationOperands(LVSmall Opcode, ArrayRef< uint64_t > Operands)
Definition: LVSymbol.cpp:85
size_t getLinkageNameIndex() const override
Definition: LVSymbol.h:113
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:280
void addLocationConstant(dwarf::Attribute Attr, LVUnsigned Constant, uint64_t LocDescOffset)
Definition: LVSymbol.cpp:92
StringRef getValue() const override
Definition: LVSymbol.h:119
bool equals(const LVSymbol *Symbol) const
Definition: LVSymbol.cpp:354
uint32_t getBitSize() const override
Definition: LVSymbol.h:115
const char * kind() const override
Definition: LVSymbol.cpp:36
void printLocations(raw_ostream &OS, bool Full=true) const
Definition: LVSymbol.cpp:384
static bool parametersMatch(const LVSymbols *References, const LVSymbols *Targets)
Definition: LVSymbol.cpp:332
StringRef resolveReferencesChain()
Definition: LVSymbol.cpp:271
LVSymbol * getReference() const
Definition: LVSymbol.h:97
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
@ DW_ACCESS_private
Definition: Dwarf.h:186
@ DW_ACCESS_public
Definition: Dwarf.h:184
Attribute
Attributes.
Definition: Dwarf.h:124
@ DW_OP_hi_user
Definition: Dwarf.h:142
LVReader & getReader()
Definition: LVReader.h:360
std::string formattedNames(StringRef Name1, StringRef Name2)
Definition: LVSupport.h:244
LVPatterns & patterns()
Definition: LVOptions.h:645
std::string formattedKind(StringRef Kind)
Definition: LVSupport.h:236
LVScopeCompileUnit * getReaderCompileUnit()
Definition: LVReader.h:364
bool(LVLocation::*)() LVValidLocation
Definition: LVObject.h:101
std::string formattedName(StringRef Name)
Definition: LVSupport.h:240
std::map< LVSymbolKind, LVSymbolGetFunction > LVSymbolDispatch
Definition: LVSymbol.h:34
const LVSmall LVLocationMemberOffset
Definition: LVLocation.h:26
LVOptions & options()
Definition: LVOptions.h:448
std::string formatAttributes(const StringRef First, Args... Others)
Definition: LVSupport.h:143
LVCompare & getComparator()
Definition: LVCompare.h:85
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2155
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207