summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Utils/AddDiscriminators.cpp
diff options
context:
space:
mode:
authorDehao Chen <dehao@google.com>2015-11-19 19:53:05 +0000
committerDehao Chen <dehao@google.com>2015-11-19 19:53:05 +0000
commit23e2278e271ef82b0a72c031856ce06da696d817 (patch)
treeaf688571ae0a62a47e6acd5550e8863a398e9ba7 /llvm/lib/Transforms/Utils/AddDiscriminators.cpp
parentbfb3f2bf7d856692a103f3a32d402bcc535c163d (diff)
downloadbcm5719-llvm-23e2278e271ef82b0a72c031856ce06da696d817.tar.gz
bcm5719-llvm-23e2278e271ef82b0a72c031856ce06da696d817.zip
Reimplement discriminator assignment algorithm.
Summary: The new algorithm is more efficient (O(n), n is number of basic blocks). And it is guaranteed to cover all cases of multiple BB mapped to same line. Reviewers: dblaikie, davidxl, dnovillo Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D14738 llvm-svn: 253594
Diffstat (limited to 'llvm/lib/Transforms/Utils/AddDiscriminators.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/AddDiscriminators.cpp89
1 files changed, 37 insertions, 52 deletions
diff --git a/llvm/lib/Transforms/Utils/AddDiscriminators.cpp b/llvm/lib/Transforms/Utils/AddDiscriminators.cpp
index db2bf6a2a9f..ed9ff5a96c4 100644
--- a/llvm/lib/Transforms/Utils/AddDiscriminators.cpp
+++ b/llvm/lib/Transforms/Utils/AddDiscriminators.cpp
@@ -52,7 +52,7 @@
// http://wiki.dwarfstd.org/index.php?title=Path_Discriminators
//===----------------------------------------------------------------------===//
-#include "llvm/Transforms/Scalar.h"
+#include "llvm/ADT/DenseMap.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DIBuilder.h"
@@ -65,6 +65,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Transforms/Scalar.h"
using namespace llvm;
@@ -169,60 +170,44 @@ bool AddDiscriminators::runOnFunction(Function &F) {
LLVMContext &Ctx = M->getContext();
DIBuilder Builder(*M, /*AllowUnresolved*/ false);
- // Traverse all the blocks looking for instructions in different
- // blocks that are at the same file:line location.
- for (BasicBlock &B : F) {
- TerminatorInst *Last = B.getTerminator();
- const DILocation *LastDIL = Last->getDebugLoc();
- if (!LastDIL)
- continue;
-
- for (unsigned I = 0; I < Last->getNumSuccessors(); ++I) {
- BasicBlock *Succ = Last->getSuccessor(I);
- Instruction *First = Succ->getFirstNonPHIOrDbgOrLifetime();
- const DILocation *FirstDIL = First->getDebugLoc();
- if (!FirstDIL || FirstDIL->getDiscriminator())
- continue;
-
- // If the first instruction (First) of Succ is at the same file
- // location as B's last instruction (Last), add a new
- // discriminator for First's location and all the instructions
- // in Succ that share the same location with First.
- if (!FirstDIL->canDiscriminate(*LastDIL)) {
- // Create a new lexical scope and compute a new discriminator
- // number for it.
- StringRef Filename = FirstDIL->getFilename();
- auto *Scope = FirstDIL->getScope();
- auto *File = Builder.createFile(Filename, Scope->getDirectory());
+ typedef std::pair<StringRef, unsigned> Location;
+ typedef DenseMap<const BasicBlock *, Metadata *> BBScopeMap;
+ typedef DenseMap<Location, BBScopeMap> LocationBBMap;
- // FIXME: Calculate the discriminator here, based on local information,
- // and delete DILocation::computeNewDiscriminator(). The current
- // solution gives different results depending on other modules in the
- // same context. All we really need is to discriminate between
- // FirstDIL and LastDIL -- a local map would suffice.
- unsigned Discriminator = FirstDIL->computeNewDiscriminator();
- auto *NewScope =
- Builder.createLexicalBlockFile(Scope, File, Discriminator);
+ LocationBBMap LBM;
- // Attach this new debug location to First and every
- // instruction following First that shares the same location.
- for (BasicBlock::iterator I1(*First), E1 = Succ->end(); I1 != E1;
- ++I1) {
- const DILocation *CurrentDIL = I1->getDebugLoc();
- if (CurrentDIL && CurrentDIL->getLine() == FirstDIL->getLine() &&
- CurrentDIL->getFilename() == FirstDIL->getFilename()) {
- I1->setDebugLoc(DILocation::get(Ctx, CurrentDIL->getLine(),
- CurrentDIL->getColumn(), NewScope,
- CurrentDIL->getInlinedAt()));
- DEBUG(dbgs() << CurrentDIL->getFilename() << ":"
- << CurrentDIL->getLine() << ":"
- << CurrentDIL->getColumn() << ":"
- << CurrentDIL->getDiscriminator() << *I1 << "\n");
- }
- }
- DEBUG(dbgs() << "\n");
- Changed = true;
+ // Traverse all instructions in the function. If the source line location
+ // of the instruction appears in other basic block, assign a new
+ // discriminator for this instruction.
+ for (BasicBlock &B : F) {
+ for (auto &I : B.getInstList()) {
+ if (isa<DbgInfoIntrinsic>(&I))
+ continue;
+ const DILocation *DIL = I.getDebugLoc();
+ if (!DIL)
+ continue;
+ Location L = std::make_pair(DIL->getFilename(), DIL->getLine());
+ auto &BBMap = LBM[L];
+ auto R = BBMap.insert(std::make_pair(&B, (Metadata *)nullptr));
+ if (BBMap.size() == 1)
+ continue;
+ bool InsertSuccess = R.second;
+ Metadata *&NewScope = R.first->second;
+ // If we could insert a different block in the same location, a
+ // discriminator is needed to distinguish both instructions.
+ if (InsertSuccess) {
+ auto *Scope = DIL->getScope();
+ auto *File =
+ Builder.createFile(DIL->getFilename(), Scope->getDirectory());
+ NewScope = Builder.createLexicalBlockFile(
+ Scope, File, DIL->computeNewDiscriminator());
}
+ I.setDebugLoc(DILocation::get(Ctx, DIL->getLine(), DIL->getColumn(),
+ NewScope, DIL->getInlinedAt()));
+ DEBUG(dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
+ << DIL->getColumn() << ":" << NewScope->getDiscriminator()
+ << I << "\n");
+ Changed = true;
}
}
OpenPOWER on IntegriCloud