-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfg.cpp
170 lines (152 loc) · 4.19 KB
/
dfg.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
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
#include "dfg.h"
Block::Block(vector<Quaternion* > code, int id)
{
visit = false;
isreach = true; //假设每个数据块都可达
this->id = id;
for(auto i: code)
{
i->setBlock(this); //把指令标记为本块
blockCode.push_back(i);
}
}
Block::~Block()
{}
void Block::printCode()
{
printf("------Block %d---------\n", id);
for(auto code: blockCode)
code->toString();
}
DFG::DFG(InterCode& code)
{
code.makeFirst(); //表示首指令
intercode = code.getInterCode();
createBlock();
linkBlock();
//getBlockInfo();
}
DFG::~DFG()
{}
void DFG::createBlock()
{
int id = 0;
vector<Quaternion* > tmpCode;
tmpCode.push_back(intercode[0]);
for(int i = 1; i < intercode.size(); i ++)
{
if(intercode[i]->getFirst())
{
blocks.push_back(new Block(tmpCode, ++ id));
tmpCode.clear();
}
tmpCode.push_back(intercode[i]);
}
blocks.push_back(new Block(tmpCode, ++ id));
}
void DFG::linkBlock()
{
for(int i = 0; i < blocks.size(); i ++)
{
Quaternion* last = blocks[i]->blockCode.back();
if(last->isJmp() || last->isJmpCond()) //跳转指令
{
Block* tarBlock = last->getTarget()->getBlock();
blocks[i]->nextBlock.push_back(tarBlock);
tarBlock->preBlock.push_back(blocks[i]);
}
if(!last->isJmp() && i != blocks.size() - 1)
{
blocks[i]->nextBlock.push_back(blocks[i + 1]);
blocks[i + 1]->preBlock.push_back(blocks[i]);
}
}
}
void DFG::delBlock(Block* begin, Block* end)
{
//解除数据块关联关系
if(begin)
{
//begin->nextBlock.erase(find(begin->nextBlock.begin(), begin->nextBlock.end(), end));
//end->preBlock.erase(find(end->preBlock.begin(), end->preBlock.end(), begin));
begin->nextBlock.remove(end);
end->preBlock.remove(begin);
}
release(end);
}
void DFG::release(Block* block)
{
if(!reachable(block))
{
list<Block*> delList;
for(auto next: block->nextBlock)
delList.push_back(next);
for(auto next: delList)
{
//next->preBlock.erase(next->preBlock.begin(), next->preBlock.end(), block);
//block->nextBlock.erase(block->nextBlock.begin(), block->nextBlock.end(), next);
next->preBlock.remove(block);
block->nextBlock.remove(next);
}
for(auto next: delList)
release(next);
}
}
bool DFG::reachable(Block* block)
{
for(auto blk: blocks) blk->visit = false;
return reach(block);
}
bool DFG::reach(Block* block)
{
if(block == blocks[0]) return true; //Entry
if(block->visit == true) return false;
block->visit = true; //标记已经访问过
bool isReach = false;
for(auto pre: block->preBlock)
{
isReach = reach(pre);
if(isReach) break;
}
return isReach;
}
void DFG::getCode(list<Quaternion*> &optcode)
{
for(auto blk: blocks) blk->visit = false;
for(auto block: blocks)
{
list<Quaternion*> tmp;
if(reachable(block))
{
for(auto code: block->blockCode)
tmp.push_back(code);
optcode.splice(optcode.end(), tmp);
}
else
{
//记录数据块不可达
block->isreach = false;
}
}
}
void DFG::getBlockInfo()
{
printf("The first inst is:\n");
for(auto code: intercode)
if(code->getFirst()) code->toString();
printf("The block's num is: %d\n", blocks.size());
for(auto block: blocks)
{
printf("----------Block id: %d----------\n", block->id);
printf("The code is:\n");
for(auto code: block->blockCode)
code->toString();
printf("\nThe block's pre is:\n");
for(auto bk: block->preBlock)
printf("%d ", bk->id);
printf("\nThe block's next is:\n");
for(auto bk: block->nextBlock)
printf("%d ", bk->id);
system("pause");
}
}