-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw3-cfg.cpp
More file actions
70 lines (66 loc) · 2.52 KB
/
hw3-cfg.cpp
File metadata and controls
70 lines (66 loc) · 2.52 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
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "hw3-utils.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Dominators.h"
using namespace llvm;
#define DEBUG_TYPE "hw3_cfg"
#define SHOW(a) std::cout << #a << ": " << (a) << std::endl
struct hw3_cfg : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
hw3_cfg() : FunctionPass(ID) {}
bool runOnFunction(Function &F) override {
OFile ofile(F);
// TODO: write something here
//to output the edges
//ofile.printEdge(BB1, BB2, [OFile::EdgeType::<type>]);
//to output key blocks
//ofile.printKeyBlock(BB);
//prints name of function
errs() << "Visiting function " << F.getName();
//iterate over basic blocks
BasicBlock &BBF = F.back();
DominatorTree DT = DominatorTree(F);
for (BasicBlock &BB : F){
//Checking to see if BB branches, do we have to handle BB branchign into 1 and into 2 differently?
//get terminator instruction of basic block and iterate through its succesors
for (j)
Instruction *I = BB.getTerminator();
//handle 2 successors
if(I->getNumSuccessors() == 2)
{
BasicBlock BB1 =(I->getSuccessor(0));
BasicBlock BB2 =(I->getSuccessor(1));
ofile.printEdge(BB, *BB1, OFile::EdgeType::T);
ofile.printEdge(BB, *BB2, OFile::EdgeType::T);
}
//handle 1 successors
else if (I->getNumSuccessors() == 1)
{
BasicBlock BB1 = I->getSuccessor(0);
ofile.printEdge(BB, *BB1, OFile::EdgeType::T);
}
for (Instruction &II : BB) {
// Iterate over each Instructions in Basic Blocks
// Dynamically cast Instruction to CallInst.
// This step will return false for any instruction which is not a CallInst
if(CallInst *CI = dyn_cast<CallInst>(&II)) {
// Print out that we have encountered a call instruction
errs() << "Encountered a call instruction " << CI << "\n";
//Print out function name
outs() << " |-" << CI->getCalledFunction()->getName() << "\n";
if(DT.dominates(&BB, &II)){
errs() << "Encountered a key block " << CI << "\n";
ofile.printKeyBlock(BB);
}
}
}}
ofile.printKeyBlock(BBF);
return false;
}
};