Last active
December 26, 2017 21:08
-
-
Save hiroto-takatoshi/6330166e6d35eb439b095e3ce5aa7388 to your computer and use it in GitHub Desktop.
Nasty code to create an interference graph for register assignment. This code produces in[], out[], interference matrix. C++ language. Edit the data by yourselves, folks, if anyone is doing compiler assignments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <memory.h> | |
//using namepsace std; | |
enum Var { m, v, r, s, x }; // Change your variables here | |
char str[10] = "mvrsx"; // And here. | |
int in[20], out[20], def[20], use[20]; | |
int hd[20], nxt[200], to[200], gr; | |
void add(int src, int dst) | |
{ | |
nxt[gr] = hd[src]; | |
to[gr] = dst; | |
hd[src] = gr++; | |
} | |
int get(int &num, int dig) | |
{ | |
return (num >> dig) & 1; | |
} | |
void set(int &num, int dig) | |
{ | |
num |= (1 << dig); | |
} | |
void unset(int &num, int dig) | |
{ | |
num -= (1 << dig); | |
} | |
void init() | |
{ | |
// add(a,b) means sentence a has sentence be as next state. If there is an conditional branch just add both. | |
add(1,2); | |
add(2,3); | |
add(3,4), add(3, 15); | |
add(4,5); | |
add(5,6); | |
add(6,7),add(6,9); | |
add(7,3); | |
add(9,10); | |
add(10,11); | |
add(11,12),add(11,13); | |
add(12,13); | |
add(13,6); | |
// set def, set use means what it writes. | |
set(def[1],m); | |
set(def[2],v); | |
set(def[4],r); | |
set(def[5],s); | |
set(def[7],v); | |
set(def[9],x); | |
set(def[10],s); | |
set(def[12],m); | |
set(def[13],r); | |
set(use[3],v); | |
set(use[4],v); | |
set(use[6],r); | |
set(use[7],v); | |
set(use[9],r); | |
set(use[10],s); | |
set(use[10],x); | |
set(use[11],s); | |
set(use[11],m); | |
set(use[12],s); | |
set(use[13],r); | |
set(use[15],m); | |
} | |
void liveness() | |
{ | |
bool flag = false; | |
int cnt = 0; | |
do { | |
std::cout << ++cnt << std::endl; | |
flag = false; | |
for(int i = 1; i <= 15; ++i) { | |
int _in = in[i], _out = out[i]; | |
int tmp = _out; | |
for(int j = 0; j < 5; ++j) { | |
if(get(_out, j) && get(def[i], j)) | |
unset(tmp, j); | |
} | |
in[i] = ( use[i] | tmp ); | |
tmp = 0; | |
for(int j = hd[i]; ~j; j = nxt[j]) | |
tmp |= in[to[j]]; | |
out[i] = tmp; | |
if(_in != in[i]) flag = true; | |
if(_out != out[i]) flag = true; | |
} | |
} while(flag); | |
for(int i = 1; i <= 15; ++i) { | |
std::cout << i << std::endl; | |
std::cout << "in:"; | |
for(int j = 0; j < 5; ++j) { | |
if(get(in[i], j)) | |
std::cout << str[j]; | |
} | |
std::cout << std::endl; | |
std::cout << "out:"; | |
for(int j = 0; j < 5; ++j) { | |
if(get(out[i], j)) | |
std::cout << str[j]; | |
} | |
std::cout << std::endl; | |
} | |
} | |
int mat[10][10]; | |
void interference() | |
{ | |
// credit: https://www.cs.princeton.edu/courses/archive/spr05/cos320/notes/Register%20Allocation.ppt , Page 3 | |
// Cond. 2 : both on some out[node] && Cond. 3 | |
for(int i = 1; i <= 15; ++i) { | |
for(int a = 0; a < 5; ++a) { | |
for(int b = a + 1; b < 5; ++b) { | |
if(get(out[i], a) && get(out[i], b)) | |
mat[a][b] = 1, mat[b][a] = 1; | |
if(get(def[i], a) && get(out[i], b) || get(def[i], b) && get(out[i], a)) | |
mat[a][b] = 1, mat[b][a] = 1; | |
} | |
} | |
} | |
for(int a = 0; a < 5; ++a) { | |
std::cout << "dep " << str[a] << std::endl; | |
for(int b = a + 1; b < 5; ++b) { | |
if(mat[a][b]) std::cout << str[b]; | |
} | |
std::cout << std::endl; | |
} | |
} | |
int main() | |
{ | |
memset(hd, -1 ,sizeof hd); | |
init(); | |
liveness(); | |
interference(); | |
//std::cout << def[1] << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[Important] in the loops the boundaries needs to be modified too, if you have different number of variables and lines.