Introduce updateDiscriminator interface to DILocation to make it cleaner assigning...
authorDehao Chen <dehao@google.com>
Wed, 26 Oct 2016 15:48:45 +0000 (15:48 +0000)
committerDehao Chen <dehao@google.com>
Wed, 26 Oct 2016 15:48:45 +0000 (15:48 +0000)
Summary: This patch introduces updateDiscriminator to DILocation so that it can be directly called by AddDiscriminator. It also makes it easier to update the discriminator later.

Reviewers: dnovillo, dblaikie, aprantl, echristo

Subscribers: mehdi_amini, llvm-commits

Differential Revision: https://reviews.llvm.org/D25959

llvm-svn: 285207

llvm/include/llvm/IR/DebugInfoMetadata.h
llvm/lib/Transforms/Utils/AddDiscriminators.cpp
llvm/test/Transforms/AddDiscriminators/inlined.ll

index 47a6b87..6d02276 100644 (file)
@@ -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;
index 8120fa9..66781ca 100644 (file)
 #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;
       }
     }
index 25bbd54..2e8ea97 100644 (file)
@@ -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)