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