LLVM 24.0.0git
DebugLoc.h
Go to the documentation of this file.
1//===- DebugLoc.h - Debug Location Information ------------------*- C++ -*-===//
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// This file defines a number of light weight data structures used
10// to describe and track debug location information.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_DEBUGLOC_H
15#define LLVM_IR_DEBUGLOC_H
16
17#include "llvm/Config/llvm-config.h"
21
22namespace llvm {
23
24class LLVMContext;
25class raw_ostream;
26class DILocation;
27class Function;
28
29#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
30#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
31extern bool DebugLocOriginCollectionEnabled;
32
33struct DbgLocOrigin {
34 static constexpr unsigned long MaxDepth = 16;
35 using StackTracesTy =
36 SmallVector<std::pair<int, std::array<void *, MaxDepth>>, 0>;
37 StackTracesTy StackTraces;
38 DbgLocOrigin(bool ShouldCollectTrace);
39 void addTrace();
40 const StackTracesTy &getOriginStackTraces() const { return StackTraces; };
41};
42#else
43struct DbgLocOrigin {
44 DbgLocOrigin(bool) {}
45};
46#endif
47// Used to represent different "kinds" of DebugLoc, expressing that the
48// instruction it is part of is either normal and should contain a valid
49// DILocation, or otherwise describing the reason why the instruction does
50// not contain a valid DILocation.
51enum class DebugLocKind : uint8_t {
52 // The instruction is expected to contain a valid DILocation.
53 Normal,
54 // The instruction is compiler-generated, i.e. it is not associated with any
55 // line in the original source.
57 // The instruction has intentionally had its source location removed,
58 // typically because it was moved outside of its original control-flow and
59 // presenting the prior source location would be misleading for debuggers
60 // or profilers.
61 Dropped,
62 // The instruction does not have a known or currently knowable source
63 // location, e.g. the attribution is ambiguous in a way that can't be
64 // represented, or determining the correct location is complicated and
65 // requires future developer effort.
66 Unknown,
67 // DebugLoc is attached to an instruction that we don't expect to be
68 // emitted, and so can omit a valid DILocation; we don't expect to ever try
69 // and emit these into the line table, and trying to do so is a sign that
70 // something has gone wrong (most likely a DebugLoc leaking from a transient
71 // compiler-generated instruction).
72 Temporary
73};
74
75// Extends a DILocation pointer to also store a DebugLocKind and Origin,
76// allowing Debugify to ignore intentionally-empty DebugLocs and display the
77// code responsible for generating unintentionally-empty DebugLocs.
78// Currently we only need to track the Origin of this DILoc when using a
79// DebugLoc that is not annotated (i.e. has DebugLocKind::Normal) and has a
80// null DILocation, so only collect the origin stacktrace in those cases.
81class DILocAndCoverageTracking : public DbgLocOrigin {
82 DILocation *Loc;
83
84public:
85 DebugLocKind Kind;
86 // Default constructor for empty DebugLocs.
87 DILocAndCoverageTracking()
88 : DbgLocOrigin(true), Loc(nullptr), Kind(DebugLocKind::Normal) {}
89 // Valid or nullptr DILocation*, no annotative DebugLocKind.
90 DILocAndCoverageTracking(const DILocation *Loc)
91 : DbgLocOrigin(!Loc), Loc(const_cast<DILocation *>(Loc)),
92 Kind(DebugLocKind::Normal) {}
93 // Explicit DebugLocKind, which always means a nullptr DILocation*.
94 DILocAndCoverageTracking(DebugLocKind Kind)
95 : DbgLocOrigin(Kind == DebugLocKind::Normal), Loc(nullptr), Kind(Kind) {}
96
97 operator DILocation *() const { return Loc; }
98};
99template <> struct simplify_type<DILocAndCoverageTracking> {
100 using SimpleType = DILocation *;
101
102 static DILocation *getSimplifiedValue(DILocAndCoverageTracking &MD) {
103 return MD;
104 }
105};
106template <> struct simplify_type<const DILocAndCoverageTracking> {
107 using SimpleType = DILocation *;
108
109 static DILocation *getSimplifiedValue(const DILocAndCoverageTracking &MD) {
110 return MD;
111 }
112};
113
114using DebugLocRef = DILocAndCoverageTracking;
115#else
117#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
118
119/// A debug info location.
120///
121/// This class is a wrapper around an \a DILocation
122/// pointer.
123///
124/// To avoid extra includes, \a DebugLoc doubles the \a DILocation API with a
125/// one based on relatively opaque \a MDNode pointers.
126class DebugLoc {
127 DebugLocRef Loc = {};
128
129public:
130 /// Construct from an \a DILocation.
131 DebugLoc(const DILocation *L = nullptr) : Loc(const_cast<DILocation *>(L)) {}
132
133#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
134 DebugLoc(DebugLocKind Kind) : Loc(Kind) {}
135 DebugLocKind getKind() const { return Loc.Kind; }
136#endif
137
138#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
139 static inline DebugLoc getTemporary() {
140 return DebugLoc(DebugLocKind::Temporary);
141 }
142 static inline DebugLoc getUnknown() {
143 return DebugLoc(DebugLocKind::Unknown);
144 }
145 static inline DebugLoc getCompilerGenerated() {
146 return DebugLoc(DebugLocKind::CompilerGenerated);
147 }
148 static inline DebugLoc getDropped() {
149 return DebugLoc(DebugLocKind::Dropped);
150 }
151#else
152 static inline DebugLoc getTemporary() { return DebugLoc(); }
153 static inline DebugLoc getUnknown() { return DebugLoc(); }
154 static inline DebugLoc getCompilerGenerated() { return DebugLoc(); }
155 static inline DebugLoc getDropped() { return DebugLoc(); }
156#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
157
158 /// When two instructions are combined into a single instruction we also
159 /// need to combine the original locations into a single location.
160 /// When the locations are the same we can use either location.
161 /// When they differ, we need a third location which is distinct from
162 /// either. If they share a common scope, use this scope and compare the
163 /// line/column pair of the locations with the common scope:
164 /// * if both match, keep the line and column;
165 /// * if only the line number matches, keep the line and set the column as
166 /// 0;
167 /// * otherwise set line and column as 0.
168 /// If they do not share a common scope the location is ambiguous and can't
169 /// be represented in a line entry. In this case, set line and column as 0
170 /// and use the scope of any location.
171 ///
172 /// Intermediate-IR layers (`irlayers`) merge independently: the result keeps
173 /// the entries present in both, and none if either side has no layers or the
174 /// intersection is empty. Entries keep \p LocA's order and duplicate count,
175 /// so swapping the operands can yield a different list node with the same
176 /// entries -- LLVM assigns no meaning to layer order.
177 ///
178 /// \p LocA \p LocB: The locations to be merged.
180
181 /// Try to combine the vector of locations passed as input in a single one.
182 /// This function applies getMergedLocation() repeatedly left-to-right.
183 ///
184 /// \p Locs: The locations to be merged.
186
187 /// If this DebugLoc is non-empty, returns this DebugLoc; otherwise, selects
188 /// \p Other.
189 /// In coverage-tracking builds, this also accounts for whether this or
190 /// \p Other have an annotative DebugLocKind applied, such that if both are
191 /// empty but exactly one has an annotation, we prefer that annotated
192 /// location.
194 if (*this)
195 return *this;
196#if LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
197 if (Other)
198 return Other;
199 if (getKind() != DebugLocKind::Normal)
200 return *this;
201 if (Other.getKind() != DebugLocKind::Normal)
202 return Other;
203 return *this;
204#else
205 return Other;
206#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
207 }
208
209#if LLVM_ENABLE_DEBUGLOC_TRACKING_ORIGIN
210 const DbgLocOrigin::StackTracesTy &getOriginStackTraces() const {
211 return Loc.getOriginStackTraces();
212 }
213 DebugLoc getCopied() const {
214 DebugLoc NewDL = *this;
215 NewDL.Loc.addTrace();
216 return NewDL;
217 }
218#else
219 DebugLoc getCopied() const { return *this; }
220#endif
221
222 /// Get the underlying \a DILocation.
223 ///
224 /// \pre !*this or \c isa<DILocation>(getAsMDNode()).
225 /// @{
226 DILocation *get() const { return Loc; }
227 operator DILocation *() const { return get(); }
228 DILocation *operator->() const { return get(); }
229 DILocation &operator*() const { return *get(); }
230 /// @}
231
232 /// Check for null.
233 ///
234 /// Check for null in a way that is safe with broken debug info. Unlike
235 /// the conversion to \c DILocation, this doesn't require that \c Loc is of
236 /// the right type. Important for cases like \a llvm::StripDebugInfo() and
237 /// \a Instruction::hasMetadata().
238 explicit operator bool() const { return Loc; }
239
240 enum { ReplaceLastInlinedAt = true };
241 /// Rebuild the entire inlined-at chain for this instruction so that the top
242 /// of the chain now is inlined-at the new call site.
243 /// \param InlinedAt The new outermost inlined-at in the chain.
244 LLVM_ABI static DebugLoc
245 appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt, LLVMContext &Ctx,
247
248 /// Return true if the source locations match, ignoring isImplicitCode and
249 /// source atom info.
251 if (get() == Other.get())
252 return true;
253 return ((bool)*this == (bool)Other) && getLine() == Other.getLine() &&
254 getCol() == Other.getCol() && getScope() == Other.getScope() &&
255 getInlinedAt() == Other.getInlinedAt();
256 }
257
258 LLVM_ABI unsigned getLine() const;
259 LLVM_ABI unsigned getCol() const;
260 LLVM_ABI MDNode *getScope() const;
262 /// The raw intermediate-IR layer list (\a DILayerLocList) of the underlying
263 /// location, or null. Out-of-line so this header need not see DILocation's
264 /// definition.
266
267 /// Get the fully inlined-at scope for a DebugLoc.
268 ///
269 /// Gets the inlined-at scope for a DebugLoc.
271
272 /// Rebuild the entire inline-at chain by replacing the subprogram at the
273 /// end of the chain with NewSP.
274 LLVM_ABI static DebugLoc
276 LLVMContext &Ctx,
278
279 /// Find the debug info location for the start of the function.
280 ///
281 /// Walk up the scope chain of given debug loc and find line number info
282 /// for the function.
283 ///
284 /// FIXME: Remove this. Users should use DILocation/DILocalScope API to
285 /// find the subprogram, and then DILocation::get().
287
288 /// Return \c this as a bar \a MDNode.
289 LLVM_ABI MDNode *getAsMDNode() const;
290
291 /// Check if the DebugLoc corresponds to an implicit code.
292 LLVM_ABI bool isImplicitCode() const;
293 LLVM_ABI void setImplicitCode(bool ImplicitCode);
294
295 bool operator==(const DebugLoc &DL) const { return Loc == DL.Loc; }
296 bool operator!=(const DebugLoc &DL) const { return Loc != DL.Loc; }
297
298 LLVM_ABI void dump() const;
299
300 /// prints source location /path/to/file.exe:line:col @[inlined at]
301 LLVM_ABI void print(raw_ostream &OS) const;
302};
303
304} // end namespace llvm
305
306#endif // LLVM_IR_DEBUGLOC_H
aarch64 promote const
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
basic Basic Alias true
#define LLVM_ABI
Definition Compiler.h:215
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Subprogram description. Uses SubclassData1.
A debug info location.
Definition DebugLoc.h:126
LLVM_ABI void setImplicitCode(bool ImplicitCode)
Definition DebugLoc.cpp:89
DILocation & operator*() const
Definition DebugLoc.h:229
bool isSameSourceLocation(const DebugLoc &Other) const
Return true if the source locations match, ignoring isImplicitCode and source atom info.
Definition DebugLoc.h:250
static DebugLoc getCompilerGenerated()
Definition DebugLoc.h:154
bool operator==(const DebugLoc &DL) const
Definition DebugLoc.h:295
LLVM_ABI unsigned getLine() const
Definition DebugLoc.cpp:43
DebugLoc orElse(DebugLoc Other) const
If this DebugLoc is non-empty, returns this DebugLoc; otherwise, selects Other.
Definition DebugLoc.h:193
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
DebugLoc getCopied() const
Definition DebugLoc.h:219
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
DILocation * operator->() const
Definition DebugLoc.h:228
LLVM_ABI MDNode * getRawIRLayers() const
The raw intermediate-IR layer list (DILayerLocList) of the underlying location, or null.
Definition DebugLoc.cpp:63
static DebugLoc getTemporary()
Definition DebugLoc.h:152
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
static DebugLoc getUnknown()
Definition DebugLoc.h:153
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:58
static DebugLoc getDropped()
Definition DebugLoc.h:155
bool operator!=(const DebugLoc &DL) const
Definition DebugLoc.h:296
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
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.
@ Unknown
Not known to have no common set bits.
DILocation * DebugLocRef
Definition DebugLoc.h:116
@ Other
Any other memory.
Definition ModRef.h:68
Define a template that can be specialized by smart pointers to reflect the fact that they are automat...
Definition Casting.h:34
static SimpleType & getSimplifiedValue(From &Val)
Definition Casting.h:38