-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintBranchProbability.cpp
More file actions
88 lines (73 loc) · 2.33 KB
/
Copy pathPrintBranchProbability.cpp
File metadata and controls
88 lines (73 loc) · 2.33 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
// Print branch probabilities in a program
//
// By Andrew Santosa <santosa_1999@yahoo.com>
// This software is in the public domain.
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/PassManager.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SourceMgr.h"
#include <set>
#include <vector>
class PrintBranchProbability : public llvm::ModulePass {
public:
static char ID;
PrintBranchProbability() : ModulePass(ID) {}
virtual bool runOnModule(llvm::Module &M) {
for (llvm::Module::iterator func = M.begin(), fe = M.end(); func != fe;
++func) {
if (func->isDeclaration())
continue;
const llvm::BranchProbabilityInfo &BPI =
getAnalysis<llvm::BranchProbabilityInfo>(*func);
for (llvm::Function::iterator bi = func->begin(), be = func->end();
bi != be; ++bi) {
llvm::TerminatorInst *ti = bi->getTerminator();
llvm::errs() << "BRANCH: ";
ti->dump();
unsigned numSuccessors = ti->getNumSuccessors();
for (unsigned i = 0; i < numSuccessors; ++i) {
llvm::BranchProbability prob = BPI.getEdgeProbability(&(*bi), i);
llvm::errs() << "EDGE " << i << " PROBABILITY: ";
prob.dump();
}
}
}
return false; // does not modify program
}
virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<llvm::BranchProbabilityInfo>();
}
};
char PrintBranchProbability::ID = 0;
static llvm::RegisterPass<PrintBranchProbability>
X("print-branch-probability", "Print branch probability", false, false);
/*
* Main.
*/
int main(int argc, char **argv) {
if (argc < 2) {
llvm::errs() << "Filename unspecified\n";
return 1;
}
llvm::LLVMContext &Context = llvm::getGlobalContext();
llvm::SMDiagnostic Err;
llvm::Module *M = ParseIRFile(argv[1], Err, Context);
if (M == 0) {
llvm::errs() << "ERROR: failed to load " << argv[0] << "\n";
return 1;
}
llvm::PassManager PM;
PM.add(new llvm::BranchProbabilityInfo());
PM.add(new PrintBranchProbability());
PM.run(*M);
return 0;
}