Skip to content

Commit 0486684

Browse files
committed
Address review comments, split out value-name mapping to separate class, remove line tracking
1 parent 026838e commit 0486684

File tree

3 files changed

+62
-78
lines changed

3 files changed

+62
-78
lines changed

llvm/include/llvm/Transforms/Utils/DebugSSAUpdater.h

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,30 @@
1313
// the state of debug variables, such as measuring the change in values of a
1414
// variable over time, or calculating coverage stats.
1515
//
16+
// NB: This is an expensive analysis that is generally not suitable for use in
17+
// LLVM passes, but may be useful for standalone tools.
18+
//
1619
//===----------------------------------------------------------------------===//
1720

1821
#ifndef LLVM_TRANSFORMS_UTILS_DEBUGSSAUPDATER_H
1922
#define LLVM_TRANSFORMS_UTILS_DEBUGSSAUPDATER_H
2023

24+
#include "llvm/ADT/SmallVector.h"
2125
#include "llvm/IR/BasicBlock.h"
2226
#include "llvm/IR/CFG.h"
2327
#include "llvm/IR/DebugInfoMetadata.h"
2428
#include "llvm/IR/DebugProgramInstruction.h"
2529
#include "llvm/IR/Instruction.h"
30+
#include "llvm/IR/ValueHandle.h"
31+
#include "llvm/IR/ValueMap.h"
32+
#include <cstdint>
2633

