-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiveVariableAnalysis.cpp
More file actions
75 lines (70 loc) · 2.21 KB
/
Copy pathLiveVariableAnalysis.cpp
File metadata and controls
75 lines (70 loc) · 2.21 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
#include "LiveVariableAnalysis.h"
#include "MachineCode.h"
#include <algorithm>
void LiveVariableAnalysis::pass(MachineUnit *unit)
{
for (auto &func : unit->getFuncs())
{
computeUsePos(func);
computeDefUse(func);
iterate(func);
}
}
void LiveVariableAnalysis::pass(MachineFunction *func)
{
computeUsePos(func);
computeDefUse(func);
iterate(func);
}
void LiveVariableAnalysis::computeDefUse(MachineFunction *func)
{
for (auto &block : func->getBlocks())
{
for (auto inst = block->getInsts().begin(); inst != block->getInsts().end(); inst++)
{
auto user = (*inst)->getUse();
std::set<MachineOperand *> temp(user.begin(), user.end());
set_difference(temp.begin(), temp.end(),
def[block].begin(), def[block].end(), inserter(use[block], use[block].end()));
auto defs = (*inst)->getDef();
for (auto &d : defs)
def[block].insert(all_uses[*d].begin(), all_uses[*d].end());
}
}
}
void LiveVariableAnalysis::iterate(MachineFunction *func)
{
for (auto &block : func->getBlocks())
block->getLiveIn().clear();
bool change;
change = true;
while (change)
{
change = false;
for (auto &block : func->getBlocks())
{
block->getLiveOut().clear();
auto old = block->getLiveIn();
for (auto &succ : block->getSuccs())
block->getLiveOut().insert(succ->getLiveIn().begin(), succ->getLiveIn().end());
block->getLiveIn() = use[block];
std::vector<MachineOperand *> temp;
set_difference(block->getLiveOut().begin(), block->getLiveOut().end(),
def[block].begin(), def[block].end(), inserter(block->getLiveIn(), block->getLiveIn().end()));
if (old != block->getLiveIn())
change = true;
}
}
}
void LiveVariableAnalysis::computeUsePos(MachineFunction *func)
{
for (auto &block : func->getBlocks())
{
for (auto &inst : block->getInsts())
{
auto uses = inst->getUse();
for (auto &use : uses)
all_uses[*use].insert(use);
}
}
}