Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit d0da5af

Browse files
committed
Use value semantics to manage DbgVariables rather than dynamic allocation/pointers.
Requires switching some vectors to lists to maintain pointer validity. These could be changed to forward_lists (singly linked) with a bit more work - I've left comments to that effect. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206780 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c5b286b commit d0da5af

File tree

2 files changed

+57
-54
lines changed

2 files changed

+57
-54
lines changed

lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -563,14 +563,13 @@ DIE *DwarfDebug::createScopeChildrenDIE(DwarfCompileUnit *TheCU,
563563

564564
// Collect arguments for current function.
565565
if (LScopes.isCurrentFunctionScope(Scope)) {
566-
for (DbgVariable *ArgDV : CurrentFnArguments)
567-
if (ArgDV)
568-
if (DIE *Arg =
569-
TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
570-
Children.push_back(Arg);
571-
if (ArgDV->isObjectPointer())
572-
ObjectPointer = Arg;
573-
}
566+
for (DbgVariable &ArgDV : CurrentFnArguments)
567+
if (ArgDV.getVariable()) {
568+
DIE *Arg = TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope());
569+
Children.push_back(Arg);
570+
if (ArgDV.isObjectPointer())
571+
ObjectPointer = Arg;
572+
}
574573

575574
// If this is a variadic function, add an unspecified parameter.
576575
DISubprogram SP(Scope->getScopeNode());
@@ -583,11 +582,11 @@ DIE *DwarfDebug::createScopeChildrenDIE(DwarfCompileUnit *TheCU,
583582
}
584583

585584
// Collect lexical scope children first.
586-
for (DbgVariable *DV : ScopeVariables.lookup(Scope))
587-
if (DIE *Variable = TheCU->constructVariableDIE(*DV,
585+
for (DbgVariable &DV : ScopeVariables.lookup(Scope))
586+
if (DIE *Variable = TheCU->constructVariableDIE(DV,
588587
Scope->isAbstractScope())) {
589588
Children.push_back(Variable);
590-
if (DV->isObjectPointer())
589+
if (DV.isObjectPointer())
591590
ObjectPointer = Variable;
592591
}
593592
for (LexicalScope *LS : Scope->getChildren())
@@ -1120,32 +1119,33 @@ DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
11201119
if (!Scope)
11211120
return NULL;
11221121

1123-
AbsDbgVariable = new DbgVariable(Var, NULL, this);
1124-
addScopeVariable(Scope, AbsDbgVariable);
1122+
AbsDbgVariable = &addScopeVariable(Scope, DbgVariable(Var, NULL, this));
11251123
AbstractVariables[Var] = AbsDbgVariable;
11261124
return AbsDbgVariable;
11271125
}
11281126

11291127
// If Var is a current function argument then add it to CurrentFnArguments list.
1130-
bool DwarfDebug::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) {
1128+
DbgVariable *DwarfDebug::addCurrentFnArgument(DbgVariable &Var, LexicalScope *Scope) {
11311129
if (!LScopes.isCurrentFunctionScope(Scope))
1132-
return false;
1133-
DIVariable DV = Var->getVariable();
1130+
return nullptr;
1131+
DIVariable DV = Var.getVariable();
11341132
if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1135-
return false;
1133+
return nullptr;
11361134
unsigned ArgNo = DV.getArgNumber();
11371135
if (ArgNo == 0)
1138-
return false;
1136+
return nullptr;
11391137

1140-
size_t Size = CurrentFnArguments.size();
1141-
if (Size == 0)
1142-
CurrentFnArguments.resize(CurFn->getFunction()->arg_size());
1143-
// llvm::Function argument size is not good indicator of how many
1144-
// arguments does the function have at source level.
1145-
if (ArgNo > Size)
1146-
CurrentFnArguments.resize(ArgNo * 2);
1147-
CurrentFnArguments[ArgNo - 1] = Var;
1148-
return true;
1138+
auto I = CurrentFnArguments.begin();
1139+
for (; I != CurrentFnArguments.end(); ++I)
1140+
if (ArgNo < I->getVariable().getArgNumber())
1141+
break;
1142+
return &*CurrentFnArguments.insert(I, std::move(Var));
1143+
}
1144+
1145+
DbgVariable &DwarfDebug::addVariable(DbgVariable Var, LexicalScope *Scope) {
1146+
if (DbgVariable *Res = addCurrentFnArgument(Var, Scope))
1147+
return *Res;
1148+
return addScopeVariable(Scope, std::move(Var));
11491149
}
11501150

11511151
// Collect variable information from side table maintained by MMI.
@@ -1163,10 +1163,9 @@ void DwarfDebug::collectVariableInfoFromMMITable(
11631163
continue;
11641164

11651165
DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VI.Loc);
1166-
DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
1167-
RegVar->setFrameIndex(VI.Slot);
1168-
if (!addCurrentFnArgument(RegVar, Scope))
1169-
addScopeVariable(Scope, RegVar);
1166+
DbgVariable RegVar(DV, AbsDbgVariable, this);
1167+
RegVar.setFrameIndex(VI.Slot);
1168+
addVariable(std::move(RegVar), Scope);
11701169
if (AbsDbgVariable)
11711170
AbsDbgVariable->setFrameIndex(VI.Slot);
11721171
}
@@ -1247,21 +1246,19 @@ DwarfDebug::collectVariableInfo(SmallPtrSet<const MDNode *, 16> &Processed) {
12471246
Processed.insert(DV);
12481247
assert(MInsn->isDebugValue() && "History must begin with debug value");
12491248
DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
1250-
DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
1251-
if (!addCurrentFnArgument(RegVar, Scope))
1252-
addScopeVariable(Scope, RegVar);
1249+
DbgVariable &RegVar = addVariable(DbgVariable(DV, AbsVar, this), Scope);
12531250
if (AbsVar)
12541251
AbsVar->setMInsn(MInsn);
12551252

12561253
// Simplify ranges that are fully coalesced.
12571254
if (History.size() <= 1 ||
12581255
(History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
1259-
RegVar->setMInsn(MInsn);
1256+
RegVar.setMInsn(MInsn);
12601257
continue;
12611258
}
12621259

12631260
// Handle multiple DBG_VALUE instructions describing one variable.
1264-
RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
1261+
RegVar.setDotDebugLocOffset(DotDebugLocEntries.size());
12651262

12661263
DotDebugLocEntries.resize(DotDebugLocEntries.size() + 1);
12671264
DebugLocList &LocList = DotDebugLocEntries.back();
@@ -1319,7 +1316,7 @@ DwarfDebug::collectVariableInfo(SmallPtrSet<const MDNode *, 16> &Processed) {
13191316
if (!DV || !DV.isVariable() || !Processed.insert(DV))
13201317
continue;
13211318
if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
1322-
addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
1319+
addScopeVariable(Scope, DbgVariable(DV, NULL, this));
13231320
}
13241321
}
13251322

@@ -1626,9 +1623,9 @@ void DwarfDebug::beginFunction(const MachineFunction *MF) {
16261623
}
16271624
}
16281625

1629-
void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
1630-
SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1631-
DIVariable DV = Var->getVariable();
1626+
DbgVariable &DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable Var) {
1627+
auto &Vars = ScopeVariables[LS];
1628+
DIVariable DV = Var.getVariable();
16321629
// Variables with positive arg numbers are parameters.
16331630
if (unsigned ArgNum = DV.getArgNumber()) {
16341631
// Keep all parameters in order at the start of the variable list to ensure
@@ -1638,9 +1635,9 @@ void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
16381635
// builds have the right order to begin with), searching from the back (this
16391636
// would catch the unoptimized case quickly), or doing a binary search
16401637
// rather than linear search.
1641-
SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1638+
auto I = Vars.begin();
16421639
while (I != Vars.end()) {
1643-
unsigned CurNum = (*I)->getVariable().getArgNumber();
1640+
unsigned CurNum = I->getVariable().getArgNumber();
16441641
// A local (non-parameter) variable has been found, insert immediately
16451642
// before it.
16461643
if (CurNum == 0)
@@ -1650,11 +1647,11 @@ void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
16501647
break;
16511648
++I;
16521649
}
1653-
Vars.insert(I, Var);
1654-
return;
1650+
return *Vars.insert(I, std::move(Var));
16551651
}
16561652

1657-
Vars.push_back(Var);
1653+
Vars.push_back(std::move(Var));
1654+
return Vars.back();
16581655
}
16591656

