LLVM 23.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
13using namespace llvm;
14
15#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
17namespace llvm {
18bool DebugLocOriginCollectionEnabled = false;
19} // namespace llvm
20
21DbgLocOrigin::DbgLocOrigin(bool ShouldCollectTrace) {
22 if (!ShouldCollectTrace || !DebugLocOriginCollectionEnabled)
23 return;
24 auto &[Depth, StackTrace] = StackTraces.emplace_back();
25 Depth = sys::getStackTrace(StackTrace);
26}
27void DbgLocOrigin::addTrace() {
28 // We only want to add new stacktraces if we already have one: addTrace exists
29 // to provide more context to how missing DebugLocs have propagated through
30 // the program, but by design if there is no existing stacktrace then we have
31 // decided not to track this DebugLoc as being "missing".
32 if (StackTraces.empty())
33 return;
34 auto &[Depth, StackTrace] = StackTraces.emplace_back();
35 Depth = sys::getStackTrace(StackTrace);
36}
37#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
38
39//===----------------------------------------------------------------------===//
40// DebugLoc Implementation
41//===----------------------------------------------------------------------===//
42
43unsigned DebugLoc::getLine() const {
44 assert(get() && "Expected valid DebugLoc");
45 return get()->getLine();
46}
47
48unsigned DebugLoc::getCol() const {
49 assert(get() && "Expected valid DebugLoc");
50 return get()->getColumn();
51}
52
54 assert(get() && "Expected valid DebugLoc");
55 return get()->getScope();
56}
57
59 assert(get() && "Expected valid DebugLoc");
60 return get()->getInlinedAt();
61}
62
64 return cast<DILocation>(Loc)->getInlinedAtScope();
65}
66
68 // FIXME: Add a method on \a DILocation that does this work.
69 const MDNode *Scope = getInlinedAtScope();
70 if (auto *SP = getDISubprogram(Scope))
71 return DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP);
72
73 return DebugLoc();
74}
75
76MDNode *DebugLoc::getAsMDNode() const { return Loc; }
77
79 if (DILocation *Loc = get())
80 return Loc->isImplicitCode();
81 return true;
82}
83
84void DebugLoc::setImplicitCode(bool ImplicitCode) {
85 if (DILocation *Loc = get())
86 Loc->setImplicitCode(ImplicitCode);
87}
88
90 const DebugLoc &RootLoc, DISubprogram &NewSP, LLVMContext &Ctx,
93 DILocation *CachedResult = nullptr;
94
95 // Collect the inline chain, stopping if we find a location that has already
96 // been processed.
97 for (DILocation *Loc = RootLoc; Loc; Loc = Loc->getInlinedAt()) {
98 if (auto It = Cache.find(Loc); It != Cache.end()) {
99 CachedResult = cast<DILocation>(It->second);
100 break;
101 }
102 LocChain.push_back(Loc);
103 }
104
105 DILocation *UpdatedLoc = CachedResult;
106 if (!UpdatedLoc) {
107 // If no cache hits, then back() is the end of the inline chain, that is,
108 // the DILocation whose scope ends in the Subprogram to be replaced.
109 DILocation *LocToUpdate = LocChain.pop_back_val();
111 *LocToUpdate->getScope(), NewSP, Ctx, Cache);
112 UpdatedLoc = DILocation::get(Ctx, LocToUpdate->getLine(),
113 LocToUpdate->getColumn(), NewScope);
114 Cache[LocToUpdate] = UpdatedLoc;
115 }
116
117 // Recreate the location chain, bottom-up, starting at the new scope (or a
118 // cached result).
119 for (const DILocation *LocToUpdate : reverse(LocChain)) {
120 UpdatedLoc =
121 DILocation::get(Ctx, LocToUpdate->getLine(), LocToUpdate->getColumn(),
122 LocToUpdate->getScope(), UpdatedLoc);
123 Cache[LocToUpdate] = UpdatedLoc;
124 }
125
126 return UpdatedLoc;
127}
128
130 LLVMContext &Ctx,
132 SmallVector<DILocation *, 3> InlinedAtLocations;
133 DILocation *Last = InlinedAt;
134 DILocation *CurInlinedAt = DL;
135
136 // Gather all the inlined-at nodes.
137 while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
138 // Skip any we've already built nodes for.
139 if (auto *Found = Cache[IA]) {
140 Last = cast<DILocation>(Found);
141 break;
142 }
143
144 InlinedAtLocations.push_back(IA);
145 CurInlinedAt = IA;
146 }
147
148 // Starting from the top, rebuild the nodes to point to the new inlined-at
149 // location (then rebuilding the rest of the chain behind it) and update the
150 // map of already-constructed inlined-at nodes.
151 // Key Instructions: InlinedAt fields don't need atom info.
152 for (const DILocation *MD : reverse(InlinedAtLocations))
153 Cache[MD] = Last = DILocation::getDistinct(
154 Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
155
156 return Last;
157}
158
160 if (Locs.empty())
161 return DebugLoc();
162 if (Locs.size() == 1)
163 return Locs[0];
164 DebugLoc Merged = Locs[0];
165 for (const DebugLoc &DL : llvm::drop_begin(Locs)) {
166 Merged = getMergedLocation(Merged, DL);
167 if (!Merged)
168 break;
169 }
170 return Merged;
171}
173 if (!LocA || !LocB) {
174 // If coverage tracking is enabled, prioritize returning empty non-annotated
175 // locations to empty annotated locations.
176#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
177 if (!LocA && LocA.getKind() == DebugLocKind::Normal)
178 return LocA;
179 if (!LocB && LocB.getKind() == DebugLocKind::Normal)
180 return LocB;
181#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
182 if (!LocA)
183 return LocA;
184 return LocB;
185 }
186 return DILocation::getMergedLocation(LocA, LocB);
187}
188
189#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
191#endif
192
194 if (!Loc)
195 return;
196
197 // Print source line info.
198 auto *Scope = cast<DIScope>(getScope());
199 OS << Scope->getFilename();
200 OS << ':' << getLine();
201 if (getCol() != 0)
202 OS << ':' << getCol();
203
204 if (DebugLoc InlinedAtDL = getInlinedAt()) {
205 OS << " @[ ";
206 InlinedAtDL.print(OS);
207 OS << " ]";
208 }
209}
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:663
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
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:84
LLVM_ABI unsigned getLine() const
Definition DebugLoc.cpp:43
DILocation * get() const
Get the underlying DILocation.
Definition DebugLoc.h:220
LLVM_ABI DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
Definition DebugLoc.cpp:67
LLVM_ABI MDNode * getScope() const
Definition DebugLoc.cpp:53
LLVM_ABI MDNode * getAsMDNode() const
Return this as a bar MDNode.
Definition DebugLoc.cpp:76
DebugLoc(const DILocation *L=nullptr)
Construct from an DILocation.
Definition DebugLoc.h:131
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:129
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:172
LLVM_ABI void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition DebugLoc.cpp:193
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:159
LLVM_ABI unsigned getCol() const
Definition DebugLoc.cpp:48
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:89
LLVM_ABI bool isImplicitCode() const
Check if the DebugLoc corresponds to an implicit code.
Definition DebugLoc.cpp:78
LLVM_ABI void dump() const
Definition DebugLoc.cpp:190
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:58
LLVM_ABI MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
Definition DebugLoc.cpp:63
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Metadata node.
Definition Metadata.h:1069
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1562
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1554
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:315
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.