Refactor the DIExpression fragment query interface (NFC)

... so it becomes available to DIExpressionCursor.

llvm-svn: 290322
This commit is contained in:
Adrian Prantl 2016-12-22 05:27:12 +00:00
parent b458841745
commit 49797ca6be
11 changed files with 59 additions and 50 deletions

View File

@ -1973,15 +1973,6 @@ public:
return Elements[I]; return Elements[I];
} }
/// Return whether this is a piece of an aggregate variable.
bool isFragment() const;
/// Return the offset of this fragment in bits.
uint64_t getFragmentOffsetInBits() const;
/// Return the size of this fragment in bits.
uint64_t getFragmentSizeInBits() const;
/// Determine whether this represents a standalone constant value. /// Determine whether this represents a standalone constant value.
bool isConstant() const; bool isConstant() const;
@ -2085,6 +2076,24 @@ public:
bool startsWithDeref() const { bool startsWithDeref() const {
return getNumElements() > 0 && getElement(0) == dwarf::DW_OP_deref; return getNumElements() > 0 && getElement(0) == dwarf::DW_OP_deref;
} }
/// Holds the characteristics of one fragment of a larger variable.
struct FragmentInfo {
uint64_t SizeInBits;
uint64_t OffsetInBits;
};
/// Retrieve the details of this fragment expression.
static Optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
expr_op_iterator End);
/// Retrieve the details of this fragment expression.
Optional<FragmentInfo> getFragmentInfo() const {
return getFragmentInfo(expr_op_begin(), expr_op_end());
}
/// Return whether this is a piece of an aggregate variable.
bool isFragment() const { return getFragmentInfo().hasValue(); }
}; };
/// Global variables. /// Global variables.

View File

