/// 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)
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;
#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"
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;
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;
}
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;
}
}
!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)