Skip to content

Commit

Permalink
AsmPrinter: Store abbreviation data directly in DIE and DIEValue
Browse files Browse the repository at this point in the history
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.)

llvm-svn: 238364
  • Loading branch information
dexonsmith committed May 27, 2015
1 parent 3154aa2 commit 815a6eb
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 99 deletions.
60 changes: 40 additions & 20 deletions llvm/include/llvm/CodeGen/DIE.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ class DIEValue {
private:
/// 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.
///
Expand Down Expand Up @@ -387,27 +389,37 @@ class DIEValue {
}

public:
DIEValue() : Ty(isNone) {}
DIEValue(const DIEValue &X) : Ty(X.Ty) { copyVal(X); }
DIEValue() = default;
DIEValue(const DIEValue &X) : Ty(X.Ty), Attribute(X.Attribute), Form(X.Form) {
copyVal(X);
}
DIEValue &operator=(const DIEValue &X) {
destroyVal();
Ty = X.Ty;
Attribute = X.Attribute;
Form = X.Form;
copyVal(X);
return *this;
}
~DIEValue() { destroyVal(); }

#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) \
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"); \
construct<const DIE##T *>(V); \
}
#include "llvm/CodeGen/DIEValue.def"

// Accessors
Type getType() const { return Ty; }
dwarf::Attribute getAttribute() const { return Attribute; }
dwarf::Form getForm() const { return Form; }
explicit operator bool() const { return Ty; }

#define HANDLE_DIEVALUE_SMALL(T) \
Expand Down Expand Up @@ -449,9 +461,11 @@ class DIE {
///
unsigned Size;

/// Abbrev - Buffer for constructing abbreviation.
unsigned AbbrevNumber = ~0u;

/// Tag - Dwarf tag code.
///
DIEAbbrev Abbrev;
dwarf::Tag Tag = (dwarf::Tag)0;

/// Children DIEs.
///
Expand All @@ -470,22 +484,18 @@ class DIE {
SmallVector<DIEValue, 12> Values;

protected:
DIE()
: Offset(0), Size(0), Abbrev((dwarf::Tag)0, dwarf::DW_CHILDREN_no),
Parent(nullptr) {}
DIE() : Offset(0), Size(0), Parent(nullptr) {}

public:
explicit DIE(dwarf::Tag Tag)
: Offset(0), Size(0), Abbrev((dwarf::Tag)Tag, dwarf::DW_CHILDREN_no),
Parent(nullptr) {}
: Offset(0), Size(0), Tag(Tag), Parent(nullptr) {}

// Accessors.
DIEAbbrev &getAbbrev() { return Abbrev; }
const DIEAbbrev &getAbbrev() const { return Abbrev; }
unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
dwarf::Tag getTag() const { return Abbrev.getTag(); }
unsigned getAbbrevNumber() const { return AbbrevNumber; }
dwarf::Tag getTag() const { return Tag; }
unsigned getOffset() const { return Offset; }
unsigned getSize() const { return Size; }
bool hasChildren() const { return !Children.empty(); }
const std::vector<std::unique_ptr<DIE>> &getChildren() const {
return Children;
}
Expand All @@ -495,6 +505,16 @@ class DIE {
Values[I] = New;
}
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
/// belongs to.
const DIE *getUnit() const;
Expand All @@ -506,16 +526,16 @@ class DIE {

/// addValue - Add a value and attributes to a DIE.
///
void addValue(dwarf::Attribute Attribute, dwarf::Form Form, DIEValue Value) {
Abbrev.AddAttribute(Attribute, Form);
Values.push_back(Value);
void addValue(DIEValue Value) { Values.push_back(Value); }
template <class T>
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.
///
void addChild(std::unique_ptr<DIE> Child) {
assert(!Child->getParent());
Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
Child->Parent = this;
Children.push_back(std::move(Child));
}
Expand Down
20 changes: 8 additions & 12 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,20 @@ void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) 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.
if (isVerbose())
OutStreamer->AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
"] 0x" + Twine::utohexstr(Die.getOffset()) +
":0x" + Twine::utohexstr(Die.getSize()) + " " +
dwarf::TagString(Abbrev.getTag()));
EmitULEB128(Abbrev.getNumber());
OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
Twine::utohexstr(Die.getOffset()) + ":0x" +
Twine::utohexstr(Die.getSize()) + " " +
dwarf::TagString(Die.getTag()));
EmitULEB128(Die.getAbbrevNumber());

const SmallVectorImpl<DIEValue> &Values = Die.getValues();
const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();

// Emit the DIE attribute values.
for (unsigned i = 0, N = Values.size(); i < N; ++i) {
dwarf::Attribute Attr = AbbrevData[i].getAttribute();
dwarf::Form Form = AbbrevData[i].getForm();
dwarf::Attribute Attr = Values[i].getAttribute();
dwarf::Form Form = Values[i].getForm();
assert(Form && "Too many attributes for DIE (check abbreviation)");

if (isVerbose()) {
Expand All @@ -286,7 +282,7 @@ void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
}

// Emit the DIE children if any.
if (Abbrev.hasChildren()) {
if (Die.hasChildren()) {
for (auto &Child : Die.getChildren())
emitDwarfDIE(*Child);

Expand Down
36 changes: 18 additions & 18 deletions llvm/lib/CodeGen/AsmPrinter/DIE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ void DIEAbbrev::print(raw_ostream &O) {
void DIEAbbrev::dump() { print(dbgs()); }
#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
/// belongs.
const DIE *DIE::getUnit() const {
Expand All @@ -130,20 +137,19 @@ const DIE *DIE::getUnitOrNull() const {

DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
const SmallVectorImpl<DIEValue> &Values = getValues();
const DIEAbbrev &Abbrevs = getAbbrev();

// Iterate through all the attributes until we find the one we're
// looking for, if we can't find it return NULL.
for (size_t i = 0; i < Values.size(); ++i)
if (Abbrevs.getData()[i].getAttribute() == Attribute)
if (Values[i].getAttribute() == Attribute)
return Values[i];
return DIEValue();
}

#ifndef NDEBUG
void DIE::print(raw_ostream &O, unsigned IndentCount) const {
const std::string Indent(IndentCount, ' ');
bool isBlock = Abbrev.getTag() == 0;
bool isBlock = getTag() == 0;

if (!isBlock) {
O << Indent
Expand All @@ -153,26 +159,24 @@ void DIE::print(raw_ostream &O, unsigned IndentCount) const {
<< ", Size: " << Size << "\n";

O << Indent
<< dwarf::TagString(Abbrev.getTag())
<< dwarf::TagString(getTag())
<< " "
<< dwarf::ChildrenString(Abbrev.hasChildren()) << "\n";
<< dwarf::ChildrenString(hasChildren()) << "\n";
} else {
O << "Size: " << Size << "\n";
}

const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();

IndentCount += 2;
for (unsigned i = 0, N = Data.size(); i < N; ++i) {
for (unsigned i = 0, N = Values.size(); i < N; ++i) {
O << Indent;

if (!isBlock)
O << dwarf::AttributeString(Data[i].getAttribute());
O << dwarf::AttributeString(Values[i].getAttribute());
else
O << "Blk[" << i << "]";

O << " "
<< dwarf::FormEncodingString(Data[i].getForm())
<< dwarf::FormEncodingString(Values[i].getForm())
<< " ";
Values[i].print(O);
O << "\n";
Expand Down Expand Up @@ -505,9 +509,8 @@ void DIETypeSignature::print(raw_ostream &O) const {
///
unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
if (!Size) {
const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
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;
Expand All @@ -526,9 +529,8 @@ void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
Asm->EmitULEB128(Size); break;
}

const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
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.
Expand Down Expand Up @@ -560,9 +562,8 @@ void DIELoc::print(raw_ostream &O) const {
///
unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
if (!Size) {
const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
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;
Expand All @@ -579,9 +580,8 @@ void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
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)
Values[i].EmitValue(Asm, AbbrevData[i].getForm());
Values[i].EmitValue(Asm, Values[i].getForm());
}

/// SizeOf - Determine size of block data in bytes.
Expand Down
14 changes: 5 additions & 9 deletions llvm/lib/CodeGen/AsmPrinter/DIEHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ using namespace llvm;
/// a reference to it.
static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
const auto &Values = Die.getValues();
const DIEAbbrev &Abbrevs = Die.getAbbrev();

// Iterate through all the attributes until we find the one we're
// looking for, if we can't find it return an empty string.
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 StringRef("");
Expand Down Expand Up @@ -120,19 +119,17 @@ void DIEHash::addParentContext(const DIE &Parent) {
// Collect all of the attributes for a particular DIE in single structure.
void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
const SmallVectorImpl<DIEValue> &Values = Die.getValues();
const DIEAbbrev &Abbrevs = Die.getAbbrev();

#define COLLECT_ATTR(NAME) \
case dwarf::NAME: \
Attrs.NAME.Val = Values[i]; \
Attrs.NAME.Desc = &Abbrevs.getData()[i]; \
break

for (size_t i = 0, e = Values.size(); i != e; ++i) {
DEBUG(dbgs() << "Attribute: "
<< dwarf::AttributeString(Abbrevs.getData()[i].getAttribute())
<< dwarf::AttributeString(Values[i].getAttribute())
<< " added.\n");
switch (Abbrevs.getData()[i].getAttribute()) {
switch (Values[i].getAttribute()) {
COLLECT_ATTR(DW_AT_name);
COLLECT_ATTR(DW_AT_accessibility);
COLLECT_ATTR(DW_AT_address_class);
Expand Down Expand Up @@ -288,8 +285,7 @@ void DIEHash::hashLocList(const DIELocList &LocList) {
// the form.
void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
const DIEValue &Value = Attr.Val;
const DIEAbbrevData *Desc = Attr.Desc;
dwarf::Attribute Attribute = Desc->getAttribute();
dwarf::Attribute Attribute = Value.getAttribute();

// 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
Expand All @@ -311,7 +307,7 @@ void DIEHash::hashAttribute(AttrEntry Attr, dwarf::Tag Tag) {
case DIEValue::isInteger: {
addULEB128('A');
addULEB128(Attribute);
switch (Desc->getForm()) {
switch (Value.getForm()) {
case dwarf::DW_FORM_data1:
case dwarf::DW_FORM_data2:
case dwarf::DW_FORM_data4:
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/AsmPrinter/DIEHash.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class CompileUnit;
class DIEHash {

// The entry for a particular attribute.
//
// FIXME: Remove this struct, it's pretty boring now.
struct AttrEntry {
DIEValue Val;
const DIEAbbrevData *Desc;
};

// Collection of all attributes used in hashing a particular DIE.
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
if (Label)
DD->addArangeLabel(SymbolCU(this, Label));

Die.addValue(Attribute, dwarf::DW_FORM_addr,
Label ? DIEValue(DIELabel(Label)) : DIEValue(DIEInteger(0)));
if (Label)
Die.addValue(Attribute, dwarf::DW_FORM_addr, DIELabel(Label));
else
Die.addValue(Attribute, dwarf::DW_FORM_addr, DIEInteger(0));
}

unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName,
Expand Down Expand Up @@ -253,9 +255,7 @@ void DwarfCompileUnit::initStmtList() {
}

void DwarfCompileUnit::applyStmtList(DIE &D) {
D.addValue(dwarf::DW_AT_stmt_list,
UnitDie.getAbbrev().getData()[stmtListIndex].getForm(),
UnitDie.getValues()[stmtListIndex]);
D.addValue(UnitDie.getValues()[stmtListIndex]);
}

void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
Expand Down
Loading

0 comments on commit 815a6eb

Please sign in to comment.