Last active
July 11, 2021 21:22
-
-
Save nefanov/1f5dba40d96f07ed6fe797dddcec474c to your computer and use it in GitHub Desktop.
gcc_gimple_cfg_dot_thin_bb.py
This file contains 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
import pydot | |
import re | |
import os, sys | |
import copy | |
graphs = pydot.graph_from_dot_file('graph.dot') | |
graph = graphs[0] | |
graph.write_png('src.png') | |
nodes = graph.get_node_list() | |
for n in nodes: | |
print(n.get_name()+" handling:") | |
name = n.get_name() | |
label = n.get_attributes()['label'] | |
label = re.sub('"{|}"','', re.sub(r"\\l", r"\n", label)) | |
bb_labels = re.split(r"\n", label) | |
bb_labels.pop() | |
del bb_labels[1::2] | |
print(bb_labels) | |
# graph transformation | |
if len(bb_labels) > 1: | |
split_nodes_list = [] | |
for idx, l in enumerate(bb_labels): | |
split_nodes_list.append(pydot.Node(n)) | |
split_nodes_list[-1].set_name(name+"_"+str(idx)) | |
split_nodes_list[-1].set('label', l) | |
graph.add_node(split_nodes_list[-1]) | |
# connect internal nodes to chain | |
for idx, nd in enumerate(split_nodes_list): | |
if idx > 0: | |
graph.add_edge(pydot.Edge(split_nodes_list[idx-1], nd)) | |
# connect in_node with predecessors: | |
for e in graph.get_edges(): | |
print(e.get_source()) | |
if e.get_destination().startswith(name+":"): | |
e.__setattr__('label', 'del') | |
graph.add_edge(pydot.Edge(e.get_source(), split_nodes_list[0])) | |
e.get_attributes() | |
graph.del_edge(e.get_source(),e.get_destination()) | |
# connect out_node with descendants: | |
if e.get_source().startswith(name+":"): | |
e.__setattr__('label','del') | |
graph.add_edge(pydot.Edge(split_nodes_list[-1], e.get_destination())) | |
graph.del_edge(e.get_source(), e.get_destination()) | |
# remove old edges | |
graph.del_node(n.get_name()) | |
print(len(graph.get_nodes())) | |
# remove old node | |
graph.write_jpg('res.jpg') | |
graph.write('res1.dot') | |
#print([name.get_name() for name in graph.get_nodes()]) | |
#print(graph.get_edges()) | |
pass |
This file contains 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
digraph "_main" { | |
style="dashed"; | |
color="black"; | |
label="main ()"; | |
fn_1_basic_block_0 [shape=Mdiamond,style=filled,fillcolor=white,label="ENTRY"]; | |
fn_1_basic_block_1 [shape=Mdiamond,style=filled,fillcolor=white,label="EXIT"]; | |
fn_1_basic_block_2 [shape=record,style=filled,fillcolor=lightgrey,label="{\<bb\ 2\>:\l\ | |
|a\ =\ 1;\l\ | |
|b\ =\ 2;\l\ | |
|_1\ =\ a\ +\ b;\l\ | |
|D.2320\ =\ _1;\l\ | |
}"]; | |
fn_1_basic_block_3 [shape=record,style=filled,fillcolor=lightgrey,label="{\<bb\ 3\>:\l\ | |
|\<L0\>:\l\ | |
|return\ D.2320;\l\ | |
}"]; | |
fn_1_basic_block_0:s -> fn_1_basic_block_2:n [style="solid,bold",color=blue,weight=100,constraint=true]; | |
fn_1_basic_block_2:s -> fn_1_basic_block_3:n [style="solid,bold",color=blue,weight=100,constraint=true]; | |
fn_1_basic_block_3:s -> fn_1_basic_block_1:n [style="solid,bold",color=black,weight=10,constraint=true]; | |
fn_1_basic_block_0:s -> fn_1_basic_block_1:n [style="invis",constraint=true]; | |
} |
This file contains 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
style="dashed"; | |
fn_1_basic_block_0 [fillcolor=white, label="ENTRY", shape=Mdiamond, style=filled]; | |
fn_1_basic_block_1 [fillcolor=white, label="EXIT", shape=Mdiamond, style=filled]; | |
fn_1_basic_block_0:s -> fn_1_basic_block_1:n [constraint=true, style="invis"]; | |
fn_1_basic_block_2_0 [label="\<bb\ 2\>:"]; | |
fn_1_basic_block_2_1 [label="|a\ =\ 1;"]; | |
fn_1_basic_block_2_2 [label="|b\ =\ 2;"]; | |
fn_1_basic_block_2_3 [label="|_1\ =\ a\ +\ b;"]; | |
fn_1_basic_block_2_4 [label="|D.2320\ =\ _1;"]; | |
fn_1_basic_block_2_0 -> fn_1_basic_block_2_1; | |
fn_1_basic_block_2_1 -> fn_1_basic_block_2_2; | |
fn_1_basic_block_2_2 -> fn_1_basic_block_2_3; | |
fn_1_basic_block_2_3 -> fn_1_basic_block_2_4; | |
fn_1_basic_block_0:s -> fn_1_basic_block_2_0; | |
fn_1_basic_block_3_0 [label="\<bb\ 3\>:"]; | |
fn_1_basic_block_3_1 [label="|\<L0\>:"]; | |
fn_1_basic_block_3_2 [label="|return\ D.2320;"]; | |
fn_1_basic_block_3_0 -> fn_1_basic_block_3_1; | |
fn_1_basic_block_3_1 -> fn_1_basic_block_3_2; | |
fn_1_basic_block_3_2 -> fn_1_basic_block_1:n; | |
fn_1_basic_block_2_4 -> fn_1_basic_block_3_0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment