1 /* Graph representation and manipulation functions.
2 Copyright (C) 2007-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 /* Dumps graph G into F. */
29 dump_graph (FILE *f
, struct graph
*g
)
34 fprintf (f
, "digraph {\n");
35 for (i
= 0; i
< g
->n_vertices
; i
++)
37 fprintf (f
, "\"%d\" [label=\"%d (%d): %p\"];\n",
38 i
, i
, g
->vertices
[i
].component
, g
->vertices
[i
].data
);
39 for (e
= g
->vertices
[i
].pred
; e
; e
= e
->pred_next
)
40 fprintf (f
, "\"%d\" -> \"%d\" [label=\"%p\"];\n", e
->src
, e
->dest
, e
->data
);
41 for (e
= g
->vertices
[i
].succ
; e
; e
= e
->succ_next
)
42 fprintf (f
, "\"%d\" -> \"%d\";\n", e
->src
, e
->dest
);
47 /* Creates a new graph with N_VERTICES vertices. */
50 new_graph (int n_vertices
)
52 struct graph
*g
= XNEW (struct graph
);
54 gcc_obstack_init (&g
->ob
);
55 g
->n_vertices
= n_vertices
;
56 g
->vertices
= XOBNEWVEC (&g
->ob
, struct vertex
, n_vertices
);
57 memset (g
->vertices
, 0, sizeof (struct vertex
) * n_vertices
);
62 /* Adds an edge from F to T to graph G. The new edge is returned. */
65 add_edge (struct graph
*g
, int f
, int t
)
67 struct graph_edge
*e
= XOBNEW (&g
->ob
, struct graph_edge
);
68 struct vertex
*vf
= &g
->vertices
[f
], *vt
= &g
->vertices
[t
];
73 e
->pred_next
= vt
->pred
;
76 e
->succ_next
= vf
->succ
;
83 /* Moves all the edges incident with U to V. */
86 identify_vertices (struct graph
*g
, int v
, int u
)
88 struct vertex
*vv
= &g
->vertices
[v
];
89 struct vertex
*uu
= &g
->vertices
[u
];
90 struct graph_edge
*e
, *next
;
92 for (e
= uu
->succ
; e
; e
= next
)
97 e
->succ_next
= vv
->succ
;
102 for (e
= uu
->pred
; e
; e
= next
)
107 e
->pred_next
= vv
->pred
;
113 /* Helper function for graphds_dfs. Returns the source vertex of E, in the
114 direction given by FORWARD. */
117 dfs_edge_src (struct graph_edge
*e
, bool forward
)
119 return forward
? e
->src
: e
->dest
;
122 /* Helper function for graphds_dfs. Returns the destination vertex of E, in
123 the direction given by FORWARD. */
126 dfs_edge_dest (struct graph_edge
*e
, bool forward
)
128 return forward
? e
->dest
: e
->src
;
131 /* Helper function for graphds_dfs. Returns the first edge after E (including
132 E), in the graph direction given by FORWARD, that belongs to SUBGRAPH. If
133 SKIP_EDGE_P is not NULL, it points to a callback function. Edge E will be
134 skipped if callback function returns true. */
136 static inline struct graph_edge
*
137 foll_in_subgraph (struct graph_edge
*e
, bool forward
, bitmap subgraph
,
138 skip_edge_callback skip_edge_p
)
145 if (!subgraph
&& (!skip_edge_p
|| !skip_edge_p (e
)))
150 d
= dfs_edge_dest (e
, forward
);
151 /* Return edge if it belongs to subgraph and shouldn't be skipped. */
152 if ((!subgraph
|| bitmap_bit_p (subgraph
, d
))
153 && (!skip_edge_p
|| !skip_edge_p (e
)))
156 e
= forward
? e
->succ_next
: e
->pred_next
;
162 /* Helper function for graphds_dfs. Select the first edge from V in G, in the
163 direction given by FORWARD, that belongs to SUBGRAPH. If SKIP_EDGE_P is not
164 NULL, it points to a callback function. Edge E will be skipped if callback
165 function returns true. */
167 static inline struct graph_edge
*
168 dfs_fst_edge (struct graph
*g
, int v
, bool forward
, bitmap subgraph
,
169 skip_edge_callback skip_edge_p
)
171 struct graph_edge
*e
;
173 e
= (forward
? g
->vertices
[v
].succ
: g
->vertices
[v
].pred
);
174 return foll_in_subgraph (e
, forward
, subgraph
, skip_edge_p
);
177 /* Helper function for graphds_dfs. Returns the next edge after E, in the
178 graph direction given by FORWARD, that belongs to SUBGRAPH. If SKIP_EDGE_P
179 is not NULL, it points to a callback function. Edge E will be skipped if
180 callback function returns true. */
182 static inline struct graph_edge
*
183 dfs_next_edge (struct graph_edge
*e
, bool forward
, bitmap subgraph
,
184 skip_edge_callback skip_edge_p
)
186 return foll_in_subgraph (forward
? e
->succ_next
: e
->pred_next
,
187 forward
, subgraph
, skip_edge_p
);
190 /* Runs dfs search over vertices of G, from NQ vertices in queue QS.
191 The vertices in postorder are stored into QT. If FORWARD is false,
192 backward dfs is run. If SUBGRAPH is not NULL, it specifies the
193 subgraph of G to run DFS on. Returns the number of the components
194 of the graph (number of the restarts of DFS). If SKIP_EDGE_P is not
195 NULL, it points to a callback function. Edge E will be skipped if
196 callback function returns true. */
199 graphds_dfs (struct graph
*g
, int *qs
, int nq
, vec
<int> *qt
,
200 bool forward
, bitmap subgraph
,
201 skip_edge_callback skip_edge_p
)
203 int i
, tick
= 0, v
, comp
= 0, top
;
204 struct graph_edge
*e
;
205 struct graph_edge
**stack
= XNEWVEC (struct graph_edge
*, g
->n_vertices
);
211 EXECUTE_IF_SET_IN_BITMAP (subgraph
, 0, av
, bi
)
213 g
->vertices
[av
].component
= -1;
214 g
->vertices
[av
].post
= -1;
219 for (i
= 0; i
< g
->n_vertices
; i
++)
221 g
->vertices
[i
].component
= -1;
222 g
->vertices
[i
].post
= -1;
226 for (i
= 0; i
< nq
; i
++)
229 if (g
->vertices
[v
].post
!= -1)
232 g
->vertices
[v
].component
= comp
++;
233 e
= dfs_fst_edge (g
, v
, forward
, subgraph
, skip_edge_p
);
240 if (g
->vertices
[dfs_edge_dest (e
, forward
)].component
243 e
= dfs_next_edge (e
, forward
, subgraph
, skip_edge_p
);
250 g
->vertices
[v
].post
= tick
++;
256 v
= dfs_edge_src (e
, forward
);
257 e
= dfs_next_edge (e
, forward
, subgraph
, skip_edge_p
);
262 v
= dfs_edge_dest (e
, forward
);
263 e
= dfs_fst_edge (g
, v
, forward
, subgraph
, skip_edge_p
);
264 g
->vertices
[v
].component
= comp
- 1;
273 /* Determines the strongly connected components of G, using the algorithm of
274 Kosaraju -- first determine the postorder dfs numbering in reversed graph,
275 then run the dfs on the original graph in the order given by decreasing
276 numbers assigned by the previous pass. If SUBGRAPH is not NULL, it
277 specifies the subgraph of G whose strongly connected components we want
278 to determine. If SKIP_EDGE_P is not NULL, it points to a callback function.
279 Edge E will be skipped if callback function returns true. If SCC_GROUPING
280 is not null, the nodes will be added to it in the following order:
282 - If SCC A is a direct or indirect predecessor of SCC B in the SCC dag,
283 A's nodes come before B's nodes.
285 - All of an SCC's nodes are listed consecutively, although the order
286 of the nodes within an SCC is not really meaningful.
288 After running this function, v->component is the number of the strongly
289 connected component for each vertex of G. Returns the number of the
293 graphds_scc (struct graph
*g
, bitmap subgraph
,
294 skip_edge_callback skip_edge_p
, vec
<int> *scc_grouping
)
296 int *queue
= XNEWVEC (int, g
->n_vertices
);
297 vec
<int> postorder
= vNULL
;
305 EXECUTE_IF_SET_IN_BITMAP (subgraph
, 0, v
, bi
)
312 for (i
= 0; i
< g
->n_vertices
; i
++)
317 graphds_dfs (g
, queue
, nq
, &postorder
, false, subgraph
, skip_edge_p
);
318 gcc_assert (postorder
.length () == (unsigned) nq
);
320 for (i
= 0; i
< nq
; i
++)
321 queue
[i
] = postorder
[nq
- i
- 1];
322 comp
= graphds_dfs (g
, queue
, nq
, scc_grouping
, true, subgraph
, skip_edge_p
);
325 postorder
.release ();
330 /* Runs CALLBACK for all edges in G. DATA is private data for CALLBACK. */
333 for_each_edge (struct graph
*g
, graphds_edge_callback callback
, void *data
)
335 struct graph_edge
*e
;
338 for (i
= 0; i
< g
->n_vertices
; i
++)
339 for (e
= g
->vertices
[i
].succ
; e
; e
= e
->succ_next
)
340 callback (g
, e
, data
);
343 /* Releases the memory occupied by G. */
346 free_graph (struct graph
*g
)
348 obstack_free (&g
->ob
, NULL
);
352 /* Returns the nearest common ancestor of X and Y in tree whose parent
353 links are given by PARENT. MARKS is the array used to mark the
354 vertices of the tree, and MARK is the number currently used as a mark. */
357 tree_nca (int x
, int y
, int *parent
, int *marks
, int mark
)
359 if (x
== -1 || x
== y
)
362 /* We climb with X and Y up the tree, marking the visited nodes. When
363 we first arrive to a marked node, it is the common ancestor. */
372 if (marks
[x
] == mark
)
379 if (marks
[y
] == mark
)
384 /* If we reached the root with one of the vertices, continue
385 with the other one till we reach the marked part of the
389 for (y
= parent
[y
]; marks
[y
] != mark
; y
= parent
[y
])
396 for (x
= parent
[x
]; marks
[x
] != mark
; x
= parent
[x
])
403 /* Determines the dominance tree of G (stored in the PARENT, SON and BROTHER
404 arrays), where the entry node is ENTRY. */
407 graphds_domtree (struct graph
*g
, int entry
,
408 int *parent
, int *son
, int *brother
)
410 vec
<int> postorder
= vNULL
;
411 int *marks
= XCNEWVEC (int, g
->n_vertices
);
412 int mark
= 1, i
, v
, idom
;
414 struct graph_edge
*e
;
416 /* We use a slight modification of the standard iterative algorithm, as
419 K. D. Cooper, T. J. Harvey and K. Kennedy: A Simple, Fast Dominance
422 sort vertices in reverse postorder
427 while (anything changes)
429 dom(v) = {v} union (intersection of dom(p) over all predecessors of v)
431 The sets dom(v) are represented by the parent links in the current version
432 of the dominance tree. */
434 for (i
= 0; i
< g
->n_vertices
; i
++)
440 graphds_dfs (g
, &entry
, 1, &postorder
, true, NULL
);
441 gcc_assert (postorder
.length () == (unsigned) g
->n_vertices
);
442 gcc_assert (postorder
[g
->n_vertices
- 1] == entry
);
448 for (i
= g
->n_vertices
- 2; i
>= 0; i
--)
452 for (e
= g
->vertices
[v
].pred
; e
; e
= e
->pred_next
)
455 && parent
[e
->src
] == -1)
458 idom
= tree_nca (idom
, e
->src
, parent
, marks
, mark
++);
461 if (idom
!= parent
[v
])
470 postorder
.release ();
472 for (i
= 0; i
< g
->n_vertices
; i
++)
475 brother
[i
] = son
[parent
[i
]];