-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiveVariables.cpp
More file actions
140 lines (99 loc) · 3.48 KB
/
Copy pathLiveVariables.cpp
File metadata and controls
140 lines (99 loc) · 3.48 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
#include "DataFlowAnalysis.h"
#include <iostream>
namespace DFA {
LiveVariables::LiveVariables(BasicBlock_list_sptr bbs) : _bbs(bbs) {
auto counter = 1;
// Compute Use[i] and Def[i] sets. Where 'i' is an instruction and
// Use[i]/Def[i] contains the set of all variables used/defined by that instruction.
for (auto& bb : *_bbs) {
for (auto& ins : bb->instructions()) {
auto instr = ins.shared_from_this();
_inInstr[instr]= make_shared<STEntry_set>();
_outInstr[instr]= make_shared<STEntry_set>();
this->_defs[instr] = instr->defs();
this->_uses[instr] = instr->uses();
}
_outBb[bb] = make_shared<STEntry_set>();
_inBb[bb]= make_shared<STEntry_set>();
}
}
void LiveVariables::execute() {
bool hadChanges = true;
while (hadChanges) {
hadChanges = false;
for (auto& bb : *_bbs) {
// This represents the UNION of INs of successors BBs/Instructions
auto prevIns = make_shared<STEntry_set>();
// for each successor basic block
auto succs = *bb->succs();
for (auto succ : succs) {
auto firstIn = _inBb[succ];
// This is just to contain tmp Unions
auto tmpRes = make_shared<STEntry_set>();
// Add together the OUT of the last bb with the OUT of the current one
std::set_union( prevIns->begin(), prevIns->end(),
firstIn->begin(), firstIn->end(),
std::inserter(*tmpRes, tmpRes->begin()));
prevIns = tmpRes;
}
// Output of current basic block comes from the Ins of the successor BBs
_outBb[bb] = prevIns;
// So the in of the first instruction is the UNION of the OUT of
// the predecessors. In the case of the first instruction of the BB the OUT
// is the UNION of OUT's of all predecessors.
// Now compute the IN/OUT of each instruction
auto begInstrIte = bb->instructions().rbegin();
auto endInstrIte = bb->instructions().rend();
while (begInstrIte != endInstrIte) {
auto instr = (*begInstrIte).shared_from_this();
_outInstr[instr] = prevIns;
auto tmpIns = make_shared<STEntry_set>();
// Add together the OUT of the last bb with the OUT of the current one
std::set_difference(_outInstr[instr]->begin(), _outInstr[instr]->end(),
_defs[instr]->begin(), _defs[instr]->end(),
std::inserter(*tmpIns, tmpIns->begin()));
auto newIns = make_shared<STEntry_set>();
std::set_union(_uses[instr]->begin(), _uses[instr]->end(),
tmpIns->begin(), tmpIns->end(),
std::inserter(*newIns, newIns->begin()));
if (*_inInstr[instr] != *newIns) {
hadChanges = true;
_inInstr[instr] = newIns;
}
prevIns = newIns;
begInstrIte++;
}
_inBb[bb] = prevIns;
}
}
}
void LiveVariables::dump() {
stringstream output;
output << "Live Variable Analysis dump output: " << endl;
for (auto& bb : *_bbs) {
output << "BB" << bb->id() << ":" << endl;
for (auto& ins : bb->instructions()) {
auto instr = ins.shared_from_this();
instr->dump(output);
output << "\t|-> IN {";
for (auto in : *_inInstr[instr]) {
output << in->getName() << " ";
}
output << "} OUT { ";
for (auto out : *_outInstr[instr]) {
output << out->getName() << " ";
}
output << "} DEFS { ";
for (auto d : *_defs[instr]) {
output << d->getName() << " ";
}
output << "} USES { ";
for (auto u : *_uses[instr]) {
output << u->getName() << " ";
}
output << "} " << endl;
}
}
cout << output.str() << endl;
}
}