forked from revng/revng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectfunctionboundaries.cpp
63 lines (52 loc) · 1.84 KB
/
collectfunctionboundaries.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
/// \file collectfunctionboundaries.cpp
/// \brief Implementation of the pass to collect the function boundaries
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
// LLVM includes
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Metadata.h"
// Local includes
#include "collectfunctionboundaries.h"
using namespace llvm;
template<typename T>
struct CompareByName {
bool operator()(const T *LHS, const T *RHS) const {
return LHS->getName() < RHS->getName();
}
};
char CollectFunctionBoundaries::ID = 0;
static RegisterPass<CollectFunctionBoundaries> X("cfb",
"Collect function boundaries "
"Pass",
true,
true);
void CollectFunctionBoundaries::serialize(std::ostream &Output) {
Output << "function,basicblock\n";
auto Comparator = CompareByName<const BasicBlock>();
for (auto &P : Functions) {
std::sort(P.second.begin(), P.second.end(), Comparator);
for (BasicBlock *BB : P.second) {
Output << P.first.data() << "," << BB->getName().data() << "\n";
}
}
}
bool CollectFunctionBoundaries::runOnFunction(Function &F) {
Functions.clear();
for (BasicBlock &BB : F) {
if (!BB.empty()) {
TerminatorInst *Terminator = BB.getTerminator();
if (MDNode *Node = Terminator->getMetadata("func.member.of")) {
auto *Tuple = cast<MDTuple>(Node);
for (const MDOperand &Op : Tuple->operands()) {
auto *FunctionMD = cast<MDTuple>(Op);
auto *FunctionNameMD = cast<MDString>(&*FunctionMD->getOperand(0));
Functions[FunctionNameMD->getString()].push_back(&BB);
}
}
}
}
return false;
}