-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModulePassDemo.cpp
More file actions
141 lines (100 loc) · 4.21 KB
/
Copy pathModulePassDemo.cpp
File metadata and controls
141 lines (100 loc) · 4.21 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#define DEBUG_TYPE "times"
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/InstIterator.h"
#include "llvm/Support/Compiler.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Constant.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/Twine.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/ADT/Statistic.h"
using namespace llvm;
STATISTIC(PrintCounter, "Number of printf functions calls");
STATISTIC(LoadCounter, "Number of Load instrunction");
STATISTIC(StoreCounter, "Number of Store Instructions");
STATISTIC(FunctionCounter, "Number of Function Calls Encountered ");
STATISTIC(InstCounter, "Total number of Instructions Except the Function call");
STATISTIC(OtherFunctionCounter,"Number of Function calls apart from Printf");
STATISTIC(BasicBlockCounter, "Total number of Basic Blocks");
namespace {
struct ModulePassDemo : public ModulePass {
static char ID; // Pass identification
ModulePassDemo() : ModulePass(ID) {}
virtual bool runOnModule(Module &M) {
/*
llvm::GlobalVariable *globalcounter = new llvm::GlobalVariable(llvm::IntegerType::get(llvm::getGlobalContext(), 8), false, llvm::GlobalVariable::InternalLinkage, llvm::ConstantInt::get(llvm::getGlobalContext(), llvm::APInt(8, 0, false)), "GlobalCounter");
*/
errs()<<"==========Start============\n\n\n";
errs()<<"\n=======================Inside Module======================\n";
for (Module::iterator FunctionIter = M.getFunctionList().begin(); FunctionIter != M.getFunctionList().end(); ) {
Function *F = FunctionIter++; // F is a pointer to a Function instance
FunctionCounter++;
errs() << "-----------------------Function Name :" << F->getName() << "-----------------------\n";
errs()<<"\n== == == == ==Instructions Starts== == == == ==\n\n";
errs()<<" OPCODE \t| Instruction\n";
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
{
errs()<<" "<< I->getOpcodeName()<< "\t| "<< *I <<" \n";
InstCounter++;
}
errs()<<"\n== == == == ==Instructions Ends== == == == ==\n\n";
errs() << "-----------------------Function Ends Here---------------------\n\n";
errs() << "=======================================================================================\n";
errs()<<"\n=======================Module Ends======================\n";
errs() <<"Success Till Here";
}
errs()<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
// Printing all the Load instructions in the program
errs()<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~LOAD INSTRUCTIONS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
errs()<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
//DSADFSGFDHGFJHYKHKGHDGDFGDFGDFGDFG
for(Module::iterator F = M.begin(), E = M.end(); F!= E; ++F)
{
for(Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
{
ModulePassDemo::runOnBasicBlock(BB);
++BasicBlockCounter;
}
}
errs()<<"\n";
/*
errs()<<"\n-------------------------------------------------------------";
errs()<<"\nTotal Number of Printf Statements in the Program ="<<" "<<counter;
errs()<<"\n-------------------------------------------------------------\n";
*/
return false;
}
virtual bool runOnBasicBlock(Function::iterator &BB)
{
for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI)
{
if( CallInst * pCall = dyn_cast<CallInst>(BI)){
Function * pFunction = pCall->getCalledFunction();
std::string fname = pFunction->getName();
errs()<<"\n Function Name: "<<fname<<"\t| Function Call number: "<<++FunctionCounter;
if(fname =="printf")
PrintCounter++;
else
OtherFunctionCounter++;
//counter
}
else if(isa<LoadInst>(BI)){
LoadCounter++;
}
else if(isa<StoreInst>(BI)){
StoreCounter++;
}
}
return true;
}
};
}
char ModulePassDemo::ID = 0;
static RegisterPass<ModulePassDemo> X("ModulePassDemo", "Simple Module Pass program", false, false);