clfaster
clfaster

Reputation: 1824

Draw a program dependence graph with graphviz

I'm trying to draw a PDG, but when I add the data dependencies it gets malformed.

What I have

When I only draw the control dependencies the graph looks fine:

digraph {
4[label="4. int x=1"];
5[label="5. int y=2"];
6[label="6. while(x>0)"];
8[label="8. x=(y+x)"];
10[label="10. z=x+y"];
ENTRY -> 4[rank=same, splines=line];
ENTRY -> 5[rank=same, splines=line];
ENTRY -> 6[rank=same, splines=line];
ENTRY -> 10[rank=same, splines=line];
6 -> 8[splines=line];
}

Only Control dependencies

When I try to add the data dependencies the graph gets malformed:

digraph {
4[label="4. int x=1"];
5[label="5. int y=2"];
6[label="6. while(x>0)"];
8[label="8. x=(y+x)"];
10[label="10. z=x+y"];
ENTRY -> 4[rank=same, splines=line];
ENTRY -> 5[rank=same, splines=line];
ENTRY -> 6[rank=same, splines=line];
ENTRY -> 10[rank=same, splines=line];
6 -> 8[splines=line];
4 -> 6[style=dashed, splines=curved, color=red];
8 -> 6[style=dashed, splines=curved, color=red];
4 -> 8[style=dashed, splines=curved, color=red];
5 -> 8[style=dashed, splines=curved, color=red];
4 -> 10[style=dashed, splines=curved, color=red];
5 -> 10[style=dashed, splines=curved, color=red];
8 -> 10[style=dashed, splines=curved, color=red];
}

With data dependencies

I tried to add the attribute "splines=line" to draw straight lines (control dep.), but it doesnt't worked like expected. I also experimented with the attribute "weight" and "rank"...

Can someone give me a hint? Is it possible to set an order for the nodes? Like: Entry = first row and first element Node 4 = second row and first element ... Node 8 = third row and first element

Expected

Expected

Upvotes: 1

Views: 1045

Answers (1)

vaettchen
vaettchen

Reputation: 7659

Using rank = same properly, plus invisible edges to keep the order of the nodes in the middle should help:

digraph so
{
    splines=true;

    4[label="4. int x=1"];
    5[label="5. int y=2"];
    6[label="6. while(x>0)"];
    8[label="8. x=(y+x)"];
    10[label="10. z=x+y"];

    { rank = same; 4 5 6 10 }
    ENTRY -> { 4 5 6 10 }
    6 -> 8;

    edge[style=dashed, color=red];
    { 4 8 } -> 6;
    { 4 5 } -> 8;
    { 4 5 8 } -> 10;
    // keep graphViz from re-ordering these nodes:
    4 -> 5 -> 6 -> 10[ style = invis ];
}

yields

enter image description here

Upvotes: 1

Related Questions