LLVM: lib/IR/DebugLoc.cpp Source File

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
16#include "llvm/Support/Signals.h"
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
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//===----------------------------------------------------------------------===//
45 DebugLoc::DebugLoc(const DILocation *L) : Loc(const_cast<DILocation *>(L)) {}
46 DebugLoc::DebugLoc(const MDNode *L) : Loc(const_cast<MDNode *>(L)) {}
47
49 return cast_or_null<DILocation>(Loc.get());
50}
51
52 unsigned DebugLoc::getLine() const {
53 assert(get() && "Expected valid DebugLoc");
54 return get()->getLine();
55}
56
57 unsigned 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 }
89 return true;
90}
91
92 void DebugLoc::setImplicitCode(bool ImplicitCode) {
93 if (DILocation *Loc = get()) {
94 Loc->setImplicitCode(ImplicitCode);
95 }
96}
97
99 const DebugLoc &RootLoc, DISubprogram &NewSP, LLVMContext &Ctx,
102 DILocation *CachedResult = nullptr;
103
104 // Collect the inline chain, stopping if we find a location that has already
105 // been processed.
106 for (DILocation *Loc = RootLoc; Loc; Loc = Loc->getInlinedAt()) {
107 if (auto It = Cache.find(Loc); It != Cache.end()) {
108 CachedResult = cast<DILocation>(It->second);
109 break;
110 }
111 LocChain.push_back(Loc);
112 }
113
114 DILocation *UpdatedLoc = CachedResult;
115 if (!UpdatedLoc) {
116 // If no cache hits, then back() is the end of the inline chain, that is,
117 // the DILocation whose scope ends in the Subprogram to be replaced.
118 DILocation *LocToUpdate = LocChain.pop_back_val();
120 *LocToUpdate->getScope(), NewSP, Ctx, Cache);
121 UpdatedLoc = DILocation::get(Ctx, LocToUpdate->getLine(),
122 LocToUpdate->getColumn(), NewScope);
123 Cache[LocToUpdate] = UpdatedLoc;
124 }
125
126 // Recreate the location chain, bottom-up, starting at the new scope (or a
127 // cached result).
128 for (const DILocation *LocToUpdate : reverse(LocChain)) {
129 UpdatedLoc =
130 DILocation::get(Ctx, LocToUpdate->getLine(), LocToUpdate->getColumn(),
131 LocToUpdate->getScope(), UpdatedLoc);
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 if (auto *Found = Cache[IA]) {
149 Last = cast<DILocation>(Found);
150 break;
151 }
152
153 InlinedAtLocations.push_back(IA);
154 CurInlinedAt = IA;
155 }
156
157 // Starting from the top, rebuild the nodes to point to the new inlined-at
158 // location (then rebuilding the rest of the chain behind it) and update the
159 // map of already-constructed inlined-at nodes.
160 // Key Instructions: InlinedAt fields don't need atom info.
161 for (const DILocation *MD : reverse(InlinedAtLocations))
162 Cache[MD] = Last = DILocation::getDistinct(
163 Ctx, MD->getLine(), MD->getColumn(), MD->getScope(), Last);
164
165 return Last;
166}
167
169 if (Locs.empty())
170 return DebugLoc();
171 if (Locs.size() == 1)
172 return Locs[0];
173 DebugLoc Merged = Locs[0];
174 for (const DebugLoc &DL : llvm::drop_begin(Locs)) {
175 Merged = getMergedLocation(Merged, DL);
176 if (!Merged)
177 break;
178 }
179 return Merged;
180}
182 if (!LocA || !LocB) {
183 // If coverage tracking is enabled, prioritize returning empty non-annotated
184 // locations to empty annotated locations.
185#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
186 if (!LocA && LocA.getKind() == DebugLocKind::Normal)
187 return LocA;
188 if (!LocB && LocB.getKind() == DebugLocKind::Normal)
189 return LocB;
190#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
191 if (!LocA)
192 return LocA;
193 return LocB;
194 }
195 return DILocation::getMergedLocation(LocA, LocB);
196}
197
198#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
199 LLVM_DUMP_METHOD void DebugLoc::dump() const { print(dbgs()); }
200#endif
201
202 void DebugLoc::print(raw_ostream &OS) const {
203 if (!Loc)
204 return;
205
206 // Print source line info.
207 auto *Scope = cast<DIScope>(getScope());
208 OS << Scope->getFilename();
209 OS << ':' << getLine();
210 if (getCol() != 0)
211 OS << ':' << getCol();
212
213 if (DebugLoc InlinedAtDL = getInlinedAt()) {
214 OS << " @[ ";
215 InlinedAtDL.print(OS);
216 OS << " ]";
217 }
218}
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:143
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:138
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 "...
Debug location.
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:92
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:138
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:181
LLVM_ABI void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition DebugLoc.cpp:202
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:168
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:98
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:199
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)
Definition SmallVector.h:417
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1203
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.
Definition AddressRanges.h:18
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.
Definition DebugInfo.cpp:135

Generated on for LLVM by doxygen 1.14.0

AltStyle によって変換されたページ (->オリジナル) /