-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPrintDbgInfo.cpp
More file actions
70 lines (60 loc) · 2.1 KB
/
PrintDbgInfo.cpp
File metadata and controls
70 lines (60 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//===- PrintDbgInfo.cpp - Example code from "Writing an LLVM Pass" ---------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file prints out the source code info of loops and the loop_ids
//
//
//===----------------------------------------------------------------------===//
#include "llvm/Pass.h"
#include "llvm/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/DebugInfo.h"
#include "llvm/Support/type_traits.h"
#include "llvm/ADT/StringRef.h"
using namespace llvm;
StringRef * loopCond= new StringRef ("for.cond");
int loop_count = 0;
namespace {
struct PrintDbgInfo : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
PrintDbgInfo() : FunctionPass(ID) {}
virtual bool runOnFunction(Function &F) {
if (F.isDeclaration())
return false;
for (Function::iterator i = F.begin(), e = F.end(); i != e; ++i)
{
if(!((i->getName().find(*loopCond,0)))) //the first basic block of for loop
{
loop_count++;
for (BasicBlock::const_iterator I = i->begin(), E = i->end();
I != E; ++I) {
//get the line info for the first instruction of the basic block
if (MDNode *N = I->getMetadata("dbg")) { // Here I is an LLVM instruction
DILocation Loc(N); // DILocation is in DebugInfo.h
unsigned Line = Loc.getLineNumber();
StringRef File = Loc.getFilename();
//StringRef Dir = Loc.getDirectory();
errs()<<loop_count<<" "<<Line<<" "<<File<<"\n";
break;
}//end of if
}//end of for
}//end of if the first basic block of loop
}
return false;
}
// We don't modify the program, so we preserve all analyses
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
}
};
}
char PrintDbgInfo::ID = 0;
static RegisterPass<PrintDbgInfo>
Y("printDbgInfo", "print lines of instructions");