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