-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathProbabilities.cpp
128 lines (113 loc) · 3.18 KB
/
PathProbabilities.cpp
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
#include "llvm/IR/Module.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/CallSite.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/IR/CFG.h"
#include "llvm/ADT/Hashing.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace llvm;
#include "PathProbabilities.h"
void
PathProbabilities::getAnalysisUsage(AnalysisUsage &AU) const
{
AU.setPreservesCFG();
AU.addRequired<BranchProbabilityInfoWrapperPass>();
}
class BranchConnect {
BasicBlock *source;
BasicBlock *destination;
int64_t Num;
int64_t Dom;
public:
BranchConnect(BasicBlock *B, BasicBlock *BB, int64_t N, int64_t D)
: source(B), destination(BB), Num(N), Dom(D) {}
BasicBlock *getSourceBlock() {
return source;
}
Function *getSourceFunction() {
return source->getParent();
}
BasicBlock *getDestinationBlock() {
return destination;
}
Function *getDestinationFunction() {
return destination->getParent();
}
double getProbability() {
return (double)Num/double(Dom);
}
};
static void
printFunctionName(Function *F)
{
if (!F->hasName()) {
errs() << "Function has no name,.\n";
return;
}
errs() << "Function name: " << F->getName().str() << "\n";
return;
}
bool
PathProbabilities::runOnModule(Module &M)
{
std::vector<BranchConnect *> rawProbList;
std::vector<BranchConnect *> probList;
for (Function &F : M) {
if (F.isDeclaration()) {
continue;
}
BranchProbabilityInfo &bpi = \
getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI();
for (BasicBlock &B : F) {
TerminatorInst *ti = B.getTerminator();
for (unsigned ii = 0; ii < ti->getNumSuccessors(); ii++) {
BranchProbability bp = bpi.getEdgeProbability(&B, ii);
bp.dump();
BranchConnect *bConn = new BranchConnect(&B,
ti->getSuccessor(ii), bp.getNumerator(),
bp.getDenominator());
rawProbList.push_back(bConn);
}
errs() << "----------------------------------------\n";
for (auto it = succ_begin(&B); it != succ_end(&B); ++it) {
BasicBlock *dst = *it;
BranchProbability bp = bpi.getEdgeProbability(&B, dst);
bp.dump();
BranchConnect *bConn = new BranchConnect(&B, dst,
bp.getNumerator(),
bp.getDenominator());
probList.push_back(bConn);
}
errs() << "##########################################33\n";
}
}
errs() << "\n~~Raw edge results: \n";
for (BranchConnect *c : rawProbList) {
// BranchConnect *c = *it;
BasicBlock *sb = c->getSourceBlock();
BasicBlock *db = c->getDestinationBlock();
errs() << (long)sb << " --> " << (long)db << " : " << c->getProbability() << "\n";
}
errs() << "\n~~Summed results: \n";
for (BranchConnect *c : probList) {
BasicBlock *sb = c->getSourceBlock();
BasicBlock *db = c->getDestinationBlock();
errs() << (long)sb << " --> " << (long)db << " : " << c->getProbability() << "\n";
}
return false;
}
char PathProbabilities::ID = 0;
static RegisterPass<PathProbabilities> X("pprobs", "");