3 Copyright (C) 2002, 2004-2005, 2009-2015, 2018-2021 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 relation_print (const char *title
,
31 relation r
, relation_node size
,
32 relation_node_print print
, FILE *out
)
35 fprintf (out
, "%s:\n", title
);
36 for (relation_node i
= 0; i
< size
; ++i
)
43 fprintf (out
, "%3ld", (long) i
);
45 for (relation_node j
= 0; r
[i
][j
] != END_NODE
; ++j
)
51 fprintf (out
, "%3ld", (long) r
[i
][j
]);
59 /*---------------------------------------------------------------.
60 | digraph & traverse. |
62 | The following variables are used as common storage between the |
64 `---------------------------------------------------------------*/
67 static relation_nodes indexes
;
68 static relation_nodes vertices
;
69 static relation_node top
;
70 static relation_node infinity
;
74 traverse (relation_node i
)
77 relation_node height
= indexes
[i
] = top
;
80 for (relation_node j
= 0; R
[i
][j
] != END_NODE
; ++j
)
82 if (indexes
[R
[i
][j
]] == 0)
85 if (indexes
[i
] > indexes
[R
[i
][j
]])
86 indexes
[i
] = indexes
[R
[i
][j
]];
88 bitset_or (F
[i
], F
[i
], F
[R
[i
][j
]]);
91 if (indexes
[i
] == height
)
94 relation_node j
= vertices
[top
--];
95 indexes
[j
] = infinity
;
100 bitset_copy (F
[j
], F
[i
]);
106 relation_digraph (relation r
, relation_node size
, bitsetv function
)
109 indexes
= xcalloc (size
+ 1, sizeof *indexes
);
110 vertices
= xnmalloc (size
+ 1, sizeof *vertices
);
116 for (relation_node i
= 0; i
< size
; i
++)
117 if (indexes
[i
] == 0 && R
[i
])
127 /*-------------------------------------------.
128 | Destructively transpose R_ARG, of size N. |
129 `-------------------------------------------*/
132 relation_transpose (relation
*R_arg
, relation_node size
)
136 if (trace_flag
& trace_sets
)
137 relation_print ("relation_transpose", r
, size
, NULL
, stderr
);
140 /* NEDGES[I] -- total size of NEW_R[I]. */
141 size_t *nedges
= xcalloc (size
, sizeof *nedges
);
142 for (relation_node i
= 0; i
< size
; i
++)
144 for (relation_node j
= 0; r
[i
][j
] != END_NODE
; ++j
)
149 relation new_R
= xnmalloc (size
, sizeof *new_R
);
150 /* END_R[I] -- next entry of NEW_R[I]. */
151 relation end_R
= xnmalloc (size
, sizeof *end_R
);
152 for (relation_node i
= 0; i
< size
; i
++)
154 relation_node
*sp
= NULL
;
157 sp
= xnmalloc (nedges
[i
] + 1, sizeof *sp
);
158 sp
[nedges
[i
]] = END_NODE
;
165 for (relation_node i
= 0; i
< size
; i
++)
167 for (relation_node j
= 0; r
[i
][j
] != END_NODE
; ++j
)
168 *end_R
[r
[i
][j
]]++ = i
;
173 /* Free the input: it is replaced with the result. */
174 for (relation_node i
= 0; i
< size
; i
++)
178 if (trace_flag
& trace_sets
)
179 relation_print ("relation_transpose: output", new_R
, size
, NULL
, stderr
);