LLVM 24.0.0git
Utils.cpp
Go to the documentation of this file.
1//===- Utils.cpp ------------------------------------------------*- C++ -*-===//
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
10#include "llvm/ADT/STLExtras.h"
15#include <limits>
16#include <map>
17
18namespace llvm {
19namespace dwarf_linker {
20
22 std::optional<DWARFFormValue> Location = Die.find(dwarf::DW_AT_location);
23 if (!Location)
24 return false;
25
26 std::optional<ArrayRef<uint8_t>> Block = Location->getAsBlock();
27 if (!Block)
28 return false;
29
30 DWARFUnit *U = Die.getDwarfUnit();
31 DataExtractor Data(*Block, U->getContext().isLittleEndian());
32 DWARFExpression Expression(Data, U->getAddressByteSize(),
33 U->getFormParams().Format);
34 return !Expression.isMemoryLocation() &&
36 return !Op.isError() && (Op.getCode() == dwarf::DW_OP_addr ||
37 Op.getCode() == dwarf::DW_OP_addrx);
38 });
39}
40
41bool isStaticDataMember(const DWARFDie &Die) {
42 assert(Die.getTag() == dwarf::DW_TAG_member && "expected a DW_TAG_member");
43
44 if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_declaration), 0))
45 return false;
46
47 // A member that has a location occupies storage in the record regardless of
48 // how else it is marked, so it is the absence of a location that makes this a
49 // pure declaration. Zero is a valid offset, so only the attribute's presence
50 // matters, not its value.
51 return !Die.find(
52 {dwarf::DW_AT_data_member_location, dwarf::DW_AT_data_bit_offset});
53}
54
56 switch (Tag) {
57 case dwarf::DW_TAG_class_type:
58 case dwarf::DW_TAG_enumeration_type:
59 case dwarf::DW_TAG_interface_type:
60 case dwarf::DW_TAG_structure_type:
61 case dwarf::DW_TAG_union_type:
62 return true;
63 default:
64 return false;
65 }
66}
67
68bool isIdentifyingChild(const DWARFDie &Die) {
69 // Units disagree on implicit member functions and on how static data
70 // members are spelled.
71 switch (Die.getTag()) {
72 case dwarf::DW_TAG_member:
73 return !isStaticDataMember(Die);
74 case dwarf::DW_TAG_enumerator:
75 case dwarf::DW_TAG_inheritance:
76 return true;
77 default:
78 return false;
79 }
80}
81
84 ArrayRef<uint64_t> SortedStmtSeqOffsets,
85 DenseMap<uint64_t, uint64_t> &SeqOffToFirstRow) {
86 // Use std::map for ordered iteration by input stmt-sequence offset.
87 std::map<uint64_t, uint64_t> LineTableMapping;
88 for (const DWARFDebugLine::Sequence &Seq : LT.Sequences)
89 LineTableMapping[Seq.StmtSeqOffset] = Seq.FirstRowIndex;
90
91 if (LT.Rows.empty()) {
92 for (const auto &[Off, Row] : LineTableMapping)
93 SeqOffToFirstRow[Off] = Row;
94 return;
95 }
96
97 // Row indices that look like sequence starts: row 0, plus every row
98 // immediately following an end_sequence marker.
99 SmallVector<uint64_t> SeqStartRows;
100 SeqStartRows.push_back(0);
101 for (auto [I, Row] : llvm::enumerate(ArrayRef(LT.Rows).drop_back()))
102 if (Row.EndSequence)
103 SeqStartRows.push_back(I + 1);
104
105 ArrayRef<uint64_t> StmtAttrsRef(SortedStmtSeqOffsets);
106 ArrayRef<uint64_t> SeqStartRowsRef(SeqStartRows);
107
108 // While SeqOffToFirstRow parsed from LT could be the ground truth, e.g.
109 //
110 // SeqOff Row
111 // 0x08 9
112 // 0x14 15
113 //
114 // The StmtAttrs and SeqStartRows may not match perfectly, e.g.
115 //
116 // StmtAttrs SeqStartRows
117 // 0x04 3
118 // 0x08 5
119 // 0x10 9
120 // 0x12 11
121 // 0x14 15
122 //
123 // In this case, we don't want to assign 5 to 0x08, since we know 0x08
124 // maps to 9. If we do a dummy 1:1 mapping 0x10 will be mapped to 9
125 // which is incorrect. The expected behavior is ignore 5, realign the
126 // table based on the result from the line table:
127 //
128 // StmtAttrs SeqStartRows
129 // 0x04 3
130 // -- 5
131 // 0x08 9 <- LineTableMapping ground truth
132 // 0x10 11
133 // 0x12 --
134 // 0x14 15 <- LineTableMapping ground truth
135
136 // Dummy trailing anchor so both refs always drain before we run out
137 // of map entries to walk.
138 constexpr uint64_t DummyKey = std::numeric_limits<uint64_t>::max();
139 constexpr uint64_t DummyVal = std::numeric_limits<uint64_t>::max();
140 LineTableMapping[DummyKey] = DummyVal;
141
142 for (auto [NextSeqOff, NextRow] : LineTableMapping) {
143 auto StmtAttrSmallerThanNext = [N = NextSeqOff](uint64_t SA) {
144 return SA < N;
145 };
146 auto SeqStartSmallerThanNext = [N = NextRow](uint64_t Row) {
147 return Row < N;
148 };
149 // While both lists still point strictly before the next anchor,
150 // pair them up 1:1 — this captures sequences the parser missed.
151 while (!StmtAttrsRef.empty() && !SeqStartRowsRef.empty() &&
152 StmtAttrSmallerThanNext(StmtAttrsRef.front()) &&
153 SeqStartSmallerThanNext(SeqStartRowsRef.front())) {
154 SeqOffToFirstRow[StmtAttrsRef.consume_front()] =
155 SeqStartRowsRef.consume_front();
156 }
157 // Either list may now be ahead of or at the anchor: drop entries we
158 // can't safely pair, then use the parser's (NextSeqOff,NextRow)
159 // mapping as ground truth.
160 StmtAttrsRef = StmtAttrsRef.drop_while(StmtAttrSmallerThanNext);
161 SeqStartRowsRef = SeqStartRowsRef.drop_while(SeqStartSmallerThanNext);
162 if (NextSeqOff != DummyKey)
163 SeqOffToFirstRow[NextSeqOff] = NextRow;
164 // Advance each list past the anchor only if it was pointing exactly
165 // at it.
166 if (!StmtAttrsRef.empty() && StmtAttrsRef.front() == NextSeqOff)
167 StmtAttrsRef = StmtAttrsRef.drop_front();
168 if (!SeqStartRowsRef.empty() && SeqStartRowsRef.front() == NextRow)
169 SeqStartRowsRef = SeqStartRowsRef.drop_front();
170 }
171}
172
173} // namespace dwarf_linker
174} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
#define I(x, y, z)
Definition MD5.cpp:57
This file contains some templates that are useful if you are working with the STL at all.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ArrayRef< T > drop_while(PredicateT Pred) const
Return a copy of *this with the first N elements satisfying the given predicate removed.
Definition ArrayRef.h:207
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:194
const T & front() const
Get the first element.
Definition ArrayRef.h:144
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
const T & consume_front()
consume_front() - Returns the first element and drops it from ArrayRef.
Definition ArrayRef.h:156
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition DWARFDie.h:43
LLVM_ABI std::optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition DWARFDie.cpp:335
DWARFUnit * getDwarfUnit() const
Definition DWARFDie.h:55
dwarf::Tag getTag() const
Definition DWARFDie.h:73
This class represents an Operation in the Expression.
Class representing an expression and its matching format.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM_ABI bool isIdentifiedByChildren(dwarf::Tag Tag)
Test if an unnamed type with tag Tag is identified by its children, which tell it apart from other un...
Definition Utils.cpp:55
LLVM_ABI bool hasImplicitAddressLocation(const DWARFDie &Die)
Test if the location of variable Die contains a global address but only uses to compute the variable'...
Definition Utils.cpp:21
LLVM_ABI void buildStmtSeqOffsetToFirstRowIndex(const DWARFDebugLine::LineTable &LT, ArrayRef< uint64_t > SortedStmtSeqOffsets, DenseMap< uint64_t, uint64_t > &SeqOffToFirstRow)
Build a map from an input DW_AT_LLVM_stmt_sequence byte offset to the first-row index (in LT....
Definition Utils.cpp:82
LLVM_ABI bool isStaticDataMember(const DWARFDie &Die)
Test if the DW_TAG_member Die is a DWARF 2-4 style static data member: a declaration with no storage ...
Definition Utils.cpp:41
LLVM_ABI bool isIdentifyingChild(const DWARFDie &Die)
Test if Die is a child that identifies its unnamed parent type.
Definition Utils.cpp:68
std::optional< uint64_t > toUnsigned(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2570
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
#define N
Represents a series of contiguous machine instructions.
uint64_t StmtSeqOffset
The offset into the line table where this sequence begins.