-
Notifications
You must be signed in to change notification settings - Fork 1
/
flowgraph.cpp
73 lines (62 loc) · 2.03 KB
/
flowgraph.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
#include "flowgraph.h"
#include <map>
namespace Optimize {
FlowGraphNode::FlowGraphNode(int _index, const Asm::Instruction* _instruction,
IR::VirtualRegister *ignored_register)
: index(_index), instruction(_instruction)
{
is_reg_to_reg_assign = instruction->is_reg_to_reg_assign;
for (int i = 0; i < instruction->outputs.size(); i++)
assert(instruction->outputs[i]->getIndex() !=
ignored_register->getIndex());
for (int i = 0; i < instruction->inputs.size(); i++)
if ((ignored_register == NULL) ||
(instruction->inputs[i]->getIndex() !=
ignored_register->getIndex()))
used.push_back(instruction->inputs[i]);
else
is_reg_to_reg_assign = false;
}
const std::vector< IR::VirtualRegister* >& FlowGraphNode::usedRegisters() const
{
return used;
}
const std::vector< IR::VirtualRegister* >& FlowGraphNode::assignedRegisters() const
{
return instruction->outputs;
}
FlowGraph::FlowGraph(const Asm::Instructions &code,
IR::VirtualRegister *ignored_register)
{
std::map<std::string, FlowGraphNode *> label_positions;
nodecount = 0;
FlowGraphNode *newnode = NULL;
for (Asm::Instructions::const_iterator inst = code.begin();
inst != code.end(); inst++) {
nodes.push_back(FlowGraphNode(nodecount, &(*inst), ignored_register));
if ((*inst).label != NULL)
label_positions[(*inst).label->getName()] = & nodes.back();;
nodecount++;
}
last_instruction = newnode;
for (std::list<FlowGraphNode>::iterator node = nodes.begin();
node != nodes.end(); node++) {
FlowGraphNode *current = &(*node);
for (int i = 0; i < current->instruction->destinations.size(); i++) {
const IR::Label *label = current->instruction->destinations[i];
FlowGraphNode *nextnode = NULL;
if (label == NULL) {
std::list<FlowGraphNode>::iterator next_iter = node;
next_iter++;
if (next_iter != nodes.end())
nextnode = &(*next_iter);
} else
nextnode = label_positions.at(label->getName());
if (nextnode != NULL) {
current->next.push_back(& *nextnode);
(*nextnode).previous.push_back(current);
}
}
}
}
}