Skip to content

Commit 9899cf0

Browse files
committed
[DebugInfo][OPT] NFC follow-up on "Fixing a couple of DI duplication bugs of CloneModule"
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@330070 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 883750c commit 9899cf0

File tree

4 files changed

+55
-89
lines changed

4 files changed

+55
-89
lines changed

include/llvm/IR/DebugInfo.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class DbgDeclareInst;
2828
class DbgValueInst;
2929
class Module;
3030

31-
/// \brief Find subprogram that is enclosing this scope.
31+
/// Find subprogram that is enclosing this scope.
3232
DISubprogram *getDISubprogram(const MDNode *Scope);
3333

34-
/// \brief Strip debug info in the module if it exists.
34+
/// Strip debug info in the module if it exists.
3535
///
3636
/// To do this, we remove all calls to the debugger intrinsics and any named
3737
/// metadata for debugging. We also remove debug locations for instructions.
@@ -51,10 +51,10 @@ bool stripDebugInfo(Function &F);
5151
/// All debug type metadata nodes are unreachable and garbage collected.
5252
bool stripNonLineTableDebugInfo(Module &M);
5353

54-
/// \brief Return Debug Info Metadata Version by checking module flags.
54+
/// Return Debug Info Metadata Version by checking module flags.
5555
unsigned getDebugMetadataVersionFromModule(const Module &M);
5656

57-
/// \brief Utility to find all debug info in a module.
57+
/// Utility to find all debug info in a module.
5858
///
5959
/// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
6060
/// list debug info MDNodes used by an instruction, DebugInfoFinder uses
@@ -64,31 +64,33 @@ unsigned getDebugMetadataVersionFromModule(const Module &M);
6464
/// used by the CUs.
6565
class DebugInfoFinder {
6666
public:
67-
/// \brief Process entire module and collect debug info anchors.
67+
/// Process entire module and collect debug info anchors.
6868
void processModule(const Module &M);
69+
/// Process a single instruction and collect debug info anchors.
70+
void processInstruction(const Module &M, const Instruction &I);
6971

70-
/// \brief Process DbgDeclareInst.
72+
/// Process DbgDeclareInst.
7173
void processDeclare(const Module &M, const DbgDeclareInst *DDI);
72-
/// \brief Process DbgValueInst.
74+
/// Process DbgValueInst.
7375
void processValue(const Module &M, const DbgValueInst *DVI);
74-
/// \brief Process debug info location.
76+
/// Process debug info location.
7577
void processLocation(const Module &M, const DILocation *Loc);
7678

77-
/// \brief Clear all lists.
79+
/// Clear all lists.
7880
void reset();
7981

8082
private:
8183
void InitializeTypeMap(const Module &M);
8284

83-
void processType(DIType *DT);
84-
void processSubprogram(DISubprogram *SP);
8585
void processCompileUnit(DICompileUnit *CU);
8686
void processScope(DIScope *Scope);
87+
void processSubprogram(DISubprogram *SP);
88+
void processType(DIType *DT);
8789
bool addCompileUnit(DICompileUnit *CU);
8890
bool addGlobalVariable(DIGlobalVariableExpression *DIG);
91+
bool addScope(DIScope *Scope);
8992
bool addSubprogram(DISubprogram *SP);
9093
bool addType(DIType *DT);
91-
bool addScope(DIScope *Scope);
9294

9395
public:
9496
using compile_unit_iterator =

lib/IR/DebugInfo.cpp

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -61,52 +61,16 @@ void DebugInfoFinder::reset() {
6161
}
6262

6363
void DebugInfoFinder::processModule(const Module &M) {
64-
for (auto *CU : M.debug_compile_units()) {
65-
addCompileUnit(CU);
66-
for (auto DIG : CU->getGlobalVariables()) {
67-
if (!addGlobalVariable(DIG))
68-
continue;
69-
auto *GV = DIG->getVariable();
70-
processScope(GV->getScope());
71-
processType(GV->getType().resolve());
72-
}
73-
for (auto *ET : CU->getEnumTypes())
74-
processType(ET);
75-
for (auto *RT : CU->getRetainedTypes())
76-
if (auto *T = dyn_cast<DIType>(RT))
77-
processType(T);
78-
else
79-
processSubprogram(cast<DISubprogram>(RT));
80-
for (auto *Import : CU->getImportedEntities()) {
81-
auto *Entity = Import->getEntity().resolve();
82-
if (auto *T = dyn_cast<DIType>(Entity))
83-
processType(T);
84-
else if (auto *SP = dyn_cast<DISubprogram>(Entity))
85-
processSubprogram(SP);
86-
else if (auto *NS = dyn_cast<DINamespace>(Entity))
87-
processScope(NS->getScope());
88-
else if (auto *M = dyn_cast<DIModule>(Entity))
89-
processScope(M->getScope());
90-
else
91-
llvm_unreachable("unexpected imported entity type");
92-
}
93-
}
64+
for (auto *CU : M.debug_compile_units())
65+
processCompileUnit(CU);
9466
for (auto &F : M.functions()) {
9567
if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
9668
processSubprogram(SP);
9769
// There could be subprograms from inlined functions referenced from
9870
// instructions only. Walk the function to find them.
99-
for (const BasicBlock &BB : F) {
100-
for (const Instruction &I : BB) {
101-
if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
102-
processDeclare(M, DDI);
103-
else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
104-
processValue(M, DVI);
105-
106-
if (auto DbgLoc = I.getDebugLoc())
107-
processLocation(M, DbgLoc.get());
108-
}
109-
}
71+
for (const BasicBlock &BB : F)
72+
for (const Instruction &I : BB)
73+
processInstruction(M, I);
11074
}
11175
}
11276

@@ -142,6 +106,17 @@ void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
142106
}
143107
}
144108

