-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefUseAnalysis.cpp
More file actions
195 lines (171 loc) · 6.78 KB
/
defUseAnalysis.cpp
File metadata and controls
195 lines (171 loc) · 6.78 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Filename :
*
* Description :
*
* Author : Long Gong (saber.fate.dragon@gmail.com)
*
* Start Date : 6 Mar 2018
*
*/
#include "llvm/Pass.h"
#include "llvm/IR/Module.h"// for ModulePass
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h" // TerminatorInst
#include "llvm/IR/Value.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Instruction.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Format.h"// for format()
#include "llvm/ADT/GraphTraits.h"// for GraphTraits
#include <unordered_map> // for std::unordered_map
#include <vector> // for std::vector
#include <list>
#include "demandDrivenDataFlowHelper.h"
#include "DefUseAnalysisConfig.h"
using namespace llvm;
namespace {
using StatementDefUseInfoMap=std::unordered_map<const Instruction*, std::unique_ptr<saber::StatementDefUseInfo> >;
using DefUseVec=std::vector<saber::DefUsePair >;
struct QueryInfo{
const Instruction *QueryS; // query statement
const Instruction *Use;// statement that uses the variable
StringRef Variable;// variable name
QueryInfo(const Instruction* I, const std::string& V) : QueryS(I), Use(I), Variable(V){}
};
struct CorrelatedBranchDetection : public FunctionPass {
static char ID;
StatementDefUseInfoMap DefUseMap;
DefUseVec AllDefUses;
std::unordered_map<const Instruction*, bool> Q;
std::list<QueryInfo> Worklist;
// StringRef V;
// const Instruction *QueryS = nullptr;
size_t numOfPairs = 0;
size_t totalNumOfPairs = 0;
CorrelatedBranchDetection() : FunctionPass(ID){
}
bool runOnFunction(Function &F) override {
// Get def-use of each statement
for (auto BI = F.begin(), BE = F.end();BI != BE;++ BI){
for (Instruction &I: *BI){
const Instruction *CI = &I;
if (DefUseMap.find(CI) == DefUseMap.end())
DefUseMap[CI] = std::unique_ptr<saber::StatementDefUseInfo>(new saber::StatementDefUseInfo(CI));
}
// const Instruction *CII = &(BI->back());
// errs() << saber::toString(CII) << " <-- \n";
// const Instruction *PTI = CII->getPrevNode();
// while (PTI) {
// errs() << saber::toString(PTI) << " <-- \n";
// PTI = PTI->getPrevNode();
// }
// errs() << "\n\n";
}
// analysis def-use pair
for (auto BI = F.begin(), BE = F.end();BI != BE;++ BI) {
for (auto I = BI->rbegin(),EI = BI->rend();I != EI; ++ I) {
const Instruction *CI = &(*I);
for(const auto& v: DefUseMap[CI]->uses) {
analysis(v, CI);
}
}
}
#if DEF_USE_VERBOSE_LEVEL == 0
numOfPairs = AllDefUses.size();
DefUseMap.clear();
AllDefUses.clear();
#else
numOfPairs = AllDefUses.size() - totalNumOfPairs;
#endif
totalNumOfPairs += numOfPairs;
return false;
}
void analysis(const std::string &var, const Instruction* use){
Q.clear(); // reset Q
Worklist.clear(); // reset Worklist
QueryInfo tmpQ(use, var);
if (const Instruction* parent = use->getPrevNode()) {
raise_query(parent, use, tmpQ);
} else {
if (const BasicBlock *CBB = use->getParent()){
for (auto it=pred_begin(CBB),et=pred_end(CBB);it!=et;++it){
const BasicBlock *PB = *it;
raise_query(&(PB->back()), use, tmpQ);
}
}
}
while (!Worklist.empty()) {
auto pendingQ = Worklist.front();
Worklist.pop_front();// remove the query in the worklist
const Instruction *I = pendingQ.QueryS;
if (const Instruction *P=I->getPrevNode()){
if (Q.find(P) == Q.end()) raise_query(P, I, pendingQ);
} else {
if (const BasicBlock *CBB = I->getParent()){
for (auto it=pred_begin(CBB),et=pred_end(CBB);it!=et;++it){
const BasicBlock *PB = *it;
const Instruction *PI = &(PB->back());
if (Q.find(PI) == Q.end())
raise_query(PI, use, pendingQ);
}
}
}
}
}
void raise_query(const Instruction *m, const Instruction* n, QueryInfo& queryInfo) {
if (Q.find(m) == Q.end()) Q[m] = true;
if (!reslove(m, n, queryInfo)) {
queryInfo.QueryS = m;
Worklist.push_back(queryInfo);
}
}
bool reslove(const Instruction *m, const Instruction* n, QueryInfo& queryInfo) {
if (queryInfo.Variable.compare(DefUseMap[m]->def) == 0){// m defines V
AllDefUses.emplace_back(m, queryInfo.Use, queryInfo.Variable.str());
return true;// stop going further
}
return false;
}
void print(raw_ostream &O, const Module *) const override {
// for (const auto& pair: DefUseMap) {
// O << saber::toString(pair.first)
// << "\n";
// if (!pair.second->def.empty()) {
// O << "\t\tdef: " << pair.second->def << "\n";
// }
// if (!pair.second->uses.empty()) {
// O << "\t\tuses: ";
// for (const auto& use: pair.second->uses){
// O << use << " ";
// }
// O << "\n";
// }
// O << "\n";
// }
// }
O << "# of def-use pairs: "
// << AllDefUses.size()
<< numOfPairs << " (" << totalNumOfPairs << ")"
<< "\n";
#if DEF_USE_VERBOSE_LEVEL >= 1
for (const auto& du: AllDefUses){
O << "["
<< saber::toString(du.def)
<< " (BasicBlock: " << du.def->getParent()->getName() << ")"
<< " | "
<< saber::toString(du.use)
<< " (BasicBlock: " << du.use->getParent()->getName() << ")"
<< "]: "
<< du.variable
<< "\n";
}
#endif
}
};
char CorrelatedBranchDetection::ID = 0;
static RegisterPass<CorrelatedBranchDetection> X(PASS_NAME,
"Def-Use Analysis",
false,
false);
}