LLVM 24.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 DILocation *L = get();
65 return L ? cast_if_present<MDNode>(L->getRawIRLayers()) : nullptr;
66}
67
69 return cast<DILocation>(Loc)->getInlinedAtScope();
70}
71
73 // FIXME: Add a method on \a DILocation that does this work.
74 const MDNode *Scope = getInlinedAtScope();
75 if (auto *SP = getDISubprogram(Scope))
76 return DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP);
77
78 return DebugLoc();
79}
80
81MDNode *DebugLoc::getAsMDNode() const { return Loc; }
82
84 if (DILocation *Loc = get())
85 return Loc->isImplicitCode();
86 return true;
87}
88
89void DebugLoc::setImplicitCode(bool ImplicitCode) {
90 if (DILocation *Loc = get())
91 Loc->setImplicitCode(ImplicitCode);
92}
93
95 const DebugLoc &RootLoc, DISubprogram &NewSP, LLVMContext &Ctx,
98 DILocation *CachedResult = nullptr;
99
100 // Collect the inline chain, stopping if we find a location that has already
101 // been processed.
102 for (DILocation *Loc = RootLoc; Loc; Loc = Loc->getInlinedAt()) {
103 if (auto It = Cache.find(Loc); It != Cache.end()) {
104 CachedResult = cast<DILocation>(It->second);
105 break;
106 }
107 LocChain.push_back(Loc);
108 }
109
110 DILocation *UpdatedLoc = CachedResult;
111 if (!UpdatedLoc) {
112 // If no cache hits, then back() is the end of the inline chain, that is,
113 // the DILocation whose scope ends in the Subprogram to be replaced.
114 DILocation *LocToUpdate = LocChain.pop_back_val();
116 *LocToUpdate->getScope(), NewSP, Ctx, Cache);
117 UpdatedLoc = DILocation::get(
118 Ctx, LocToUpdate->getLine(), LocToUpdate->getColumn(), NewScope,
119 /*InlinedAt=*/nullptr, /*ImplicitCode=*/false, /*AtomGroup=*/0,
120 /*AtomRank=*/0, LocToUpdate->getRawIRLayers());
121 Cache[LocToUpdate] = UpdatedLoc;
122 }
123
124 // Recreate the location chain, bottom-up, starting at the new scope (or a
125 // cached result). Each location in the chain keeps its own irlayers, as in
126 // appendInlinedAt: any of them may carry layers.
127 for (const DILocation *LocToUpdate : reverse(LocChain)) {
128 UpdatedLoc = DILocation::get(
129 Ctx, LocToUpdate->getLine(), LocToUpdate->getColumn(),
130 LocToUpdate->getScope(), UpdatedLoc, /*ImplicitCode=*/false,
131 /*AtomGroup=*/0, /*AtomRank=*/0, LocToUpdate->getRawIRLayers());
132 Cache[LocToUpdate] = UpdatedLoc;
133 }
134
135 return UpdatedLoc;
136}
137
139 LLVMContext &Ctx,
141 SmallVector<DILocation *, 3> InlinedAtLocations;
142 DILocation *Last = InlinedAt;
143 DILocation *CurInlinedAt = DL;
144
145 // Gather all the inlined-at nodes.
146 while (DILocation *IA = CurInlinedAt->getInlinedAt()) {
147 // Skip any we've already built nodes for.
148 auto It = Cache.find(IA);
149 if (It != Cache.end() && It->second) {
150 Last = cast<DILocation>(It->second);
151 break;
152 }
153
154 InlinedAtLocations.push_back(IA);
155 CurInlinedAt = IA;
156 }
157
158 // Starting from the top, rebuild the nodes to point to the new inlined-at
159 // location (then rebuilding the rest of the chain behind it) and update the
160 // map of already-constructed inlined-at nodes.
161 // Key Instructions: InlinedAt fields don't need atom info.
162 // Each location in the chain keeps its own irlayers; any of them may carry
163 // layers, so rebuilding the chain must not drop them.
164 for (const DILocation *MD : reverse(InlinedAtLocations))
165 Cache[MD] = Last = DILocation::getDistinct(
166 Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last,
167 /*ImplicitCode=*/false, /*AtomGroup=*/0, /*AtomRank=*/0,
168 MD->getRawIRLayers());
169
170 return Last;
171}
172
174 if (Locs.empty())
175 return DebugLoc();
176 if (Locs.size() == 1)
177 return Locs[0];
178 DebugLoc Merged = Locs[0];
179 for (const DebugLoc &DL : llvm::drop_begin(Locs)) {
180 Merged = getMergedLocation(Merged, DL);
181 if (!Merged)
182 break;
183 }
184 return Merged;
185}
187 if (!LocA || !LocB) {
188 // If coverage tracking is enabled, prioritize returning empty non-annotated
189 // locations to empty annotated locations.
190#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
191 if (!LocA && LocA.getKind() == DebugLocKind::Normal)
192 return LocA;
193 if (!LocB && LocB.getKind() == DebugLocKind::Normal)
194 return LocB;
195#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
196 if (!LocA)
197 return LocA;
198 return LocB;
199 }
200 return DILocation::getMergedLocation(LocA, LocB);
201}
202
203#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
205#endif
206
208 if (!Loc)
209 return;
210
211 // Print source line info.
212 auto *Scope = cast<DIScope>(getScope());
213 OS << Scope->getFilename();
214 OS << ':' << getLine();
215 if (getCol() != 0)
216 OS << ':' << getCol();
217
218 if (DebugLoc InlinedAtDL = getInlinedAt()) {
219 OS << " @[ ";
220 InlinedAtDL.print(OS);
221 OS << " ]";
222 }
223}
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:686
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...
Metadata * getRawIRLayers() const
The optional intermediate-IR layer list (DILayerLocList), or null.
Base class for scope-like contexts.
Subprogram description. Uses SubclassData1.
LLVM_ABI void setImplicitCode(bool ImplicitCode)
Definition DebugLoc.cpp:89
LLVM_ABI unsigned getLine() const
Definition DebugLoc.cpp:43
DILocation * get() const
Get the underlying DILocation.
Definition DebugLoc.h:226
LLVM_ABI DebugLoc getFnDebugLoc() const
Find the debug info location for the start of the function.
Definition DebugLoc.cpp:72
LLVM_ABI MDNode * getScope() const
Definition DebugLoc.cpp:53
LLVM_ABI MDNode * getAsMDNode() const
Return this as a bar MDNode.
Definition DebugLoc.cpp:81
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:138
LLVM_ABI MDNode * getRawIRLayers() const
The raw intermediate-IR layer list (DILayerLocList) of the underlying location, or null.
Definition DebugLoc.cpp:63
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:186
LLVM_ABI void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition DebugLoc.cpp:207
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:173
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:94
LLVM_ABI bool isImplicitCode() const
Check if the DebugLoc corresponds to an implicit code.
Definition DebugLoc.cpp:83
LLVM_ABI void dump() const
Definition DebugLoc.cpp:204
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:68
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Metadata node.
Definition Metadata.h:1081
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1587
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
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
auto cast_if_present(const Y &Val)
cast_if_present<X> - Functionally identical to cast, except that a null value is accepted.
Definition Casting.h:683
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
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.