diff options
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/include/llvm/IR/DebugInfoMetadata.h | 19 | ||||
| -rw-r--r-- | llvm/lib/Transforms/Utils/AddDiscriminators.cpp | 37 | ||||
| -rw-r--r-- | llvm/test/Transforms/AddDiscriminators/inlined.ll | 4 |
3 files changed, 27 insertions, 33 deletions
diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h index 47a6b87f557..6d02276fc8e 100644 --- a/llvm/include/llvm/IR/DebugInfoMetadata.h +++ b/llvm/include/llvm/IR/DebugInfoMetadata.h @@ -1270,6 +1270,9 @@ public: /// instructions that are on different basic blocks. inline unsigned getDiscriminator() const; + /// Returns a new DILocation with updated \p Discriminator. + inline DILocation *cloneWithDiscriminator(unsigned Discriminator) const; + Metadata *getRawScope() const { return getOperand(0); } Metadata *getRawInlinedAt() const { if (getNumOperands() == 2) @@ -1612,6 +1615,22 @@ unsigned DILocation::getDiscriminator() const { return 0; } +DILocation *DILocation::cloneWithDiscriminator(unsigned Discriminator) const { + DIScope *Scope = getScope(); + // Skip all parent DILexicalBlockFile that already have a discriminator + // assigned. We do not want to have nested DILexicalBlockFiles that have + // mutliple discriminators because only the leaf DILexicalBlockFile's + // dominator will be used. + for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope); + LBF && LBF->getDiscriminator() != 0; + LBF = dyn_cast<DILexicalBlockFile>(Scope)) + Scope = LBF->getScope(); + DILexicalBlockFile *NewScope = + DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator); + return DILocation::get(getContext(), getLine(), getColumn(), NewScope, + getInlinedAt()); +} + class DINamespace : public DIScope { friend class LLVMContextImpl; friend class MDNode; diff --git a/llvm/lib/Transforms/Utils/AddDiscriminators.cpp b/llvm/lib/Transforms/Utils/AddDiscriminators.cpp index 8120fa93113..66781ca76b4 100644 --- a/llvm/lib/Transforms/Utils/AddDiscriminators.cpp +++ b/llvm/lib/Transforms/Utils/AddDiscriminators.cpp @@ -57,12 +57,10 @@ #include "llvm/ADT/DenseSet.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Constants.h" -#include "llvm/IR/DIBuilder.h" #include "llvm/IR/DebugInfo.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/Module.h" #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" @@ -163,14 +161,10 @@ static bool addDiscriminators(Function &F) { return false; bool Changed = false; - Module *M = F.getParent(); - LLVMContext &Ctx = M->getContext(); - DIBuilder Builder(*M, /*AllowUnresolved*/ false); typedef std::pair<StringRef, unsigned> Location; - typedef SmallDenseMap<DIScope *, DILexicalBlockFile *, 1> ScopeMap; - typedef DenseMap<const BasicBlock *, ScopeMap> BBScopeMap; - typedef DenseMap<Location, BBScopeMap> LocationBBMap; + typedef DenseSet<const BasicBlock *> BBSet; + typedef DenseMap<Location, BBSet> LocationBBMap; typedef DenseMap<Location, unsigned> LocationDiscriminatorMap; typedef DenseSet<Location> LocationSet; @@ -187,29 +181,18 @@ static bool addDiscriminators(Function &F) { const DILocation *DIL = I.getDebugLoc(); if (!DIL) continue; - DIScope *Scope = DIL->getScope(); Location L = std::make_pair(DIL->getFilename(), DIL->getLine()); auto &BBMap = LBM[L]; - auto R = BBMap.insert({&B, ScopeMap()}); + auto R = BBMap.insert(&B); if (BBMap.size() == 1) continue; - bool InsertSuccess = R.second; - ScopeMap &Scopes = R.first->second; // If we could insert more than one block with the same line+file, a // discriminator is needed to distinguish both instructions. - auto R1 = Scopes.insert({Scope, nullptr}); - DILexicalBlockFile *&NewScope = R1.first->second; - if (!NewScope) { - unsigned Discriminator = InsertSuccess ? ++LDM[L] : LDM[L]; - auto *File = Builder.createFile(DIL->getFilename(), - Scope->getDirectory()); - NewScope = Builder.createLexicalBlockFile(Scope, File, Discriminator); - } - I.setDebugLoc(DILocation::get(Ctx, DIL->getLine(), DIL->getColumn(), - NewScope, DIL->getInlinedAt())); + unsigned Discriminator = R.second ? ++LDM[L] : LDM[L]; + I.setDebugLoc(DIL->cloneWithDiscriminator(Discriminator)); DEBUG(dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":" << DIL->getColumn() << ":" - << dyn_cast<DILexicalBlockFile>(NewScope)->getDiscriminator() + << Discriminator << " " << I << "\n"); Changed = true; } @@ -232,13 +215,7 @@ static bool addDiscriminators(Function &F) { Location L = std::make_pair(CurrentDIL->getFilename(), CurrentDIL->getLine()); if (!CallLocations.insert(L).second) { - auto *Scope = CurrentDIL->getScope(); - auto *File = Builder.createFile(CurrentDIL->getFilename(), - Scope->getDirectory()); - auto *NewScope = Builder.createLexicalBlockFile(Scope, File, ++LDM[L]); - Current->setDebugLoc(DILocation::get(Ctx, CurrentDIL->getLine(), - CurrentDIL->getColumn(), NewScope, - CurrentDIL->getInlinedAt())); + Current->setDebugLoc(CurrentDIL->cloneWithDiscriminator(++LDM[L])); Changed = true; } } diff --git a/llvm/test/Transforms/AddDiscriminators/inlined.ll b/llvm/test/Transforms/AddDiscriminators/inlined.ll index 25bbd54dfb5..2e8ea97348d 100644 --- a/llvm/test/Transforms/AddDiscriminators/inlined.ll +++ b/llvm/test/Transforms/AddDiscriminators/inlined.ll @@ -75,9 +75,7 @@ attributes #3 = { nounwind readnone } !17 = distinct !DILexicalBlock(scope: !7, file: !1, line: 1, column: 9) !18 = !DILocation(line: 1, column: 9, scope: !7) !19 = !DILocation(line: 1, column: 27, scope: !12, inlinedAt: !13) -; CHECK: ![[BR]] = !DILocation(line: 1, column: 9, scope: ![[BF2:[0-9]+]]) -; CHECK: ![[BF2]] = !DILexicalBlockFile(scope: ![[BF]], -; CHECK-SAME: discriminator: 1) +; CHECK: ![[BR]] = !DILocation(line: 1, column: 9, scope: !14) !20 = !DILocation(line: 1, column: 9, scope: !14) !21 = distinct !{!21, !18} !22 = !DILocation(line: 1, column: 56, scope: !12) |

