-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLLVMAssignment.cpp
144 lines (130 loc) · 4.62 KB
/
LLVMAssignment.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements two versions of the LLVM "Hello World" pass described
// in docs/WritingAnLLVMPass.html
//
//===----------------------------------------------------------------------===//
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Pass.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/ToolOutputFile.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Transforms/Utils.h>
#include "FuncPtrVisitor.h"
#include "Liveness.h"
using namespace llvm;
static ManagedStatic<LLVMContext> GlobalContext;
static LLVMContext &getGlobalContext() { return *GlobalContext; }
struct EnableFunctionOptPass : public FunctionPass {
static char ID;
EnableFunctionOptPass() : FunctionPass(ID) {}
bool runOnFunction(Function &F) override {
if (F.hasFnAttribute(Attribute::OptimizeNone)) {
F.removeFnAttr(Attribute::OptimizeNone);
}
return true;
}
};
char EnableFunctionOptPass::ID = 0;
///!TODO TO BE COMPLETED BY YOU FOR ASSIGNMENT 3
struct FuncPtrPass : public ModulePass {
static char ID; // Pass identification, replacement for typeid
FuncPtrPass() : ModulePass(ID) {}
bool runOnModule(Module &M) override {
/*
errs() << "Hello: ";
errs().write_escaped(M.getName()) << '\n';
M.dump();
errs() << "------------------------------\n";
*/
/*
DataflowResult<PointerInfo>::Type result;
FuncPtrVisitor visitor;
std::set<Function*> worklist;
visitor.change = true;
int turn = 0;
while(visitor.change && turn++ < 50){
errs()<<"\n\n\n===================="<<turn<<"=================\n";
visitor.change = false;
for(Module::iterator mi = M.begin(),me = M.end();
mi != me;mi++){
Function* func = &*mi;
PointerInfo initval;
compForwardDataflow(func, &visitor, &result, initval);
}
}
*/
// force stop
int count = 0;
DataflowResult<PointerInfo>::Type result;
FuncPtrVisitor visitor;
std::set<Function *> worklist;
for (Module::iterator mi = M.begin(), me = M.end(); mi != me; mi++) {
worklist.insert(&*mi);
}
while (!worklist.empty() && count < 50) {
count++;
Function *func = *(worklist.begin());
worklist.erase(worklist.begin());
PointerInfo initval;
compForwardDataflow(func, &visitor, &result, initval);
visitor.arg_p2s[func].p2set.clear();
worklist.insert(visitor.worklist.begin(), visitor.worklist.end());
visitor.worklist.clear();
}
#ifdef DEBUG
M.dump();
#endif
visitor.printResult();
return false;
}
};
char FuncPtrPass::ID = 0;
static RegisterPass<FuncPtrPass> X("funcptrpass",
"Print function call instruction");
char Liveness::ID = 0;
static RegisterPass<Liveness> Y("liveness", "Liveness Dataflow Analysis");
static cl::opt<std::string> InputFilename(cl::Positional,
cl::desc("<filename>.bc"),
cl::init(""));
int main(int argc, char **argv) {
LLVMContext &Context = getGlobalContext();
SMDiagnostic Err;
// Parse the command line to read the Inputfilename
cl::ParseCommandLineOptions(
argc, argv,
"FuncPtrPass \n My first LLVM too which does not do much.\n");
// Load the input module
std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
if (!M) {
Err.print(argv[0], errs());
return 1;
}
llvm::legacy::PassManager Passes;
#if LLVM_VERSION_MAJOR >= 5
Passes.add(new EnableFunctionOptPass());
#endif
/// Transform it to SSA
Passes.add(llvm::createPromoteMemoryToRegisterPass());
/// Your pass to print Function and Call Instructions
// Passes.add(new Liveness());
Passes.add(new FuncPtrPass());
Passes.run(*M.get());
#ifndef NDEBUG
// system("pause");
#endif
}