16601657
// Gather and emit post-function debug information.
@@ -1710,7 +1707,7 @@ void DwarfDebug::endFunction(const MachineFunction *MF) {
17101707
if (AbstractVariables.lookup(CleanDV))
17111708
continue;
17121709
if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
1713-
addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
1710+
addScopeVariable(Scope, DbgVariable(DV, NULL, this));
17141711
}
17151712
}
17161713
if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
@@ -1728,10 +1725,8 @@ void DwarfDebug::endFunction(const MachineFunction *MF) {
17281725
PrevCU = TheCU;
17291726

17301727
// Clear debug info
1731-
for (auto &I : ScopeVariables)
1732-
DeleteContainerPointers(I.second);
17331728
ScopeVariables.clear();
1734-
DeleteContainerPointers(CurrentFnArguments);
1729+
CurrentFnArguments.clear();
17351730
UserVariables.clear();
17361731
DbgValues.clear();
17371732
AbstractVariables.clear();
@@ -2470,7 +2465,7 @@ void DwarfDebug::emitDebugARanges() {
24702465

24712466
// Build a set of address spans, sorted by CU.
24722467
for (const MCSection *Section : Sections) {
2473-
SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2468+
auto &List = SectionMap[Section];
24742469
if (List.size() < 2)
24752470
continue;
24762471

lib/CodeGen/AsmPrinter/DwarfDebug.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "llvm/MC/MCDwarf.h"
3131
#include "llvm/Support/Allocator.h"
3232

33+
#include <list>
34+
3335
namespace llvm {
3436

3537
class AsmPrinter;
@@ -274,15 +276,19 @@ class DwarfDebug : public AsmPrinterHandler {
274276
SectionMapType SectionMap;
275277

276278
// List of arguments for current function.
277-
SmallVector<DbgVariable *, 8> CurrentFnArguments;
279+
// Linked list use to maintain pointer validity. Singly linked list could
280+
// suffice with some contortions to addCurrentFnArgument.
281+
std::list<DbgVariable> CurrentFnArguments;
278282

279283
LexicalScopes LScopes;
280284

281285
// Collection of abstract subprogram DIEs.
282286
DenseMap<const MDNode *, DIE *> AbstractSPDies;
283287

284288
// Collection of dbg variables of a scope.
285-
typedef DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >
289+
// Linked list use to maintain pointer validity. Singly linked list could
290+
// suffice with some contortions to addScopeVariable.
291+
typedef DenseMap<LexicalScope *, std::list<DbgVariable>>
286292
ScopeVariablesMap;
287293
ScopeVariablesMap ScopeVariables;
288294

@@ -413,7 +419,7 @@ class DwarfDebug : public AsmPrinterHandler {
413419

414420
MCDwarfDwoLineTable *getDwoLineTable(const DwarfCompileUnit &);
415421

416-
void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
422+
DbgVariable &addScopeVariable(LexicalScope *LS, DbgVariable Var);
417423

418424
const SmallVectorImpl<DwarfUnit *> &getUnits() {
419425
return InfoHolder.getUnits();
@@ -591,7 +597,9 @@ class DwarfDebug : public AsmPrinterHandler {
591597

592598
/// \brief If Var is an current function argument that add it in
593599
/// CurrentFnArguments list.
594-
bool addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope);
600+
DbgVariable *addCurrentFnArgument(DbgVariable &Var, LexicalScope *Scope);
601+
602+
DbgVariable &addVariable(DbgVariable Var, LexicalScope *Scope);
595603

596604
/// \brief Populate LexicalScope entries with variables' info.
597605
void collectVariableInfo(SmallPtrSet<const MDNode *, 16> &ProcessedVars);

0 commit comments

Comments
 (0)