AsmPrinter: Store abbreviation data directly in DIE and DIEValue

Stop storing a `DIEAbbrev` in `DIE`, since the data fits neatly inside
the `DIEValue` list.  Besides being a cleaner data structure (avoiding
the parallel arrays), this gives us more freedom to rearrange the
`DIEValue` list.

This fixes the temporary memory regression from 845 MB up to 879 MB, and
drops it further to 829 MB for a net memory decrease of around 1.9%
(incremental decrease around 5.7%).

(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@238364 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith 2015-05-27 22:31:41 +00:00
parent 09fe4bf794
commit 611a2f2322
9 changed files with 128 additions and 98 deletions

View File

@ -315,7 +315,9 @@ public:
private: private:
/// Ty - Type of data stored in the value. /// Ty - Type of data stored in the value.
/// ///
Type Ty; Type Ty = isNone;
dwarf::Attribute Attribute = (dwarf::Attribute)0;
dwarf::Form Form = (dwarf::Form)0;
/// Storage for the value. /// Storage for the value.
/// ///
@ -387,20 +389,28 @@ private:
} }
public: public:
DIEValue() : Ty(isNone) {} DIEValue() = default;
DIEValue(const DIEValue &X) : Ty(X.Ty) { copyVal(X); } DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
copyVal(X);
}
DIEValue &operator=(const DIEValue &X) { DIEValue &operator=(const DIEValue &X) {
destroyVal(); destroyVal();
Ty = X.Ty; Ty = X.Ty;
Attribute = X.Attribute;
Form = X.Form;
copyVal(X); copyVal(X);
return *this; return *this;
} }
~DIEValue() { destroyVal(); } ~DIEValue() { destroyVal(); }
#define HANDLE_DIEVALUE_SMALL(T) \ #define HANDLE_DIEVALUE_SMALL(T) \
DIEValue(const DIE##T &V) : Ty(is##T) { construct<DIE##T>(V); } DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T &V) \
: Ty(is##T), Attribute(Attribute), Form(Form) { \
construct<DIE##T>(V); \
}
#define HANDLE_DIEVALUE_LARGE(T) \ #define HANDLE_DIEVALUE_LARGE(T) \
DIEValue(const DIE##T *V) : Ty(is##T) { \ DIEValue(dwarf::Attribute Attribute, dwarf::Form Form, const DIE##T *V) \
: Ty(is##T), Attribute(Attribute), Form(Form) { \
assert(V && "Expected valid value"); \ assert(V && "Expected valid value"); \
construct<const DIE##T *>(V); \ construct<const DIE##T *>(V); \
} }
@ -408,6 +418,8 @@ public:
// Accessors // Accessors
Type getType() const { return Ty; } Type getType() const { return Ty; }
dwarf::Attribute getAttribute() const { return Attribute; }
dwarf::Form getForm() const { return Form; }
explicit operator bool() const { return Ty; } explicit operator bool() const { return Ty; }
#define HANDLE_DIEVALUE_SMALL(T) \ #define HANDLE_DIEVALUE_SMALL(T) \
@ -449,9 +461,11 @@ protected:
/// ///
unsigned Size; unsigned Size;
/// Abbrev - Buffer for constructing abbreviation. unsigned AbbrevNumber = ~0u;
/// Tag - Dwarf tag code.
/// ///
DIEAbbrev Abbrev; dwarf::Tag Tag = (dwarf::Tag)0;
/// Children DIEs. /// Children DIEs.
/// ///
@ -470,22 +484,18 @@ protected:
SmallVector<DIEValue, 12> Values; SmallVector<DIEValue, 12> Values;
protected: protected:
DIE() DIE() : Offset(0), Size(0), Parent(nullptr) {}
: Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
Parent(nullptr) {}
public: public:
explicit DIE(dwarf::Tag Tag) explicit DIE(dwarf::Tag Tag)
: Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no), : Offset(0), Size(0), Tag(Tag), Parent(nullptr) {}
Parent(nullptr) {}
// Accessors. // Accessors.
DIEAbbrev &getAbbrev() { return Abbrev; } unsigned getAbbrevNumber() const { return AbbrevNumber; }
const DIEAbbrev &getAbbrev() const { return Abbrev; } dwarf::Tag getTag() const { return Tag; }
unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
dwarf::Tag getTag() const { return Abbrev.getTag(); }
unsigned getOffset() const { return Offset; } unsigned getOffset() const { return Offset; }
unsigned getSize() const { return Size; } unsigned getSize() const { return Size; }
bool hasChildren() const { return !Children.empty(); }
const std::vector<std::unique_ptr<DIE>> &getChildren() const { const std::vector<std::unique_ptr<DIE>> &getChildren() const {
return Children; return Children;
} }
@ -495,6 +505,16 @@ public:
Values[I] = New; Values[I] = New;
} }
DIE *getParent() const { return Parent; } DIE *getParent() const { return Parent; }
/// Generate the abbreviation for this DIE.
///
/// Calculate the abbreviation for this, which should be uniqued and
/// eventually used to call \a setAbbrevNumber().
DIEAbbrev generateAbbrev() const;
/// Set the abbreviation number for this DIE.
void setAbbrevNumber(unsigned I) { AbbrevNumber = I; }
/// Climb up the parent chain to get the compile or type unit DIE this DIE /// Climb up the parent chain to get the compile or type unit DIE this DIE
/// belongs to. /// belongs to.
const DIE *getUnit() const; const DIE *getUnit() const;
@ -506,16 +526,16 @@ public:
/// addValue - Add a value and attributes to a DIE. /// addValue - Add a value and attributes to a DIE.
/// ///
void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue Value) { void addValue(DIEValue Value) { Values.push_back(Value); }
Abbrev.AddAttribute(Attribute, Form); template <class T>
Values.push_back(Value); void addValue(dwarf::Attribute Attribute, dwarf::Form Form, T &&Value) {
Values.emplace_back(Attribute, Form, std::forward<T>(Value));
} }
/// addChild - Add a child to the DIE. /// addChild - Add a child to the DIE.
/// ///
void addChild(std::unique_ptr<DIE> Child) { void addChild(std::unique_ptr<DIE> Child) {
assert(!Child->getParent()); assert(!Child->getParent());
Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
Child->Parent = this; Child->Parent = this;
Children.push_back(std::move(Child)); Children.push_back(std::move(Child));
} }

