Skip to content

Commit f21b62b

Browse files
authored
[NFC] Add fragment-getting functions to DbgRecord (#97705)
Patch [1/x] to fix structured bindings debug info in SROA. Copy getFragment and getFragmentOrEntireVariable from DbgVariableIntrinsic. Move FragmentInfo out of DIExpression and DebugInfoMetadata.h into a new file DbgVariableFragmentInfo.h so it can be included into DebugProgramInstruction.h without pulling in other includes and classes. These functions will be used in subsequent patches.
1 parent c119da2 commit f21b62b

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===- llvm/IR/DbgVariableFragmentInfo.h ------------------------*- 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+
// Helper struct to describe a fragment of a debug variable.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#ifndef LLVM_IR_DBGVARIABLEFRAGMENTINFO_H
13+
#define LLVM_IR_DBGVARIABLEFRAGMENTINFO_H
14+
15+
#include <cstdint>
16+
17+
namespace llvm {
18+
struct DbgVariableFragmentInfo {
19+
DbgVariableFragmentInfo() = default;
20+
DbgVariableFragmentInfo(uint64_t SizeInBits, uint64_t OffsetInBits)
21+
: SizeInBits(SizeInBits), OffsetInBits(OffsetInBits) {}
22+
uint64_t SizeInBits;
23+
uint64_t OffsetInBits;
24+
/// Return the index of the first bit of the fragment.
25+
uint64_t startInBits() const { return OffsetInBits; }
26+
/// Return the index of the bit after the end of the fragment, e.g. for
27+
/// fragment offset=16 and size=32 return their sum, 48.
28+
uint64_t endInBits() const { return OffsetInBits + SizeInBits; }
29+
30+
/// Returns a zero-sized fragment if A and B don't intersect.
31+
static DbgVariableFragmentInfo intersect(DbgVariableFragmentInfo A,
32+
DbgVariableFragmentInfo B) {
33+
// Don't use std::max or min to avoid including <algorithm>.
34+
uint64_t StartInBits =
35+
A.OffsetInBits > B.OffsetInBits ? A.OffsetInBits : B.OffsetInBits;
36+
uint64_t EndInBits =
37+
A.endInBits() < B.endInBits() ? A.endInBits() : B.endInBits();
38+
if (EndInBits <= StartInBits)
39+
return {0, 0};
40+
return DbgVariableFragmentInfo(EndInBits - StartInBits, StartInBits);
41+
}
42+
};
43+
} // end namespace llvm
44+
45+
#endif // LLVM_IR_DBGVARIABLEFRAGMENTINFO_H

llvm/include/llvm/IR/DebugInfoMetadata.h

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ADT/StringRef.h"
2222
#include "llvm/ADT/iterator_range.h"
2323
#include "llvm/IR/Constants.h"
24+
#include "llvm/IR/DbgVariableFragmentInfo.h"
2425
#include "llvm/IR/Metadata.h"
2526
#include "llvm/IR/PseudoProbe.h"
2627
#include "llvm/Support/Casting.h"
@@ -2886,29 +2887,7 @@ class DIExpression : public MDNode {
28862887
/// Return whether there is exactly one operator and it is a DW_OP_deref;
28872888
bool isDeref() const;
28882889

2889-
/// Holds the characteristics of one fragment of a larger variable.
2890-
struct FragmentInfo {
2891-
FragmentInfo() = default;
2892-
FragmentInfo(uint64_t SizeInBits, uint64_t OffsetInBits)
2893-
: SizeInBits(SizeInBits), OffsetInBits(OffsetInBits) {}
2894-
uint64_t SizeInBits;
2895-
uint64_t OffsetInBits;
2896-
/// Return the index of the first bit of the fragment.
2897-
uint64_t startInBits() const { return OffsetInBits; }
2898-
/// Return the index of the bit after the end of the fragment, e.g. for
2899-
/// fragment offset=16 and size=32 return their sum, 48.
2900-
uint64_t endInBits() const { return OffsetInBits + SizeInBits; }
2901-
2902-
/// Returns a zero-sized fragment if A and B don't intersect.
2903-
static DIExpression::FragmentInfo intersect(DIExpression::FragmentInfo A,
2904-
DIExpression::FragmentInfo B) {
2905-
uint64_t StartInBits = std::max(A.OffsetInBits, B.OffsetInBits);
2906-
uint64_t EndInBits = std::min(A.endInBits(), B.endInBits());
2907-
if (EndInBits <= StartInBits)
2908-
return {0, 0};
2909-
return DIExpression::FragmentInfo(EndInBits - StartInBits, StartInBits);
2910-
}
2911-
};
2890+
using FragmentInfo = DbgVariableFragmentInfo;
29122891

29132892
/// Return the number of bits that have an active value, i.e. those that
29142893
/// aren't known to be zero/sign (depending on the type of Var) and which

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "llvm/ADT/ilist.h"
5151
#include "llvm/ADT/ilist_node.h"
5252
#include "llvm/ADT/iterator.h"
53+
#include "llvm/IR/DbgVariableFragmentInfo.h"
5354
#include "llvm/IR/DebugLoc.h"
5455
#include "llvm/IR/Instruction.h"
5556
#include "llvm/IR/SymbolTableListTraits.h"
@@ -460,6 +461,17 @@ class DbgVariableRecord : public DbgRecord, protected DebugValueUser {
460461
resetDebugValue(0, NewLocation);
461462
}
462463

464+
std::optional<DbgVariableFragmentInfo> getFragment() const;
465+
/// Get the FragmentInfo for the variable if it exists, otherwise return a
466+
/// FragmentInfo that covers the entire variable if the variable size is
467+
/// known, otherwise return a zero-sized fragment.
468+
DbgVariableFragmentInfo getFragmentOrEntireVariable() const {
469+
if (auto Frag = getFragment())
470+
return *Frag;
471+
if (auto Sz = getVariable()->getSizeInBits())
472+
return {*Sz, 0};
473+
return {0, 0};
474+
}
463475
/// Get the size (in bits) of the variable, or fragment of the variable that
464476
/// is described.
465477
std::optional<uint64_t> getFragmentSizeInBits() const;

llvm/lib/IR/DebugProgramInstruction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ bool DbgVariableRecord::isKillLocation() const {
371371
any_of(location_ops(), [](Value *V) { return isa<UndefValue>(V); });
372372
}
373373

374+
std::optional<DbgVariableFragmentInfo> DbgVariableRecord::getFragment() const {
375+
return getExpression()->getFragmentInfo();
376+
}
377+
374378
std::optional<uint64_t> DbgVariableRecord::getFragmentSizeInBits() const {
375379
if (auto Fragment = getExpression()->getFragmentInfo())
376380
return Fragment->SizeInBits;

0 commit comments

Comments
 (0)