Skip to content

Commit e9fbb17

Browse files
[NFC][DebugInfo] Factor out debug location code
This patch moves, into its own function, the code computing a `FileAndLocation` for the instruction being lowered (SIL>LLVM IR). Control flows is simplified as a result.
1 parent 3914cdd commit e9fbb17

File tree

1 file changed

+43
-32
lines changed

1 file changed

+43
-32
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
341341
IRGenDebugInfoFormat getDebugInfoFormat() { return Opts.DebugInfoFormat; }
342342

343343
private:
344+
/// Convert a SILLocation into the corresponding LLVM Loc.
345+
FileAndLocation computeLLVMLoc(const SILDebugScope *DS, SILLocation Loc);
346+
344347
static StringRef getFilenameFromDC(const DeclContext *DC) {
345348
if (auto *LF = dyn_cast<LoadedFile>(DC))
346349
return LF->getFilename();
@@ -2611,6 +2614,45 @@ bool IRGenDebugInfoImpl::lineEntryIsSane(FileAndLocation DL,
26112614
}
26122615
#endif
26132616

2617+
IRGenDebugInfoImpl::FileAndLocation
2618+
IRGenDebugInfoImpl::computeLLVMLoc(const SILDebugScope *DS, SILLocation Loc) {
2619+
// NOTE: In CodeView, zero is not an artificial line location. We try to
2620+
// avoid those line locations near user code to reduce the number
2621+
// of breaks in the linetables.
2622+
SILFunction *Fn = DS->getInlinedFunction();
2623+
if (Fn && (Fn->isThunk() || Fn->isTransparent()))
2624+
return {0, 0, CompilerGeneratedFile};
2625+
2626+
// Reuse the last source location if we are still in the same scope to get a
2627+
// more contiguous line table.
2628+
if (DS == LastScope && Loc.isHiddenFromDebugInfo())
2629+
return LastFileAndLocation;
2630+
2631+
// If the scope has not changed and the line number is either zero or
2632+
// artificial, we want to keep the most recent debug location.
2633+
if (DS == LastScope &&
2634+
(Loc.is<ArtificialUnreachableLocation>() || Loc.isLineZero(SM)) &&
2635+
Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView)
2636+
return LastFileAndLocation;
2637+
2638+
FileAndLocation L;
2639+
// Decode the location.
2640+
if (!Loc.isInPrologue() ||
2641+
Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView)
2642+
L = decodeFileAndLocation(Loc);
2643+
2644+
// Otherwise use a line 0 artificial location, but the file from the
2645+
// location. If we are emitting CodeView, we do not want to use line zero
2646+
// since it does not represent an artificial line location.
2647+
if (Loc.isHiddenFromDebugInfo() &&
2648+
Opts.DebugInfoFormat != IRGenDebugInfoFormat::CodeView) {
2649+
L.Line = 0;
2650+
L.Column = 0;
2651+
}
2652+
2653+
return L;
2654+
}
2655+
26142656
void IRGenDebugInfoImpl::setCurrentLoc(IRBuilder &Builder,
26152657
const SILDebugScope *DS,
26162658
SILLocation Loc) {
@@ -2619,38 +2661,7 @@ void IRGenDebugInfoImpl::setCurrentLoc(IRBuilder &Builder,
26192661
if (!Scope)
26202662
return;
26212663

2622-
// NOTE: In CodeView, zero is not an artificial line location. We try to
2623-
// avoid those line locations near user code to reduce the number
2624-
// of breaks in the linetables.
2625-
FileAndLocation L;
2626-
SILFunction *Fn = DS->getInlinedFunction();
2627-
if (Fn && (Fn->isThunk() || Fn->isTransparent())) {
2628-
L = {0, 0, CompilerGeneratedFile};
2629-
} else if (DS == LastScope && Loc.isHiddenFromDebugInfo()) {
2630-
// Reuse the last source location if we are still in the same
2631-
// scope to get a more contiguous line table.
2632-
L = LastFileAndLocation;
2633-
} else if (DS == LastScope &&
2634-
(Loc.is<ArtificialUnreachableLocation>() || Loc.isLineZero(SM)) &&
2635-
Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView) {
2636-
// If the scope has not changed and the line number is either zero or
2637-
// artificial, we want to keep the most recent debug location.
2638-
L = LastFileAndLocation;
2639-
} else {
2640-
// Decode the location.
2641-
if (!Loc.isInPrologue() ||
2642-
Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView)
2643-
L = decodeFileAndLocation(Loc);
2644-
2645-
// Otherwise use a line 0 artificial location, but the file from the
2646-
// location. If we are emitting CodeView, we do not want to use line zero
2647-
// since it does not represent an artificial line location.
2648-
if (Loc.isHiddenFromDebugInfo() &&
2649-
Opts.DebugInfoFormat != IRGenDebugInfoFormat::CodeView) {
2650-
L.Line = 0;
2651-
L.Column = 0;
2652-
}
2653-
}
2664+
FileAndLocation L = computeLLVMLoc(DS, Loc);
26542665

26552666
if (L.getFilename() != Scope->getFilename()) {
26562667
// We changed files in the middle of a scope. This happens, for

0 commit comments

Comments
 (0)