View File

@ -254,24 +254,20 @@ void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
} }
void AsmPrinter::emitDwarfDIE(const DIE &Die) const { void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
// Get the abbreviation for this DIE.
const DIEAbbrev &Abbrev = Die.getAbbrev();
// Emit the code (index) for the abbreviation. // Emit the code (index) for the abbreviation.
if (isVerbose()) if (isVerbose())
OutStreamer->AddComment("Abbrev [" + Twine(Abbrev.getNumber()) + OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
"] 0x" + Twine::utohexstr(Die.getOffset()) + Twine::utohexstr(Die.getOffset()) + ":0x" +
":0x" + Twine::utohexstr(Die.getSize()) + " " + Twine::utohexstr(Die.getSize()) + " " +
dwarf::TagString(Abbrev.getTag())); dwarf::TagString(Die.getTag()));
EmitULEB128(Abbrev.getNumber()); EmitULEB128(Die.getAbbrevNumber());
const SmallVectorImpl<DIEValue> &Values = Die.getValues(); const SmallVectorImpl<DIEValue> &Values = Die.getValues();
const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
// Emit the DIE attribute values. // Emit the DIE attribute values.
for (unsigned i = 0, N = Values.size(); i < N; ++i) { for (unsigned i = 0, N = Values.size(); i < N; ++i) {
dwarf::Attribute Attr = AbbrevData[i].getAttribute(); dwarf::Attribute Attr = Values[i].getAttribute();
dwarf::Form Form = AbbrevData[i].getForm(); dwarf::Form Form = Values[i].getForm();
assert(Form && "Too many attributes for DIE (check abbreviation)"); assert(Form && "Too many attributes for DIE (check abbreviation)");
if (isVerbose()) { if (isVerbose()) {
@ -286,7 +282,7 @@ void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
} }
// Emit the DIE children if any. // Emit the DIE children if any.
if (Abbrev.hasChildren()) { if (Die.hasChildren()) {
for (auto &Child : Die.getChildren()) for (auto &Child : Die.getChildren())
emitDwarfDIE(*Child); emitDwarfDIE(*Child);

View File

@ -107,6 +107,13 @@ void DIEAbbrev::print(raw_ostream &O) {
void DIEAbbrev::dump() { print(dbgs()); } void DIEAbbrev::dump() { print(dbgs()); }
#endif #endif
DIEAbbrev DIE::generateAbbrev() const {
DIEAbbrev Abbrev(Tag, hasChildren());
for (const DIEValue &V : Values)
Abbrev.AddAttribute(V.getAttribute(), V.getForm());
return Abbrev;
}
/// Climb up the parent chain to get the unit DIE to which this DIE /// Climb up the parent chain to get the unit DIE to which this DIE
/// belongs. /// belongs.
const DIE *DIE::getUnit() const { const DIE *DIE::getUnit() const {
@ -130,12 +137,11 @@ const DIE *DIE::getUnitOrNull() const {
DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const { DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
const SmallVectorImpl<DIEValue> &Values = getValues(); const SmallVectorImpl<DIEValue> &Values = getValues();
const DIEAbbrev &Abbrevs = getAbbrev();
// Iterate through all the attributes until we find the one we're // Iterate through all the attributes until we find the one we're
// looking for, if we can't find it return NULL. // looking for, if we can't find it return NULL.
for (size_t i = 0; i < Values.size(); ++i) for (size_t i = 0; i < Values.size(); ++i)
if (Abbrevs.getData()[i].getAttribute() == Attribute) if (Values[i].getAttribute() == Attribute)
return Values[i]; return Values[i];
return DIEValue(); return DIEValue();
} }
@ -143,7 +149,7 @@ DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
#ifndef NDEBUG #ifndef NDEBUG
void DIE::print(raw_ostream &O, unsigned IndentCount) const { void DIE::print(raw_ostream &O, unsigned IndentCount) const {
const std::string Indent(IndentCount, ' '); const std::string Indent(IndentCount, ' ');
bool isBlock = Abbrev.getTag() == 0; bool isBlock = getTag() == 0;
if (!isBlock) { if (!isBlock) {
O << Indent O << Indent
@ -153,26 +159,24 @@ void DIE::print(raw_ostream &O, unsigned IndentCount) const {
<< ", Size: " << Size << "\n"; << ", Size: " << Size << "\n";
O << Indent O << Indent
<< dwarf::TagString(Abbrev.getTag()) << dwarf::TagString(getTag())
<< " " << " "
<< dwarf::ChildrenString(Abbrev.hasChildren()) << "\n"; << dwarf::ChildrenString(hasChildren()) << "\n";
} else { } else {
O << "Size: " << Size << "\n"; O << "Size: " << Size << "\n";
} }
const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
IndentCount += 2; IndentCount += 2;
for (unsigned i = 0, N = Data.size(); i < N; ++i) { for (unsigned i = 0, N = Values.size(); i < N; ++i) {
O << Indent; O << Indent;
if (!isBlock) if (!isBlock)
O << dwarf::AttributeString(Data[i].getAttribute()); O << dwarf::AttributeString(Values[i].getAttribute());
else else
O << "Blk[" << i << "]"; O << "Blk[" << i << "]";
O << " " O << " "
<< dwarf::FormEncodingString(Data[i].getForm()) << dwarf::FormEncodingString(Values[i].getForm())
<< " "; << " ";
Values[i].print(O); Values[i].print(O);
O << "\n"; O << "\n";
@ -505,9 +509,8 @@ void DIETypeSignature::print(raw_ostream &O) const {
/// ///
unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const { unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
if (!Size) { if (!Size) {
const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
for (unsigned i = 0, N = Values.size(); i < N; ++i) for (unsigned i = 0, N = Values.size(); i < N; ++i)
Size += Values[i].SizeOf(AP, AbbrevData[i].getForm()); Size += Values[i].SizeOf(AP, Values[i].getForm());
} }
return Size; return Size;
@ -526,9 +529,8 @@ void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Asm->EmitULEB128(Size); break; Asm->EmitULEB128(Size); break;
} }
const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
for (unsigned i = 0, N = Values.size(); i < N; ++i) for (unsigned i = 0, N = Values.size(); i < N; ++i)
Values[i].EmitValue(Asm, AbbrevData[i].getForm()); Values[i].EmitValue(Asm, Values[i].getForm());
} }
/// SizeOf - Determine size of location data in bytes. /// SizeOf - Determine size of location data in bytes.
@ -560,9 +562,8 @@ void DIELoc::print(raw_ostream &O) const {
/// ///
unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const { unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
if (!Size) { if (!Size) {
const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
for (unsigned i = 0, N = Values.size(); i < N; ++i) for (unsigned i = 0, N = Values.size(); i < N; ++i)
Size += Values[i].SizeOf(AP, AbbrevData[i].getForm()); Size += Values[i].SizeOf(AP, Values[i].getForm());
} }
return Size; return Size;
@ -579,9 +580,8 @@ void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break; case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
} }
const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
for (unsigned i = 0, N = Values.size(); i < N; ++i) for (unsigned i = 0, N = Values.size(); i < N; ++i)
Values[i].EmitValue(Asm, AbbrevData[i].getForm()); Values[i].EmitValue(Asm, Values[i].getForm());
} }
/// SizeOf - Determine size of block data in bytes. /// SizeOf - Determine size of block data in bytes.

View File

@ -32,12 +32,11 @@ using namespace llvm;
/// a reference to it. /// a reference to it.
static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) { static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
const auto &Values = Die.getValues(); const auto &Values = Die.getValues();
const DIEAbbrev &Abbrevs = Die.getAbbrev();
// Iterate through all the attributes until we find the one we're // Iterate through all the attributes until we find the one we're
// looking for, if we can't find it return an empty string. // looking for, if we can't find it return an empty string.
for (size_t i = 0; i < Values.size(); ++i) { for (size_t i = 0; i < Values.size(); ++i) {
if (Abbrevs.getData()[i].getAttribute() == Attr) if (Values[i].getAttribute() == Attr)
return Values[i].getDIEString().getString(); return Values[i].getDIEString().getString();
} }
return StringRef(""); return StringRef("");
@ -120,19 +119,17 @@ void DIEHash::addParentContext(const DIE &Parent) {
// Collect all of the attributes for a particular DIE in single structure. // Collect all of the attributes for a particular DIE in single structure.
void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) { void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
const SmallVectorImpl<DIEValue> &Values = Die.getValues(); const SmallVectorImpl<DIEValue> &Values = Die.getValues();
const DIEAbbrev &Abbrevs = Die.getAbbrev();
#define COLLECT_ATTR(NAME) \ #define COLLECT_ATTR(NAME) \
case dwarf::NAME: \ case dwarf::NAME: \
Attrs.NAME.Val = Values[i]; \ Attrs.NAME.Val = Values[i]; \
Attrs.NAME.Desc = &Abbrevs.getData()[i]; \
break break
for (size_t i = 0, e = Values.size(); i != e; ++i) { for (size_t i = 0, e = Values.size(); i != e; ++i) {
DEBUG(dbgs() << "Attribute: " DEBUG(dbgs() << "Attribute: "
<< dwarf::AttributeString(Abbrevs.getData()[i].getAttribute()) << dwarf::AttributeString(Values[i].getAttribute())
<< " added.\n"); << " added.\n");
switch (Abbrevs.getData()[i].getAttribute()) { switch (Values[i].getAttribute()) {
COLLECT_ATTR(DW_AT_name); COLLECT_ATTR(DW_AT_name);
COLLECT_ATTR(DW_AT_accessibility); COLLECT_ATTR(DW_AT_accessibility);
COLLECT_ATTR(DW_AT_address_class); COLLECT_ATTR(DW_AT_address_class);
@ -288,8 +285,7 @@ void DIEHash::hashLocList(const DIELocList &LocList) {
// the form. // the form.
void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) { void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
const DIEValue &Value = Attr.Val; const DIEValue &Value = Attr.Val;
const DIEAbbrevData *Desc = Attr.Desc; dwarf::Attribute Attribute = Value.getAttribute();
dwarf::Attribute Attribute = Desc->getAttribute();
// Other attribute values use the letter 'A' as the marker, and the value // Other attribute values use the letter 'A' as the marker, and the value
// consists of the form code (encoded as an unsigned LEB128 value) followed by // consists of the form code (encoded as an unsigned LEB128 value) followed by
@ -311,7 +307,7 @@ void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
case DIEValue::isInteger: { case DIEValue::isInteger: {
addULEB128('A'); addULEB128('A');
addULEB128(Attribute); addULEB128(Attribute);
switch (Desc->getForm()) { switch (Value.getForm()) {
case dwarf::DW_FORM_data1: case dwarf::DW_FORM_data1:
case dwarf::DW_FORM_data2: case dwarf::DW_FORM_data2:
case dwarf::DW_FORM_data4: case dwarf::DW_FORM_data4:

View File

@ -28,9 +28,10 @@ class CompileUnit;
class DIEHash { class DIEHash {
// The entry for a particular attribute. // The entry for a particular attribute.
//
// FIXME: Remove this struct, it's pretty boring now.
struct AttrEntry { struct AttrEntry {
DIEValue Val; DIEValue Val;
const DIEAbbrevData *Desc;
}; };
// Collection of all attributes used in hashing a particular DIE. // Collection of all attributes used in hashing a particular DIE.

View File

@ -51,8 +51,10 @@ void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
if (Label) if (Label)
DD->addArangeLabel(SymbolCU(this, Label)); DD->addArangeLabel(SymbolCU(this, Label));
Die.addValue(Attribute, dwarf::DW_FORM_addr, if (Label)
Label ? DIEValue(DIELabel(Label)) : DIEValue(DIEInteger(0))); Die.addValue(Attribute, dwarf::DW_FORM_addr, DIELabel(Label));
else
Die.addValue(Attribute, dwarf::DW_FORM_addr, DIEInteger(0));
} }
unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName,
@ -253,9 +255,7 @@ void DwarfCompileUnit::initStmtList() {
} }
void DwarfCompileUnit::applyStmtList(DIE &D) { void DwarfCompileUnit::applyStmtList(DIE &D) {
D.addValue(dwarf::DW_AT_stmt_list, D.addValue(UnitDie.getValues()[stmtListIndex]);
UnitDie.getAbbrev().getData()[stmtListIndex].getForm(),
UnitDie.getValues()[stmtListIndex]);
} }
void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin, void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,

View File

@ -24,21 +24,27 @@ DwarfFile::~DwarfFile() {}
// Define a unique number for the abbreviation. // Define a unique number for the abbreviation.
// //
void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) { DIEAbbrev &DwarfFile::assignAbbrevNumber(DIE &Die) {
// Check the set for priors. FoldingSetNodeID ID;
DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev); DIEAbbrev Abbrev = Die.generateAbbrev();
Abbrev.Profile(ID);
// If it's newly added. void *InsertPos;
if (InSet == &Abbrev) { if (DIEAbbrev *Existing =
// Add to abbreviation list. AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
Abbreviations.push_back(&Abbrev); Die.setAbbrevNumber(Existing->getNumber());
return *Existing;
// Assign the vector position + 1 as its number.
Abbrev.setNumber(Abbreviations.size());
} else {
// Assign existing abbreviation number.
Abbrev.setNumber(InSet->getNumber());
} }
// Move the abbreviation to the heap and assign a number.
DIEAbbrev *New = new (AbbrevAllocator) DIEAbbrev(std::move(Abbrev));
Abbreviations.push_back(New);
New->setNumber(Abbreviations.size());
Die.setAbbrevNumber(Abbreviations.size());
// Store it for lookup.
AbbreviationsSet.InsertNode(New, InsertPos);
return *New;
} }
void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) { void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
@ -83,10 +89,7 @@ void DwarfFile::computeSizeAndOffsets() {
// CU. It returns the offset after laying out the DIE. // CU. It returns the offset after laying out the DIE.
unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) { unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
// Record the abbreviation. // Record the abbreviation.
assignAbbrevNumber(Die.getAbbrev()); const DIEAbbrev &Abbrev = assignAbbrevNumber(Die);
// Get the abbreviation for this DIE.
const DIEAbbrev &Abbrev = Die.getAbbrev();
// Set DIE offset // Set DIE offset
Die.setOffset(Offset); Die.setOffset(Offset);

View File

@ -37,6 +37,8 @@ class DwarfFile {
// Target of Dwarf emission, used for sizing of abbreviations. // Target of Dwarf emission, used for sizing of abbreviations.
AsmPrinter *Asm; AsmPrinter *Asm;
BumpPtrAllocator AbbrevAllocator;
// Used to uniquely define abbreviations. // Used to uniquely define abbreviations.
FoldingSet<DIEAbbrev> AbbreviationsSet; FoldingSet<DIEAbbrev> AbbreviationsSet;
@ -72,8 +74,11 @@ public:
/// \brief Compute the size and offset of all the DIEs. /// \brief Compute the size and offset of all the DIEs.
void computeSizeAndOffsets(); void computeSizeAndOffsets();
/// \brief Define a unique number for the abbreviation. /// Define a unique number for the abbreviation.
void assignAbbrevNumber(DIEAbbrev &Abbrev); ///
/// Compute the abbreviation for \c Die, look up its unique number, and
/// return a reference to it in the uniquing table.
DIEAbbrev &assignAbbrevNumber(DIE &Die);
/// \brief Add a unit to the list of CUs. /// \brief Add a unit to the list of CUs.
void addUnit(std::unique_ptr<DwarfUnit> U); void addUnit(std::unique_ptr<DwarfUnit> U);

View File

@ -72,8 +72,10 @@ struct PatchLocation {
void set(uint64_t New) const { void set(uint64_t New) const {
assert(Die); assert(Die);
assert(Index < Die->getValues().size()); assert(Index < Die->getValues().size());
assert(Die->getValues()[Index].getType() == DIEValue::isInteger); const auto &Old = Die->getValues()[Index];
Die->setValue(Index, DIEInteger(New)); assert(Old.getType() == DIEValue::isInteger);
Die->setValue(Index,
DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New)));
} }
uint64_t get() const { uint64_t get() const {
@ -1869,7 +1871,13 @@ unsigned DwarfLinker::cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
DIEBlocks.push_back(Block); DIEBlocks.push_back(Block);
} }
Attr = Loc ? static_cast<DIE *>(Loc) : static_cast<DIE *>(Block); Attr = Loc ? static_cast<DIE *>(Loc) : static_cast<DIE *>(Block);
Value = Loc ? DIEValue(Loc) : DIEValue(Block);
if (Loc)
Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
dwarf::Form(AttrSpec.Form), Loc);
else
Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
dwarf::Form(AttrSpec.Form), Block);
ArrayRef<uint8_t> Bytes = *Val.getAsBlock(); ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
for (auto Byte : Bytes) for (auto Byte : Bytes)
Attr->addValue(static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1, Attr->addValue(static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
@ -1883,8 +1891,7 @@ unsigned DwarfLinker::cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
else else
Block->ComputeSize(&Streamer->getAsmPrinter()); Block->ComputeSize(&Streamer->getAsmPrinter());
} }
Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form), Die.addValue(Value);
Value);
return AttrSize; return AttrSize;
} }
@ -2183,14 +2190,15 @@ DIE *DwarfLinker::cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE,
Unit.addTypeAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset); Unit.addTypeAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset);
} }
DIEAbbrev &NewAbbrev = Die->getAbbrev(); DIEAbbrev NewAbbrev = Die->generateAbbrev();
// If a scope DIE is kept, we must have kept at least one child. If // If a scope DIE is kept, we must have kept at least one child. If
// it's not the case, we'll just be emitting one wasteful end of // it's not the case, we'll just be emitting one wasteful end of
// children marker, but things won't break. // children marker, but things won't break.
if (InputDIE.hasChildren()) if (InputDIE.hasChildren())
NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes); NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
// Assign a permanent abbrev number // Assign a permanent abbrev number
AssignAbbrev(Die->getAbbrev()); AssignAbbrev(NewAbbrev);
Die->setAbbrevNumber(NewAbbrev.getNumber());
// Add the size of the abbreviation number to the output offset. // Add the size of the abbreviation number to the output offset.
OutOffset += getULEB128Size(Die->getAbbrevNumber()); OutOffset += getULEB128Size(Die->getAbbrevNumber());
@ -2321,14 +2329,15 @@ void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit,
// Update the cloned DW_AT_stmt_list with the correct debug_line offset. // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
if (auto *OutputDIE = Unit.getOutputUnitDIE()) { if (auto *OutputDIE = Unit.getOutputUnitDIE()) {
const auto &Abbrev = OutputDIE->getAbbrev().getData(); const auto &Values = OutputDIE->getValues();
auto Stmt = std::find_if( auto Stmt =
Abbrev.begin(), Abbrev.end(), [](const DIEAbbrevData &AbbrevData) { std::find_if(Values.begin(), Values.end(), [](const DIEValue &Value) {
return AbbrevData.getAttribute() == dwarf::DW_AT_stmt_list; return Value.getAttribute() == dwarf::DW_AT_stmt_list;
}); });
assert(Stmt < Abbrev.end() && "Didn't find DW_AT_stmt_list in cloned DIE!"); assert(Stmt < Values.end() && "Didn't find DW_AT_stmt_list in cloned DIE!");
OutputDIE->setValue(Stmt - Abbrev.begin(), OutputDIE->setValue(Stmt - Values.begin(),
DIEInteger(Streamer->getLineSectionSize())); DIEValue(Stmt->getAttribute(), Stmt->getForm(),
DIEInteger(Streamer->getLineSectionSize())));
} }
// Parse the original line info for the unit. // Parse the original line info for the unit.