@ -713,9 +713,10 @@ static bool emitDebugValueComment(const MachineInstr *MI, AsmPrinter &AP) {
OS << V->getName(); OS << V->getName();
const DIExpression *Expr = MI->getDebugExpression(); const DIExpression *Expr = MI->getDebugExpression();
if (Expr->isFragment()) auto Fragment = Expr->getFragmentInfo();
OS << " [fragment offset=" << Expr->getFragmentOffsetInBits() if (Fragment)
<< " size=" << Expr->getFragmentSizeInBits() << "]"; OS << " [fragment offset=" << Fragment->OffsetInBits
<< " size=" << Fragment->SizeInBits << "]";
OS << " <- "; OS << " <- ";
// The second operand is only an offset if it's an immediate. // The second operand is only an offset if it's an immediate.

View File

@ -944,9 +944,10 @@ void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
unsigned StructOffset = 0; unsigned StructOffset = 0;
// Handle fragments. // Handle fragments.
if (DIExpr && DIExpr->isFragment()) { auto Fragment = DIExpr->getFragmentInfo();
if (DIExpr && Fragment) {
IsSubfield = true; IsSubfield = true;
StructOffset = DIExpr->getFragmentOffsetInBits() / 8; StructOffset = Fragment->OffsetInBits / 8;
} else if (DIExpr && DIExpr->getNumElements() > 0) { } else if (DIExpr && DIExpr->getNumElements() > 0) {
continue; // Ignore unrecognized exprs. continue; // Ignore unrecognized exprs.
} }

View File

@ -65,10 +65,12 @@ MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
int DebugHandlerBase::fragmentCmp(const DIExpression *P1, int DebugHandlerBase::fragmentCmp(const DIExpression *P1,
const DIExpression *P2) { const DIExpression *P2) {
unsigned l1 = P1->getFragmentOffsetInBits(); auto Fragment1 = *P1->getFragmentInfo();
unsigned l2 = P2->getFragmentOffsetInBits(); auto Fragment2 = *P2->getFragmentInfo();
unsigned r1 = l1 + P1->getFragmentSizeInBits(); unsigned l1 = Fragment1.OffsetInBits;
unsigned r2 = l2 + P2->getFragmentSizeInBits(); unsigned l2 = Fragment2.OffsetInBits;
unsigned r1 = l1 + Fragment1.SizeInBits;
unsigned r2 = l2 + Fragment2.SizeInBits;
if (r1 <= l2) if (r1 <= l2)
return -1; return -1;
else if (r2 <= l1) else if (r2 <= l1)

View File

@ -175,8 +175,8 @@ inline bool operator==(const DebugLocEntry::Value &A,
/// Compare two fragments based on their offset. /// Compare two fragments based on their offset.
inline bool operator<(const DebugLocEntry::Value &A, inline bool operator<(const DebugLocEntry::Value &A,
const DebugLocEntry::Value &B) { const DebugLocEntry::Value &B) {
return A.getExpression()->getFragmentOffsetInBits() < return A.getExpression()->getFragmentInfo()->OffsetInBits <
B.getExpression()->getFragmentOffsetInBits(); B.getExpression()->getFragmentInfo()->OffsetInBits;
} }
} }

View File

@ -469,10 +469,12 @@ static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &
sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) { sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) {
std::sort(GVEs.begin(), GVEs.end(), std::sort(GVEs.begin(), GVEs.end(),
[](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) { [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) {
if (A.Expr != B.Expr && A.Expr && B.Expr && if (A.Expr != B.Expr && A.Expr && B.Expr) {
A.Expr->isFragment() && B.Expr->isFragment()) auto FragmentA = A.Expr->getFragmentInfo();
return A.Expr->getFragmentOffsetInBits() < auto FragmentB = B.Expr->getFragmentInfo();
B.Expr->getFragmentOffsetInBits(); if (FragmentA && FragmentB)
return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
}
return false; return false;
}); });
GVEs.erase(std::unique(GVEs.begin(), GVEs.end(), GVEs.erase(std::unique(GVEs.begin(), GVEs.end(),

View File

@ -285,7 +285,7 @@ void DwarfExpression::addFragmentOffset(const DIExpression *Expr) {
if (!Expr || !Expr->isFragment()) if (!Expr || !Expr->isFragment())
return; return;
uint64_t FragmentOffset = Expr->getFragmentOffsetInBits(); uint64_t FragmentOffset = Expr->getFragmentInfo()->OffsetInBits;
assert(FragmentOffset >= OffsetInBits && assert(FragmentOffset >= OffsetInBits &&
"overlapping or duplicate fragments"); "overlapping or duplicate fragments");
if (FragmentOffset > OffsetInBits) if (FragmentOffset > OffsetInBits)

View File

@ -601,22 +601,14 @@ bool DIExpression::isValid() const {
return true; return true;
} }
bool DIExpression::isFragment() const { Optional<DIExpression::FragmentInfo>
assert(isValid() && "Expected valid expression"); DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) {
if (unsigned N = getNumElements()) for (auto I = Start; I != End; ++I)
if (N >= 3) if (I->getOp() == dwarf::DW_OP_LLVM_fragment) {
return getElement(N - 3) == dwarf::DW_OP_LLVM_fragment; DIExpression::FragmentInfo Info = {I->getArg(1), I->getArg(0)};
return false; return Info;
} }
return None;
uint64_t DIExpression::getFragmentOffsetInBits() const {
assert(isFragment() && "Expected fragment");
return getElement(getNumElements() - 2);
}
uint64_t DIExpression::getFragmentSizeInBits() const {
assert(isFragment() && "Expected fragment");
return getElement(getNumElements() - 1);
} }
bool DIExpression::isConstant() const { bool DIExpression::isConstant() const {

View File

@ -4344,7 +4344,8 @@ void Verifier::verifyFragmentExpression(const DbgInfoIntrinsic &I) {
return; return;
// Nothing to do if this isn't a bit piece expression. // Nothing to do if this isn't a bit piece expression.
if (!E->isFragment()) auto Fragment = E->getFragmentInfo();
if (!Fragment)
return; return;
// The frontend helps out GDB by emitting the members of local anonymous // The frontend helps out GDB by emitting the members of local anonymous
@ -4362,8 +4363,8 @@ void Verifier::verifyFragmentExpression(const DbgInfoIntrinsic &I) {
if (!VarSize) if (!VarSize)
return; return;
unsigned FragSize = E->getFragmentSizeInBits(); unsigned FragSize = Fragment->SizeInBits;
unsigned FragOffset = E->getFragmentOffsetInBits(); unsigned FragOffset = Fragment->OffsetInBits;
AssertDI(FragSize + FragOffset <= VarSize, AssertDI(FragSize + FragOffset <= VarSize,
"fragment is larger than or outside of variable", &I, V, E); "fragment is larger than or outside of variable", &I, V, E);
AssertDI(FragSize != VarSize, "fragment covers entire variable", &I, V, E); AssertDI(FragSize != VarSize, "fragment covers entire variable", &I, V, E);

View File

@ -4026,13 +4026,13 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
if (Fragment.Size < AllocaSize || Expr->isFragment()) { if (Fragment.Size < AllocaSize || Expr->isFragment()) {
// If this alloca is already a scalar replacement of a larger aggregate, // If this alloca is already a scalar replacement of a larger aggregate,
// Fragment.Offset describes the offset inside the scalar. // Fragment.Offset describes the offset inside the scalar.
uint64_t Offset = auto ExprFragment = Expr->getFragmentInfo();
Expr->isFragment() ? Expr->getFragmentOffsetInBits() : 0; uint64_t Offset = ExprFragment ? ExprFragment->OffsetInBits : 0;
uint64_t Start = Offset + Fragment.Offset; uint64_t Start = Offset + Fragment.Offset;
uint64_t Size = Fragment.Size; uint64_t Size = Fragment.Size;
if (Expr->isFragment()) { if (ExprFragment) {
uint64_t AbsEnd = uint64_t AbsEnd =
Expr->getFragmentOffsetInBits() + Expr->getFragmentSizeInBits(); ExprFragment->OffsetInBits + ExprFragment->SizeInBits;
if (Start >= AbsEnd) if (Start >= AbsEnd)
// No need to describe a SROAed padding. // No need to describe a SROAed padding.
continue; continue;

View File

@ -1112,9 +1112,10 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
unsigned FragmentOffset = 0; unsigned FragmentOffset = 0;
// If this already is a bit fragment, we drop the bit fragment from the // If this already is a bit fragment, we drop the bit fragment from the
// expression and record the offset. // expression and record the offset.
if (DIExpr->isFragment()) { auto Fragment = DIExpr->getFragmentInfo();
if (Fragment) {
Ops.append(DIExpr->elements_begin(), DIExpr->elements_end()-3); Ops.append(DIExpr->elements_begin(), DIExpr->elements_end()-3);
FragmentOffset = DIExpr->getFragmentOffsetInBits(); FragmentOffset = Fragment->OffsetInBits;
} else { } else {
Ops.append(DIExpr->elements_begin(), DIExpr->elements_end()); Ops.append(DIExpr->elements_begin(), DIExpr->elements_end());
} }