-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifg.c
216 lines (181 loc) · 6.56 KB
/
ifg.c
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct InterferenceGraph {
char *variable;
struct InterferenceGraph **adjacentNodes;
int numAdjacentNodes;
int color;
bool isPrecolored;
} InterferenceGraph;
InterferenceGraph *createInterferenceGraphNode(char *variable,
bool isPrecolored) {
InterferenceGraph *node =
(InterferenceGraph *)malloc(sizeof(InterferenceGraph));
node->variable = variable;
node->adjacentNodes = NULL;
node->numAdjacentNodes = 0;
node->color = -1;
node->isPrecolored = isPrecolored;
return node;
}
void addInterference(InterferenceGraph *node1, InterferenceGraph *node2) {
node1->adjacentNodes = (InterferenceGraph **)realloc(
node1->adjacentNodes,
(node1->numAdjacentNodes + 1) * sizeof(InterferenceGraph *));
node1->adjacentNodes[node1->numAdjacentNodes] = node2;
node1->numAdjacentNodes++;
node2->adjacentNodes = (InterferenceGraph **)realloc(
node2->adjacentNodes,
(node2->numAdjacentNodes + 1) * sizeof(InterferenceGraph *));
node2->adjacentNodes[node2->numAdjacentNodes] = node1;
node2->numAdjacentNodes++;
}
void removeInterference(InterferenceGraph *node1, InterferenceGraph *node2) {
for (int i = 0; i < node1->numAdjacentNodes; i++) {
if (node1->adjacentNodes[i] == node2) {
for (int j = i; j < node1->numAdjacentNodes - 1; j++) {
node1->adjacentNodes[j] = node1->adjacentNodes[j + 1];
}
node1->numAdjacentNodes--;
break;
}
}
for (int i = 0; i < node2->numAdjacentNodes; i++) {
if (node2->adjacentNodes[i] == node1) {
for (int j = i; j < node2->numAdjacentNodes - 1; j++) {
node2->adjacentNodes[j] = node2->adjacentNodes[j + 1];
}
node2->numAdjacentNodes--;
break;
}
}
}
bool canCoalesce(InterferenceGraph *node1, InterferenceGraph *node2,
int numRegisters) {
return (!node1->isPrecolored && !node2->isPrecolored &&
node1->numAdjacentNodes + node2->numAdjacentNodes < numRegisters &&
!areNodesAdjacent(node1, node2) &&
!areNodesInterfering(node1, node2));
}
void coalesceNodes(InterferenceGraph *node1, InterferenceGraph *node2) {
for (int i = 0; i < node2->numAdjacentNodes; i++) {
InterferenceGraph *adjacentNode = node2->adjacentNodes[i];
removeInterference(node1, adjacentNode);
removeInterference(adjacentNode, node1);
addInterference(node1, adjacentNode);
addInterference(adjacentNode, node1);
}
}
int getNextAvailableColor(InterferenceGraph *node, int numRegisters) {
int *colorUsed = (int *)calloc(numRegisters, sizeof(int));
for (int i = 0; i < node->numAdjacentNodes; i++) {
InterferenceGraph *adjacentNode = node->adjacentNodes[i];
if (adjacentNode->color != -1) {
colorUsed[adjacentNode->color] = 1;
}
}
int nextAvailableColor = 0;
while (colorUsed[nextAvailableColor]) {
nextAvailableColor++;
}
free(colorUsed);
return nextAvailableColor;
}
void graphColoring(InterferenceGraph *interferenceGraph, int numRegisters) {
while (interferenceGraph) {
InterferenceGraph *currentNode = interferenceGraph;
while (currentNode && currentNode->color != -1) {
currentNode = currentNode->adjacentNodes[0];
}
if (!currentNode) {
break;
}
currentNode->color = getNextAvailableColor(currentNode, numRegisters);
for (int i = 0; i < currentNode->numAdjacentNodes; i++) {
InterferenceGraph *adjacentNode = currentNode->adjacentNodes[i];
if (adjacentNode->color == -1) {
adjacentNode->color = getNextAvailableColor(adjacentNode, numRegisters);
}
}
}
}
bool needsSpill(InterferenceGraph *node, int numRegisters) {
return (node->color == -1 && node->numAdjacentNodes >= numRegisters);
}
InterferenceGraph *spillChecking(InterferenceGraph *interferenceGraph,
int numRegisters) {
InterferenceGraph *currentNode = interferenceGraph;
InterferenceGraph *nextNode = NULL;
while (currentNode) {
if (needsSpill(currentNode, numRegisters)) {
currentNode->color = -2;
}
nextNode = currentNode->adjacentNodes[0];
currentNode = nextNode;
}
return nextNode;
}
void graphColoringWithSpill(InterferenceGraph *interferenceGraph,
int numRegisters) {
while (interferenceGraph) {
InterferenceGraph *currentNode = interferenceGraph;
while (currentNode && currentNode->color != -1) {
currentNode = currentNode->adjacentNodes[0];
}
if (!currentNode) {
break;
}
currentNode->color = getNextAvailableColor(currentNode, numRegisters);
for (int i = 0; i < currentNode->numAdjacentNodes; i++) {
InterferenceGraph *adjacentNode = currentNode->adjacentNodes[i];
if (adjacentNode->color == -1) {
adjacentNode->color = getNextAvailableColor(adjacentNode, numRegisters);
}
}
void coalesceAndColor(InterferenceGraph * interferenceGraph,
int numRegisters) {
while (interferenceGraph) {
InterferenceGraph *currentNode = interferenceGraph;
while (currentNode) {
for (int i = 0; i < currentNode->numAdjacentNodes; i++) {
InterferenceGraph *adjacentNode = currentNode->adjacentNodes[i];
if (canCoalesce(currentNode, adjacentNode, numRegisters)) {
coalesceNodes(currentNode, adjacentNode);
currentNode = interferenceGraph;
break;
}
}
currentNode = currentNode->adjacentNodes[0];
}
}
graphColoring(interferenceGraph, numRegisters);
}
bool areNodesAdjacent(InterferenceGraph * node1,
InterferenceGraph * node2) {
for (int i = ; i < node1->numAdjacentNodes; i++) {
if (node1->adjacentNodes[i] == node2) {
return true;
}
}
return false;
}
bool areNodesInterfering(InterferenceGraph * node1,
InterferenceGraph * node2) {
return (node1->color != -1 && node2->color != -1 &&
node1->color == node2->color);
}
bool areNodesAdjacent(InterferenceGraph * node1,
InterferenceGraph * node2) {
for (int i = 0; i < node1->numAdjacentNodes; i++) {
if (node1->adjacentNodes[i] == node2) {
return true;
}
}
return false;
}
bool areNodesInterfering(InterferenceGraph * node1,
InterferenceGraph * node2) {
return (node1->color != -1 && node2->color != -1 &&
node1->color == node2->color);
}