109+
void DebugInfoFinder::processInstruction(const Module &M,
110+
const Instruction &I) {
111+
if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
112+
processDeclare(M, DDI);
113+
else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
114+
processValue(M, DVI);
115+
116+
if (auto DbgLoc = I.getDebugLoc())
117+
processLocation(M, DbgLoc.get());
118+
}
119+
145120
void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
146121
if (!Loc)
147122
return;

lib/Transforms/Utils/CloneFunction.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,36 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
4343
DebugInfoFinder *DIFinder) {
4444
DenseMap<const MDNode *, MDNode *> Cache;
4545
BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F);
46-
if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
46+
if (BB->hasName())
47+
NewBB->setName(BB->getName() + NameSuffix);
4748

4849
bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
4950
Module *TheModule = F ? F->getParent() : nullptr;
5051

5152
// Loop over all instructions, and copy them over.
52-
for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
53-
II != IE; ++II) {
54-
55-
if (DIFinder && TheModule) {
56-
if (auto *DDI = dyn_cast<DbgDeclareInst>(II))
57-
DIFinder->processDeclare(*TheModule, DDI);
58-
else if (auto *DVI = dyn_cast<DbgValueInst>(II))
59-
DIFinder->processValue(*TheModule, DVI);
60-
61-
if (auto DbgLoc = II->getDebugLoc())
62-
DIFinder->processLocation(*TheModule, DbgLoc.get());
63-
}
53+
for (const Instruction &I : *BB) {
54+
if (DIFinder && TheModule)
55+
DIFinder->processInstruction(*TheModule, I);
6456

65-
Instruction *NewInst = II->clone();
66-
if (II->hasName())
67-
NewInst->setName(II->getName()+NameSuffix);
57+
Instruction *NewInst = I.clone();
58+
if (I.hasName())
59+
NewInst->setName(I.getName() + NameSuffix);
6860
NewBB->getInstList().push_back(NewInst);
69-
VMap[&*II] = NewInst; // Add instruction map to value.
61+
VMap[&I] = NewInst; // Add instruction map to value.
7062

71-
hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
72-
if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
63+
hasCalls |= (isa<CallInst>(I) && !isa<DbgInfoIntrinsic>(I));
64+
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
7365
if (isa<ConstantInt>(AI->getArraySize()))
7466
hasStaticAllocas = true;
7567
else
7668
hasDynamicAllocas = true;
7769
}
7870
}
79-
71+
8072
if (CodeInfo) {
8173
CodeInfo->ContainsCalls |= hasCalls;
8274
CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
83-
CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
75+
CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
8476
BB != &BB->getParent()->getEntryBlock();
8577
}
8678
return NewBB;
@@ -197,18 +189,15 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
197189
Returns.push_back(RI);
198190
}
199191

200-
for (DISubprogram *ISP : DIFinder.subprograms()) {
201-
if (ISP != SP) {
192+
for (DISubprogram *ISP : DIFinder.subprograms())
193+
if (ISP != SP)
202194
VMap.MD()[ISP].reset(ISP);
203-
}
204-
}
205195

206196
for (DICompileUnit *CU : DIFinder.compile_units())
207197
VMap.MD()[CU].reset(CU);
208198

209-
for (auto *Type : DIFinder.types()) {
199+
for (DIType *Type : DIFinder.types())
210200
VMap.MD()[Type].reset(Type);
211-
}
212201

213202
// Loop over all of the instructions in the function, fixing up operand
214203
// references as we go. This uses VMap to do all the hard work.

tools/opt/opt.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ int main(int argc, char **argv) {
726726
// a stream to write to them. Note that llc does something similar and it
727727
// may be worth to abstract this out in the future.
728728
SmallVector<char, 0> Buffer;
729-
SmallVector<char, 0> CompileTwiceBuffer;
729+
SmallVector<char, 0> FirstRunBuffer;
730730
std::unique_ptr<raw_svector_ostream> BOS;
731731
raw_ostream *OS = nullptr;
732732

@@ -765,20 +765,20 @@ int main(int argc, char **argv) {
765765
// Run all passes on the original module first, so the second run processes
766766
// the clone to catch CloneModule bugs.
767767
Passes.run(*M);
768-
CompileTwiceBuffer = Buffer;
768+
FirstRunBuffer = Buffer;
769769
Buffer.clear();
770770

771771
Passes.run(*M2);
772772

773773
// Compare the two outputs and make sure they're the same
774774
assert(Out);
775-
if (Buffer.size() != CompileTwiceBuffer.size() ||
776-
(memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) !=
777-
0)) {
778-
errs() << "Running the pass manager twice changed the output.\n"
779-
"Writing the result of the second run to the specified output.\n"
780-
"To generate the one-run comparison binary, just run without\n"
781-
"the compile-twice option\n";
775+
if (Buffer.size() != FirstRunBuffer.size() ||
776+
(memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
777+
errs()
778+
<< "Running the pass manager twice changed the output.\n"
779+
"Writing the result of the second run to the specified output.\n"
780+
"To generate the one-run comparison binary, just run without\n"
781+
"the compile-twice option\n";
782782
Out->os() << BOS->str();
783783
Out->keep();
784784
if (OptRemarkFile)

0 commit comments

Comments
 (0)