2734
namespace llvm {
2835

2936
////////////////////////////////////////
3037
// SSAUpdater specialization classes
3138

3239
class DbgSSAPhi;
33-
template <typename T> class SmallVectorImpl;
3440
template <typename T> class SSAUpdaterTraits;
3541

3642
/// A definition of a variable; can represent either a debug value, no
@@ -208,12 +214,15 @@ class DbgSSABlock {
208214
/// SSA construction (via the SSAUpdaterImpl class), used for analysis purposes.
209215
class DebugSSAUpdater {
210216
friend class SSAUpdaterTraits<DebugSSAUpdater>;
217+
using AvailableValsTy = DenseMap<DbgSSABlock *, DbgValueDef>;
211218

212219
private:
213220
/// This keeps track of which value to use on a per-block basis. When we
214221
/// insert PHI nodes, we keep track of them here.
215-
void *AV = nullptr;
222+
AvailableValsTy AV;
216223

224+
/// Pointer to an optionally-passed vector into which, if it is non-null,
225+
/// the PHIs that describe ambiguous variable locations will be inserted.
217226
SmallVectorImpl<DbgSSAPhi *> *InsertedPHIs;
218227

219228
DenseMap<BasicBlock *, DbgSSABlock *> BlockMap;
@@ -225,7 +234,6 @@ class DebugSSAUpdater {
225234
SmallVectorImpl<DbgSSAPhi *> *InsertedPHIs = nullptr);
226235
DebugSSAUpdater(const DebugSSAUpdater &) = delete;
227236
DebugSSAUpdater &operator=(const DebugSSAUpdater &) = delete;
228-
~DebugSSAUpdater();
229237

230238
void reset() {
231239
for (auto &Block : BlockMap)
@@ -298,21 +306,35 @@ struct DbgRangeEntry {
298306
DbgValueDef Value;
299307
};
300308

309+
/// Utility class used to store the names of SSA values after their owning
310+
/// modules have been destroyed. Values are added via \c addValue to receive a
311+
/// corresponding ID, which can then be used to retrieve the name of the SSA
312+
/// value via \c getName at any point. Adding the same value multiple times
313+
/// returns the same ID, making \c addValue idempotent.
314+
class SSAValueNameMap {
315+
struct Config : ValueMapConfig<Value *> {
316+
enum { FollowRAUW = false };
317+
};
318+
public:
319+
using ValueID = uint64_t;
320+
ValueID addValue(Value *V);
321+
std::string getName(ValueID ID) {
322+
return ValueIDToNameMap[ID];
323+
}
324+
private:
325+
DenseMap<ValueID, std::string> ValueIDToNameMap;
326+
ValueMap<Value*, ValueID, Config> ValueToIDMap;
327+
ValueID NextID = 0;
328+
};
329+
330+
/// Utility class used to find and store the live debug ranges for variables in
331+
/// a module. This class uses the DebugSSAUpdater for each variable added with
332+
/// \c addVariable to find either a single-location value, e.g. #dbg_declare, or
333+
/// a set of live value ranges corresponding to the set of #dbg_value records.
301334
class DbgValueRangeTable {
302335
DenseMap<DebugVariableAggregate, SmallVector<DbgRangeEntry>>
303336
OrigVariableValueRangeTable;
304337
DenseMap<DebugVariableAggregate, DbgValueDef> OrigSingleLocVariableValueTable;
305-
// For the only initial user of this class, the mappings below are useful and
306-
// are used in conjunction with the variable value ranges above, thus we track
307-
// them as part of the same class. If we have more uses for variable value
308-
// range tracking, then the line/variable name mapping should be moved out to
309-
// a separate class.
310-
DenseMap<BasicBlock::iterator, uint64_t> LineMapping;
311-
DenseMap<uint64_t, std::string> VariableNameMapping;
312-
313-
using VarMapping = DenseMap<Value *, uint64_t>;
314-
VarMapping VariableMapping;
315-
uint64_t KeyIndex = 0;
316338

317339
public:
318340
void addVariable(Function *F, DebugVariableAggregate DVA);
@@ -330,19 +352,6 @@ class DbgValueRangeTable {
330352
return OrigSingleLocVariableValueTable[DVA];
331353
}
332354

333-
void addLine(BasicBlock::iterator I, uint64_t LineAddr) {
334-
LineMapping[I] = LineAddr;
335-
}
336-
uint64_t getLine(BasicBlock::iterator I) {
337-
return LineMapping.contains(I) ? LineMapping[I] : (uint64_t)-1;
338-
}
339-
340-
uint64_t addVariableName(Value *V, uint64_t Size);
341-
std::string getVariableName(uint64_t Key) {
342-
assert(VariableNameMapping.contains(Key) && "Why not here?");
343-
return VariableNameMapping[Key];
344-
}
345-
346355
void printValues(DebugVariableAggregate DVA, raw_ostream &OS);
347356
};
348357

llvm/lib/Transforms/Utils/DebugSSAUpdater.cpp

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,23 @@ void DbgSSAPhi::print(raw_ostream &OS) const {
4242

4343
using AvailableValsTy = DenseMap<DbgSSABlock *, DbgValueDef>;
4444

45-
static AvailableValsTy &getAvailableVals(void *AV) {
46-
return *static_cast<AvailableValsTy *>(AV);
47-
}
48-
4945
DebugSSAUpdater::DebugSSAUpdater(SmallVectorImpl<DbgSSAPhi *> *NewPHI)
5046
: InsertedPHIs(NewPHI) {}
5147

52-
DebugSSAUpdater::~DebugSSAUpdater() {
53-
delete static_cast<AvailableValsTy *>(AV);
54-
}
55-
5648
void DebugSSAUpdater::initialize() {
57-
if (!AV)
58-
AV = new AvailableValsTy();
59-
else
60-
getAvailableVals(AV).clear();
49+
AV.clear();
6150
}
6251

6352
bool DebugSSAUpdater::hasValueForBlock(DbgSSABlock *BB) const {
64-
return getAvailableVals(AV).count(BB);
53+
return AV.count(BB);
6554
}
6655

6756
DbgValueDef DebugSSAUpdater::findValueForBlock(DbgSSABlock *BB) const {
68-
return getAvailableVals(AV).lookup(BB);
57+
return AV.lookup(BB);
6958
}
7059

7160
void DebugSSAUpdater::addAvailableValue(DbgSSABlock *BB, DbgValueDef DV) {
72-
getAvailableVals(AV)[BB] = DV;
61+
AV[BB] = DV;
7362
}
7463

7564
DbgValueDef DebugSSAUpdater::getValueAtEndOfBlock(DbgSSABlock *BB) {
@@ -171,8 +160,8 @@ template <> class SSAUpdaterTraits<DebugSSAUpdater> {
171160
static PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
172161
static PHI_iterator PHI_end(PhiT *PHI) { return PHI_iterator(PHI, true); }
173162

174-
/// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds
175-
/// vector, set Info->NumPreds, and allocate space in Info->Preds.
163+
/// FindPredecessorBlocks - Put the predecessors of BB into the Preds
164+
/// vector.
176165
static void FindPredecessorBlocks(DbgSSABlock *BB,
177166
SmallVectorImpl<DbgSSABlock *> *Preds) {
178167
for (auto PredIt = BB->pred_begin(); PredIt != BB->pred_end(); ++PredIt)
@@ -185,8 +174,7 @@ template <> class SSAUpdaterTraits<DebugSSAUpdater> {
185174
return DbgValueDef();
186175
}
187176

188-
/// CreateEmptyPHI - Create a new PHI instruction in the specified block.
189-
/// Reserve space for the operands (?) but do not fill them in yet.
177+
/// CreateEmptyPHI - Create a new debug PHI entry for the specified block.
190178
static DbgSSAPhi *CreateEmptyPHI(DbgSSABlock *BB, unsigned NumPreds,
191179
DebugSSAUpdater *Updater) {
192180
DbgSSAPhi *PHI = BB->newPHI();
@@ -225,11 +213,10 @@ template <> class SSAUpdaterTraits<DebugSSAUpdater> {
225213
/// return it. If not, construct SSA form by first calculating the required
226214
/// placement of PHIs and then inserting new PHIs where needed.
227215
DbgValueDef DebugSSAUpdater::getValueAtEndOfBlockInternal(DbgSSABlock *BB) {
228-
AvailableValsTy &AvailableVals = getAvailableVals(AV);
229-
if (AvailableVals.contains(BB))
230-
return AvailableVals[BB];
216+
if (AV.contains(BB))
217+
return AV[BB];
231218

232-
SSAUpdaterImpl<DebugSSAUpdater> Impl(this, &AvailableVals, InsertedPHIs);
219+
SSAUpdaterImpl<DebugSSAUpdater> Impl(this, &AV, InsertedPHIs);
233220
return Impl.GetValue(BB);
234221
}
235222

@@ -263,21 +250,13 @@ void DbgValueRangeTable::addVariable(Function *F, DebugVariableAggregate DVA) {
263250
for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
264251
if (DVR.getVariable() == Var &&
265252
DVR.getDebugLoc().getInlinedAt() == InlinedAt) {
266-
assert(!DVR.isDbgAssign() && "No support for #dbg_declare yet.");
253+
assert(!DVR.isDbgAssign() && "No support for #dbg_assign yet.");
267254
if (DVR.isDbgDeclare())
268255
DeclareRecordFound = true;
269256
++NumRecordsFound;
270257
LastRecordFound = &DVR;
271258
DbgRecordValues.push_back(&DVR);
272259
}
273-
if (!FoundInstructionInScope && I.getDebugLoc()) {
274-
if (I.getDebugLoc().getInlinedAt() == InlinedAt &&
275-
isContained(cast<DILocalScope>(I.getDebugLoc().getScope()),
276-
Var->getScope())) {
277-
FoundInstructionInScope = true;
278-
HasAnyInstructionsInScope.insert(&BB);
279-
}
280-
}
281260
}
282261
if (!FoundInstructionInScope && I.getDebugLoc()) {
283262
if (I.getDebugLoc().getInlinedAt() == InlinedAt &&
@@ -312,7 +291,8 @@ void DbgValueRangeTable::addVariable(Function *F, DebugVariableAggregate DVA) {
312291
return;
313292
}
314293

315-
// We don't have a single location, so let's have fun with liveness.
294+
// We don't have a single location for the variable's entire scope, so instead
295+
// we must now perform a liveness analysis to create a location list.
316296
DenseMap<BasicBlock *, DbgValueDef> LiveInMap;
317297
SmallVector<DbgSSAPhi *> HypotheticalPHIs;
318298
DebugSSAUpdater SSAUpdater(&HypotheticalPHIs);
@@ -395,21 +375,17 @@ void DbgValueRangeTable::printValues(DebugVariableAggregate DVA,
395375
}
396376
}
397377

398-
uint64_t DbgValueRangeTable::addVariableName(Value *V, uint64_t Size) {
399-
uint64_t Key = 0;
400-
auto I = VariableMapping.find(V);
401-
if (I == VariableMapping.end()) {
402-
Key = KeyIndex;
403-
VariableMapping.try_emplace(V, Key);
404-
std::string &ValueText = VariableNameMapping[Key];
405-
raw_string_ostream Stream(ValueText);
406-
Stream << " ";
407-
V->printAsOperand(Stream, true);
408-
KeyIndex += Size;
409-
} else {
410-
Key = I->second;
411-
}
412-
LLVM_DEBUG(dbgs() << "Stashing Value: " << Key << " - " << *V << "\n");
413-
414-
return Key;
378+
SSAValueNameMap::ValueID SSAValueNameMap::addValue(Value *V) {
379+
auto ExistingID = ValueToIDMap.find(V);
380+
if (ExistingID != ValueToIDMap.end())
381+
return ExistingID->second;
382+
// First, get a new ID and Map V to it.
383+
ValueID NewID = NextID++;
384+
ValueToIDMap.insert({V, NewID});
385+
// Then, get the name string for V and map NewID to it.
386+
assert(!ValueIDToNameMap.contains(NewID) && "New value ID already maps to a name?");
387+
std::string &ValueText = ValueIDToNameMap[NewID];
388+
raw_string_ostream Stream(ValueText);
389+
V->printAsOperand(Stream, true);
390+
return NewID;
415391
}

llvm/unittests/Transforms/Utils/DebugSSAUpdaterTest.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "llvm/IR/Function.h"
1616
#include "llvm/IR/GlobalValue.h"
1717
#include "llvm/IR/Module.h"
18-
#include "llvm/Support/Debug.h"
1918
#include "llvm/Support/SourceMgr.h"
2019
#include "gtest/gtest.h"
2120

0 commit comments

Comments
 (0)