Skip to content

[NFC] Add fragment-getting functions to DbgRecord #97705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions llvm/include/llvm/IR/DbgVariableFragmentInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===- llvm/IR/DbgVariableFragmentInfo.h ------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Helper struct to describe a fragment of a debug variable.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_IR_DBGVARIABLEFRAGMENTINFO_H
#define LLVM_IR_DBGVARIABLEFRAGMENTINFO_H

#include <cstdint>

namespace llvm {
struct DbgVariableFragmentInfo {
DbgVariableFragmentInfo() = default;
DbgVariableFragmentInfo(uint64_t SizeInBits, uint64_t OffsetInBits)
: SizeInBits(SizeInBits), OffsetInBits(OffsetInBits) {}
uint64_t SizeInBits;
uint64_t OffsetInBits;
/// Return the index of the first bit of the fragment.
uint64_t startInBits() const { return OffsetInBits; }
/// Return the index of the bit after the end of the fragment, e.g. for
/// fragment offset=16 and size=32 return their sum, 48.
uint64_t endInBits() const { return OffsetInBits + SizeInBits; }

/// Returns a zero-sized fragment if A and B don't intersect.
static DbgVariableFragmentInfo intersect(DbgVariableFragmentInfo A,
DbgVariableFragmentInfo B) {
// Don't use std::max or min to avoid including <algorithm>.
uint64_t StartInBits =
A.OffsetInBits > B.OffsetInBits ? A.OffsetInBits : B.OffsetInBits;
uint64_t EndInBits =
A.endInBits() < B.endInBits() ? A.endInBits() : B.endInBits();
if (EndInBits <= StartInBits)
return {0, 0};
return DbgVariableFragmentInfo(EndInBits - StartInBits, StartInBits);
}
};
} // end namespace llvm

#endif // LLVM_IR_DBGVARIABLEFRAGMENTINFO_H
25 changes: 2 additions & 23 deletions llvm/include/llvm/IR/DebugInfoMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DbgVariableFragmentInfo.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/PseudoProbe.h"
#include "llvm/Support/Casting.h"
Expand Down Expand Up @@ -2886,29 +2887,7 @@ class DIExpression : public MDNode {
/// Return whether there is exactly one operator and it is a DW_OP_deref;
bool isDeref() const;

/// Holds the characteristics of one fragment of a larger variable.
struct FragmentInfo {
FragmentInfo() = default;
FragmentInfo(uint64_t SizeInBits, uint64_t OffsetInBits)
: SizeInBits(SizeInBits), OffsetInBits(OffsetInBits) {}
uint64_t SizeInBits;
uint64_t OffsetInBits;
/// Return the index of the first bit of the fragment.
uint64_t startInBits() const { return OffsetInBits; }
/// Return the index of the bit after the end of the fragment, e.g. for
/// fragment offset=16 and size=32 return their sum, 48.
uint64_t endInBits() const { return OffsetInBits + SizeInBits; }

/// Returns a zero-sized fragment if A and B don't intersect.
static DIExpression::FragmentInfo intersect(DIExpression::FragmentInfo A,
DIExpression::FragmentInfo B) {
uint64_t StartInBits = std::max(A.OffsetInBits, B.OffsetInBits);
uint64_t EndInBits = std::min(A.endInBits(), B.endInBits());
if (EndInBits <= StartInBits)
return {0, 0};
return DIExpression::FragmentInfo(EndInBits - StartInBits, StartInBits);
}
};
using FragmentInfo = DbgVariableFragmentInfo;

/// Return the number of bits that have an active value, i.e. those that
/// aren't known to be zero/sign (depending on the type of Var) and which
Expand Down
12 changes: 12 additions & 0 deletions llvm/include/llvm/IR/DebugProgramInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/ADT/iterator.h"
#include "llvm/IR/DbgVariableFragmentInfo.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/SymbolTableListTraits.h"
Expand Down Expand Up @@ -460,6 +461,17 @@ class DbgVariableRecord : public DbgRecord, protected DebugValueUser {
resetDebugValue(0, NewLocation);
}

std::optional<DbgVariableFragmentInfo> getFragment() const;
/// Get the FragmentInfo for the variable if it exists, otherwise return a
/// FragmentInfo that covers the entire variable if the variable size is
/// known, otherwise return a zero-sized fragment.
DbgVariableFragmentInfo getFragmentOrEntireVariable() const {
if (auto Frag = getFragment())
return *Frag;
if (auto Sz = getVariable()->getSizeInBits())
return {*Sz, 0};
return {0, 0};
}
/// Get the size (in bits) of the variable, or fragment of the variable that
/// is described.
std::optional<uint64_t> getFragmentSizeInBits() const;
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/IR/DebugProgramInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ bool DbgVariableRecord::isKillLocation() const {
any_of(location_ops(), [](Value *V) { return isa<UndefValue>(V); });
}

std::optional<DbgVariableFragmentInfo> DbgVariableRecord::getFragment() const {
return getExpression()->getFragmentInfo();
}

std::optional<uint64_t> DbgVariableRecord::getFragmentSizeInBits() const {
if (auto Fragment = getExpression()->getFragmentInfo())
return Fragment->SizeInBits;
Expand Down
Loading