summaryrefslogtreecommitdiff
path: root/Source/Core/Graph.as
blob: 1466c341787aee87017184c62926d1bb5342b93f (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System.Collections;
namespace Graphing;

type Node = object;
type Edge = <Node,Node>;

class PreHeader {
  Node myHeader;
  PreHeader(Node h) { myHeader = h; }
  
  public override string ToString() { return "#" + myHeader.ToString(); }
}

public class Graph {
  private Set<Edge> es;
  private Set<Node> ns;
  private Node source;
  private bool reducible;
  private Set<Node> headers;
  private Map<Node,Set<Node>> backEdgeNodes;
  private Map<Edge,Set<Node>> naturalLoops;
  private Map<Node,Set<Node>> dominatorMap;
  private Map<Node,Set<Node>> immediateDominatorMap;
 
  public Graph(Set<Edge> edges)
  {
    es = edges;
    ns = Set<Node>{ x : <x,y> in es } + Set<Node>{ y : <x,y> in es };
  }
  public Graph()
  { es = Set<Edge>{}; ns = Set<Node>{}; } 
  
  public void AddSource(Node x)
  {
    ns += Set<Node>{x};
    source = x;
  }
  public void AddEdge(Node source, Node dest)
  {
    es += Set<Edge>{<source,dest>};
    ns += Set<Node>{source, dest};
  }
  
  public Set<Node> Nodes { get { return ns; } }
  public Set<Edge> Edges { get { return es; } }
  
  public bool Edge(Node x, Node y) { return <x,y> in es; }  
  Set<Node> predecessors(Node n)
  {
    Set<Node> result = Set{ x : x in Nodes, Edge(x,n) };
    return result;
  }
  public override string ToString() { return es.ToString(); }
  
  public IEnumerable TopologicalSort()
  {
    <bool,Seq<Node>> <res,ns> = TopSort(this);
    return  res ? ns : null;
  }
  public void ComputeLoops()
  {
     <bool, Set<Node>, Map<Node,Set<Node>>, Map<Edge,Set<Node>>>
       <reducible,headers,backEdgeNodes,naturalLoops> = Reducible(this,this.source);
       this.reducible = reducible;
       this.headers = headers;
       this.backEdgeNodes = backEdgeNodes;
       this.naturalLoops = naturalLoops;
     return;
  }
  public bool Reducible { get { return reducible; } }
  public IEnumerable Headers { get { return headers; } }
  public IEnumerable BackEdgeNodes(Node h) { return h in backEdgeNodes ? backEdgeNodes[h] : null; }
  public IEnumerable NaturalLoops(Node header, Node backEdgeNode)
  {  Edge e = <backEdgeNode,header>; return e in naturalLoops ? naturalLoops[e] : null; }
  public bool Acyclic { get { return Acyclic(this,this.source); } }
  public Map<Node,Set<Node>> DominatorMap
  {
    get { 
      if (dominatorMap == null) dominatorMap = ComputeDominators(this, source);
      return dominatorMap;
    }
  }
  public Map<Node,Set<Node>> ImmediateDominatorMap
  {
    get { 
      if (immediateDominatorMap == null)
      {
        immediateDominatorMap = Map{};
        foreach(Node y in Nodes)
        {
          Set<Node> nodesThatYDominates = Set{ x : x in Nodes, x != y && (y in DominatorMap[x]) };
          Set<Node> immediateDominatees = Set{ x : x in nodesThatYDominates,
                                                  !(Exists{ v != y && v != x && (v in DominatorMap[x]) : v in nodesThatYDominates })
                                               };
          immediateDominatorMap[y] = immediateDominatees;
        }
      }
      return immediateDominatorMap;
    }
  }
  public Set<Node> ImmediatelyDominatedBy(Node n) { return ImmediateDominatorMap[n]; }

}

// From AsmL distribution example: TopologicalSort
<bool,Seq<Node>> TopSort(Graph g)
{
  Seq<Node> S = Seq{};
  Set<Node> V = g.Nodes;
  bool change = true;
  while ( change )
  {
    change = false;
    Set<Node> X = V - ((Set<Node>) S);
    if ( X != Set{} )
    {
      Node temp = Choose{ v : v in X, !(Exists{ g.Edge(u,v) : u in X }) ifnone null };
      if ( temp == null )
      {
        return <false,Seq<Node>{}>;
      }
      else if ( temp != Seq<Node>{} )
      {
        S += Seq{temp};
        change = true;
      }
    }
  }
  return <true,S>;
}

bool Acyclic(Graph g, Node source)
{
  <bool,Seq<Node>> <acyc,xs> = TopSort(g);
  return acyc;
}

//
// [Dragon, pp. 670--671]
// returns map D s.t. d in D(n) iff d dom n
//
Map<Node,Set<Node>> ComputeDominators(Graph g, Node source) {
  Set<Node> N = g.Nodes;
  Set<Node> nonSourceNodes = N - Set{source};
  Map<Node,Set<Node>> D = Map{};
  D[source] = Set<Node>{ source };
  foreach (Node n in nonSourceNodes)
  {
    D[n] = N;
  }
  bool change = true;
  while ( change )
  {
    change = false;
    foreach (Node n in nonSourceNodes)
    {
      Set<Set<Node>> allPreds = Set{ D[p] : p in g.predecessors(n) };
      Set<Node> temp = Set<Node>{ n } + BigIntersect(allPreds);
      if ( temp != D[n] )
      {
        change = true;
        D[n] = temp;
      }
    }
  }
  return D;
}

// [Dragon, Fig. 10.15, p. 604. Algorithm for constructing the natural loop.]
Set<Node> NaturalLoop(Graph g, Edge backEdge)
{
  <Node,Node> <n,d> = backEdge;
  Seq<Node> stack = Seq{};
  Set<Node> loop = Set{ d };
  if ( n != d ) // then n is not in loop
  {
    loop += Set{ n };
    stack = Seq{ n } + stack; // push n onto stack
  }
  while ( stack != Seq{} ) // not empty
  {
    Node m = Head(stack);
    stack = Tail(stack); // pop stack
    foreach (Node p in g.predecessors(m))
    {
      if ( !(p in loop) )
      {
        loop += Set{ p };
        stack = Seq{ p } + stack; // push p onto stack
      }
    }
  }
  return loop;
}

// [Dragon, p. 606]
<bool, Set<Node>, Map<Node,Set<Node>>, Map<Edge,Set<Node>>>
  Reducible(Graph g, Node source) {
  // first, compute the dom relation
  Map<Node,Set<Node>> D = g.DominatorMap;
  return Reducible(g,source,D);
}

// [Dragon, p. 606]
<bool, Set<Node>, Map<Node,Set<Node>>, Map<Edge,Set<Node>>>
  Reducible(Graph g, Node source, Map<Node,Set<Node>> DomRelation) {

  Set<Edge> edges = g.Edges;
  Set<Edge> backEdges = Set{};
  Set<Edge> nonBackEdges = Set{};
  foreach (Edge e in edges)
  {
    <Node,Node> <x,y> = e; // so there is an edge from x to y
    if ( y in DomRelation[x] ) // y dom x: which means y dominates x
    {
      backEdges += Set{ e };
    }
    else
    {
      nonBackEdges += Set{ e };
    }
  }
  if ( !Acyclic(new Graph(nonBackEdges), source) )
  {
    return <false,Set<Node>{},Map<Node,Set<Node>>{},Map<Edge,Set<Node>>{}>;
  }
  else
  {
    Set<Node> headers = Set{ d : <n,d> in backEdges };
    Map<Node,Set<Node>> backEdgeNodes = Map{ h -> bs  : h in headers, bs = Set<Node>{ b : <b,x> in backEdges, x == h } };
    Map<Edge,Set<Node>> naturalLoops = Map{ e -> NaturalLoop(g,e) : e in backEdges };
     
    return <true, headers, backEdgeNodes, naturalLoops>;
  }
}
  
// [Dragon, p. 606]
bool OldReducible(Graph g, Node source) {
  // first, compute the dom relation
  Map<Node,Set<Node>> D = ComputeDominators(g, source);
  return OldReducible(g,source,D);
}

// [Dragon, p. 606]
bool OldReducible(Graph g, Node source, Map<Node,Set<Node>> DomRelation) {

  Set<Edge> edges = g.Edges;
  Set<Edge> backEdges = Set{};
  Set<Edge> nonBackEdges = Set{};
  foreach (Edge e in edges)
  {
    <Node,Node> <x,y> = e;
    if ( y in DomRelation[x] ) // y dom x
    {
      backEdges += Set{ e };
    }
    else
    {
      nonBackEdges += Set{ e };
    }
  }
  WriteLine("backEdges: " + backEdges);
  WriteLine("nonBackEdges: " + nonBackEdges);
  if ( Acyclic(new Graph(nonBackEdges), source) )
  {
    foreach(Edge e in backEdges)
    {
      Set<Node> naturalLoop = NaturalLoop(g,e);
      WriteLine("Natural loop for back edge '" + e + "' is: " + naturalLoop);
    }
    Set<Node> headers = Set{ d : <n,d> in backEdges };
    WriteLine("Loop headers = " + headers);
     
    edges -= backEdges; // this cuts all of the back edges
    foreach (Node h in headers)
    {
      Set<Edge> bs = Set{ <n,d> : <n,d> in backEdges, d == h };
      Set<Node> preds = Set<Node>{ p : <p,y> in edges, y == h };
      Node preheader = new PreHeader(h);
      edges += Set{ <preheader,h> };
      foreach (Node p in preds)
      {
        edges -= Set{ <p,h> };
        edges += Set{ <p,preheader> };
      }
    }
    Graph newGraph = new Graph(edges);
    WriteLine("transformed graph = " + newGraph);
    return true;
  }
  else
  {
    return false;
  }
}

void Main()
{
   Graph g;
   Map<Node,Set<Node>> D;
/*   
   g = new Graph(Set<Edge>{ <1,2>, <1,3>, <2,3> });
   g.AddSource(1);
  Map<Node,Set<Node>> doms = ComputeDominators(g,1);
   WriteLine(doms); 
*/
   g = new Graph(Set<Edge>{
          <1,2>, <1,3>,
          <2,3>,
          <3,4>,
          <4,3>, <4,5>, <4,6>,
          <5,7>,
          <6,7>,
          <7,4>, <7,8>,
          <8,3>, <8,9>, <8,10>,
          <9,1>,
          <10,7>
          });
   g.AddSource(1);
   WriteLine("G = " + g);
   D = ComputeDominators(g,1);
   WriteLine("Dom relation: " + D); 
   WriteLine("G's Dominator Map = " + g.DominatorMap);
   WriteLine("G's Immediate Dominator Map = " + g.ImmediateDominatorMap);
   WriteLine("G is reducible: " + OldReducible(g,1,D));
   g.ComputeLoops();
   
   WriteLine("");
   
   g = new Graph(Set<Edge>{ <1,2>, <1,3>, <2,3>, <3,2> });
   g.AddSource(1);
   WriteLine("G = " + g);
   D = ComputeDominators(g,1);
   WriteLine("Dom relation: " + D); 
   WriteLine("G's Dominator Map = " + g.DominatorMap);
   WriteLine("G's Immediate Dominator Map = " + g.ImmediateDominatorMap);
   WriteLine("G is reducible: " + OldReducible(g,1,D));
   g.ComputeLoops();
 
    WriteLine("");
   
   g = new Graph(Set<Edge>{ <1,2>, <2,3>, <2,4>, <3,2> });
   g.AddSource(1);
   WriteLine("G = " + g);
   WriteLine("G's Dominator Map = " + g.DominatorMap);
   WriteLine("G's Immediate Dominator Map = " + g.ImmediateDominatorMap);
//   D = ComputeDominators(g,1);
//   WriteLine("Dom relation: " + D); 
//   WriteLine("G is reducible: " + OldReducible(g,1,D));
   g.ComputeLoops();
  
}