LLVM 22.0.0git
DebugLoc.cpp
Go to the documentation of this file.
1//===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
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#include "llvm/IR/DebugLoc.h"
10#include "llvm/Config/llvm-config.h"
11#include "llvm/IR/DebugInfo.h"
12
13#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
15
16namespace llvm {
17DbgLocOrigin::DbgLocOrigin(bool ShouldCollectTrace) {
18 if (!ShouldCollectTrace)
19 return;
20 auto &[Depth, StackTrace] = StackTraces.emplace_back();
21 Depth = sys::getStackTrace(StackTrace);
22}
23void DbgLocOrigin::addTrace() {
24 // We only want to add new stacktraces if we already have one: addTrace exists
25 // to provide more context to how missing DebugLocs have propagated through
26 // the program, but by design if there is no existing stacktrace then we have
27 // decided not to track this DebugLoc as being "missing".
28 if (StackTraces.empty())
29 return;
30 auto &[Depth, StackTrace] = StackTraces.emplace_back();
31 Depth = sys::getStackTrace(StackTrace);
32}
33} // namespace llvm
34#endif
35
36using namespace llvm;
37
38#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
39DILocAndCoverageTracking::DILocAndCoverageTracking(const DILocation *L)
40 : TrackingMDNodeRef(const_cast<DILocation *>(L)), DbgLocOrigin(!L),
41 Kind(DebugLocKind::Normal) {}
42#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
43
44//===----------------------------------------------------------------------===//
45// DebugLoc Implementation
46//===----------------------------------------------------------------------===//
47DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
48DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
49
51 return cast_or_null<DILocation>(Loc.get());
52}
53
54unsigned DebugLoc::getLine() const {
55 assert(get() && "Expected valid DebugLoc");
56 return get()->getLine();
57}
58
59unsigned DebugLoc::getCol() const {
60 assert(get() && "Expected valid DebugLoc");
61 return get()->getColumn();
62}
63
65 assert(get() && "Expected valid DebugLoc");
66 return get()->getScope();
67}
68
70 assert(get() && "Expected valid DebugLoc");
71 return get()->getInlinedAt();
72}
73
75 return cast<DILocation>(Loc)->getInlinedAtScope();
76}
77
79 // FIXME: Add a method on \a DILocation that does this work.
80 const MDNode *Scope = getInlinedAtScope();
81 if (auto *SP = getDISubprogram(Scope))
82 return DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP);
83
84 return DebugLoc();
85}
86
88 if (DILocation *Loc = get()) {
89 return Loc->isImplicitCode();
90 }
91 return true;
92}
93
94void DebugLoc::setImplicitCode(bool ImplicitCode) {
95 if (DILocation *Loc = get()) {
96 Loc->setImplicitCode(ImplicitCode);
97 }
98}
99
101 const DebugLoc &RootLoc, DISubprogram &NewSP, LLVMContext &Ctx,
104 DILocation *CachedResult = nullptr;
105
106 // Collect the inline chain, stopping if we find a location that has already
107 // been processed.
108 for (DILocation *Loc = RootLoc; Loc; Loc = Loc->getInlinedAt()) {
109 if (auto It = Cache.find(Loc); It != Cache.end()) {
110 CachedResult = cast<DILocation>(It->second);
111 break;
112 }
113 LocChain.push_back(Loc);
114 }
115
116 DILocation *UpdatedLoc = CachedResult;
117 if (!UpdatedLoc) {
118 // If no cache hits, then back() is the end of the inline chain, that is,
119 // the DILocation whose scope ends in the Subprogram to be replaced.
120 DILocation *LocToUpdate = LocChain.pop_back_val();
122 *LocToUpdate->getScope(), NewSP, Ctx, Cache);
123 UpdatedLoc = DILocation::get(Ctx, LocToUpdate->getLine(),
124 LocToUpdate->getColumn(), NewScope);
125 Cache[LocToUpdate] = UpdatedLoc;
126 }
127
128 // Recreate the location chain, bottom-up, starting at the new scope (or a
129 // cached result).
130 for (const DILocation *LocToUpdate : reverse(LocChain)) {
131 UpdatedLoc =
132 DILocation::get(Ctx, LocToUpdate->getLine(), LocToUpdate->getColumn(),
133 LocToUpdate->getScope(), UpdatedLoc);
134 Cache[LocToUpdate] = UpdatedLoc;
135 }
136
137 return UpdatedLoc;
138}
139
141 LLVMContext &Ctx,
143 SmallVector<DILocation *, 3> InlinedAtLocations;
144 DILocation *Last = InlinedAt;
145 DILocation *CurInlinedAt = DL;
146
147 // Gather all the inlined-at nodes.
148 while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
149 // Skip any we've already built nodes for.
150 if (auto *Found = Cache[IA]) {
151 Last = cast<DILocation>(Found);
152 break;
153 }
154
155 InlinedAtLocations.push_back(IA);
156 CurInlinedAt = IA;
157 }
158
159 // Starting from the top, rebuild the nodes to point to the new inlined-at
160 // location (then rebuilding the rest of the chain behind it) and update the
161 // map of already-constructed inlined-at nodes.
162 // Key Instructions: InlinedAt fields don't need atom info.
163 for (const DILocation *MD : reverse(InlinedAtLocations))
164 Cache[MD] = Last = DILocation::getDistinct(
165 Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
166
167 return Last;
168}
169
171 if (Locs.empty())
172 return DebugLoc();
173 if (Locs.size() == 1)
174 return Locs[0];
175 DebugLoc Merged = Locs[0];
176 for (const DebugLoc &DL : llvm::drop_begin(Locs)) {
177 Merged = getMergedLocation(Merged, DL);
178 if (!Merged)
179 break;
180 }
181 return Merged;
182}
184 if (!LocA || !LocB) {
185 // If coverage tracking is enabled, prioritize returning empty non-annotated
186 // locations to empty annotated locations.
187#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
188 if (!LocA && LocA.getKind() == DebugLocKind::Normal)
189 return LocA;
190 if (!LocB && LocB.getKind() == DebugLocKind::Normal)
191 return LocB;
192#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
193 if (!LocA)
194 return LocA;
195 return LocB;
196 }
197 return DILocation::getMergedLocation(LocA, LocB);
198}
199
200#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
202#endif
203
205 if (!Loc)
206 return;
207
208 // Print source line info.
209 auto *Scope = cast<DIScope>(getScope());
210 OS << Scope->getFilename();
211 OS << ':' << getLine();
212 if (getCol() != 0)
213 OS << ':' << getCol();
214
215 if (DebugLoc InlinedAtDL = getInlinedAt()) {
216 OS << " @[ ";
217 InlinedAtDL.print(OS);
218 OS << " ]";
219 }
220}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
static LLVM_ABI DILocalScope * cloneScopeForSubprogram(DILocalScope &RootScope, DISubprogram &NewSP, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Traverses the scope chain rooted at RootScope until it hits a Subprogram, recreating the chain with "...
static LLVM_ABI DILocation * getMergedLocation(DILocation *LocA, DILocation *LocB)
Attempts to merge LocA and LocB into a single location; see DebugLoc::getMergedLocation for more deta...
Base class for scope-like contexts.
Subprogram description. Uses SubclassData1.
LLVM_ABI void setImplicitCode(bool ImplicitCode)
Definition DebugLoc.cpp:94
DebugLoc()=default
LLVM_ABI unsigned getLine() const
Definition DebugLoc.cpp:54
LLVM_ABI DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
Definition DebugLoc.cpp:78
LLVM_ABI DILocation * get() const
Get the underlying DILocation.
Definition DebugLoc.cpp:50
LLVM_ABI MDNode * getScope() const
Definition DebugLoc.cpp:64
static LLVM_ABI DebugLoc appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Rebuild the entire inlined-at chain for this instruction so that the top of the chain now is inlined-...
Definition DebugLoc.cpp:140
static LLVM_ABI DebugLoc getMergedLocation(DebugLoc LocA, DebugLoc LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
Definition DebugLoc.cpp:183
LLVM_ABI void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition DebugLoc.cpp:204
static LLVM_ABI DebugLoc getMergedLocations(ArrayRef< DebugLoc > Locs)
Try to combine the vector of locations passed as input in a single one.
Definition DebugLoc.cpp:170
LLVM_ABI unsigned getCol() const
Definition DebugLoc.cpp:59
static LLVM_ABI DebugLoc replaceInlinedAtSubprogram(const DebugLoc &DL, DISubprogram &NewSP, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Rebuild the entire inline-at chain by replacing the subprogram at the end of the chain with NewSP.
Definition DebugLoc.cpp:100
LLVM_ABI bool isImplicitCode() const
Check if the DebugLoc corresponds to an implicit code.
Definition DebugLoc.cpp:87
LLVM_ABI void dump() const
Definition DebugLoc.cpp:201
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:69
LLVM_ABI MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
Definition DebugLoc.cpp:74
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Metadata node.
Definition Metadata.h:1077
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1573
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1565
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:310
TypedTrackingMDRef< MDNode > TrackingMDNodeRef
auto cast_or_null(const Y &Val)
Definition Casting.h:720
auto reverse(ContainerTy &&C)
Definition STLExtras.h:400
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
LLVM_ABI DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.