* config/frv/frv.c (frv_ifcvt_modify_insn): Don't leave alone
[official-gcc.git] / gcc / et-forest.c
blobffdce1d76bd75c3dc019f8cbfa6e945a05d00489
1 /* ET-trees data structure implementation.
2 Contributed by Pavel Nejedly
3 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 The ET-forest structure is described in:
22 D. D. Sleator and R. E. Tarjan. A data structure for dynamic trees.
23 J. G'omput. System Sci., 26(3):362 381, 1983.
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "et-forest.h"
31 #include "alloc-pool.h"
33 struct et_forest_occurrence;
34 typedef struct et_forest_occurrence* et_forest_occurrence_t;
36 /* The ET-forest type. */
37 struct et_forest
39 /* Linked list of nodes is used to destroy the structure. */
40 int nnodes;
41 alloc_pool node_pool;
42 alloc_pool occur_pool;
45 /* Single occurrence of node in ET-forest.
46 A single node may have multiple occurrences.
48 struct et_forest_occurrence
50 /* Parent in the splay-tree. */
51 et_forest_occurrence_t parent;
53 /* Children in the splay-tree. */
54 et_forest_occurrence_t left, right;
56 /* Counts of vertices in the two splay-subtrees. */
57 int count_left, count_right;
59 /* Next occurrence of this node in the sequence. */
60 et_forest_occurrence_t next;
62 /* The node, which this occurrence is of. */
63 et_forest_node_t node;
67 /* ET-forest node. */
68 struct et_forest_node
70 et_forest_t forest;
71 void *value;
73 /* First and last occurrence of this node in the sequence. */
74 et_forest_occurrence_t first, last;
78 static et_forest_occurrence_t splay (et_forest_occurrence_t);
79 static void remove_all_occurrences (et_forest_t, et_forest_node_t);
80 static inline et_forest_occurrence_t find_leftmost_node
81 (et_forest_occurrence_t);
82 static inline et_forest_occurrence_t find_rightmost_node
83 (et_forest_occurrence_t);
84 static int calculate_value (et_forest_occurrence_t);
86 /* Return leftmost node present in the tree roted by OCC. */
87 static inline et_forest_occurrence_t
88 find_leftmost_node (et_forest_occurrence_t occ)
90 while (occ->left)
91 occ = occ->left;
93 return occ;
96 /* Return rightmost node present in the tree roted by OCC. */
97 static inline et_forest_occurrence_t
98 find_rightmost_node (et_forest_occurrence_t occ)
100 while (occ->right)
101 occ = occ->right;
102 return occ;
106 /* Operation splay for splay tree structure representing occurrences. */
107 static et_forest_occurrence_t
108 splay (et_forest_occurrence_t node)
110 et_forest_occurrence_t parent;
111 et_forest_occurrence_t grandparent;
113 while (1)
115 parent = node->parent;
117 if (! parent)
118 return node; /* node == root. */
120 grandparent = parent->parent;
122 if (! grandparent)
123 break;
125 /* Now there are four possible combinations: */
127 if (node == parent->left)
129 if (parent == grandparent->left)
131 et_forest_occurrence_t node1, node2;
132 int count1, count2;
134 node1 = node->right;
135 count1 = node->count_right;
136 node2 = parent->right;
137 count2 = parent->count_right;
139 grandparent->left = node2;
140 grandparent->count_left = count2;
141 if (node2)
142 node2->parent = grandparent;
143 parent->left = node1;
144 parent->count_left = count1;
145 if (node1)
146 node1->parent = parent;
147 parent->right = grandparent;
148 parent->count_right = count2 + grandparent->count_right + 1;
149 node->right = parent;
150 node->count_right = count1 + parent->count_right + 1;
152 node->parent = grandparent->parent;
153 parent->parent = node;
154 grandparent->parent = parent;
156 if (node->parent)
158 if (node->parent->left == grandparent)
159 node->parent->left = node;
160 else
161 node->parent->right = node;
164 else
166 /* parent == grandparent->right && node == parent->left*/
167 et_forest_occurrence_t node1, node2;
168 int count1, count2;
170 node1 = node->left;
171 count1 = node->count_left;
172 node2 = node->right;
173 count2 = node->count_right;
175 grandparent->right = node1;
176 grandparent->count_right = count1;
177 if (node1)
178 node1->parent = grandparent;
179 parent->left = node2;
180 parent->count_left = count2;
181 if (node2)
182 node2->parent = parent;
183 node->left = grandparent;
184 node->count_left = grandparent->count_left + count1 + 1;
185 node->right = parent;
186 node->count_right = parent->count_right + count2 + 1;
188 node->parent = grandparent->parent;
189 parent->parent = node;
190 grandparent->parent = node;
192 if (node->parent)
194 if (node->parent->left == grandparent)
195 node->parent->left = node;
196 else
197 node->parent->right = node;
201 else
203 /* node == parent->right. */
204 if (parent == grandparent->left)
206 et_forest_occurrence_t node1, node2;
207 int count1, count2;
209 node1 = node->left;
210 count1 = node->count_left;
211 node2 = node->right;
212 count2 = node->count_right;
214 parent->right = node1;
215 parent->count_right = count1;
216 if (node1)
217 node1->parent = parent;
218 grandparent->left = node2;
219 grandparent->count_left = count2;
220 if (node2)
221 node2->parent = grandparent;
222 node->left = parent;
223 node->count_left = parent->count_left + count1 + 1;
224 node->right = grandparent;
225 node->count_right = grandparent->count_right + count2 + 1;
227 node->parent = grandparent->parent;
228 parent->parent = node;
229 grandparent->parent = node;
231 if (node->parent)
233 if (node->parent->left == grandparent)
234 node->parent->left = node;
235 else
236 node->parent->right = node;
239 else
241 /* parent == grandparent->right && node == parent->right*/
242 et_forest_occurrence_t node1, node2;
243 int count1, count2;
245 node1 = node->left;
246 count1 = node->count_left;
247 node2 = parent->left;
248 count2 = parent->count_left;
250 grandparent->right = node2;
251 grandparent->count_right = count2;
252 if (node2)
253 node2->parent = grandparent;
254 parent->right = node1;
255 parent->count_right = count1;
256 if (node1)
257 node1->parent = parent;
258 parent->left = grandparent;
259 parent->count_left = count2 + grandparent->count_left + 1;
260 node->left = parent;
261 node->count_left = count1 + parent->count_left + 1;
263 node->parent = grandparent->parent;
264 parent->parent = node;
265 grandparent->parent = parent;
267 if (node->parent)
269 if (node->parent->left == grandparent)
270 node->parent->left = node;
271 else
272 node->parent->right = node;
279 /* parent == root. */
280 /* There are two possible combinations: */
282 if (node == parent->left)
284 et_forest_occurrence_t node1;
285 int count1;
287 node1 = node->right;
288 count1 = node->count_right;
290 parent->left = node1;
291 parent->count_left = count1;
292 if (node1)
293 node1->parent = parent;
294 node->right = parent;
295 node->count_right = parent->count_right + 1 + count1;
296 node->parent = parent->parent; /* the same as = 0; */
297 parent->parent = node;
299 if (node->parent)
301 if (node->parent->left == parent)
302 node->parent->left = node;
303 else
304 node->parent->right = node;
307 else
309 /* node == parent->right. */
310 et_forest_occurrence_t node1;
311 int count1;
313 node1 = node->left;
314 count1 = node->count_left;
316 parent->right = node1;
317 parent->count_right = count1;
318 if (node1)
319 node1->parent = parent;
320 node->left = parent;
321 node->count_left = parent->count_left + 1 + count1;
322 node->parent = parent->parent; /* the same as = 0; */
323 parent->parent = node;
325 if (node->parent)
327 if (node->parent->left == parent)
328 node->parent->left = node;
329 else
330 node->parent->right = node;
334 return node;
337 /* Remove all occurrences of the given node before destroying the node. */
338 static void
339 remove_all_occurrences (et_forest_t forest, et_forest_node_t forest_node)
341 et_forest_occurrence_t first = forest_node->first;
342 et_forest_occurrence_t last = forest_node->last;
343 et_forest_occurrence_t node;
345 splay (first);
347 if (first->left)
348 first->left->parent = 0;
349 if (first->right)
350 first->right->parent = 0;
352 if (last != first)
354 splay (last);
356 if (last->left)
357 last->left->parent = 0;
358 if (last->right)
359 last->right->parent = 0;
362 if (last->right && first->left) /* actually, first->left would suffice. */
364 /* Need to join them. */
365 et_forest_occurrence_t prev_node, next_node;
367 prev_node = splay (find_rightmost_node (first->left));
368 next_node = splay (find_leftmost_node (last->right));
369 /* prev_node and next_node are consecutive occurrences
370 of the same node. */
371 if (prev_node->next != next_node)
372 abort ();
374 prev_node->right = next_node->right;
375 prev_node->count_right = next_node->count_right;
376 prev_node->next = next_node->next;
377 if (prev_node->right)
378 prev_node->right->parent = prev_node;
380 if (prev_node->node->last == next_node)
381 prev_node->node->last = prev_node;
383 pool_free (forest->occur_pool, next_node);
386 if (first != last)
388 node = first->next;
390 while (node != last)
392 et_forest_occurrence_t next_node;
394 splay (node);
396 if (node->left)
397 node->left->parent = 0;
398 if (node->right)
399 node->right->parent = 0;
401 next_node = node->next;
402 pool_free (forest->occur_pool, node);
403 node = next_node;
407 pool_free (forest->occur_pool, first);
408 if (first != last)
409 pool_free (forest->occur_pool, last);
412 /* Calculate ET value of the given node. */
413 static inline int
414 calculate_value (et_forest_occurrence_t node)
416 int value = node->count_left;
418 while (node->parent)
420 if (node == node->parent->right)
421 value += node->parent->count_left + 1;
423 node = node->parent;
426 return value;
432 /* Create ET-forest structure. */
433 et_forest_t
434 et_forest_create (void)
436 et_forest_t forest = xmalloc (sizeof (struct et_forest));
438 forest->nnodes = 0;
439 forest->occur_pool = create_alloc_pool ("et_forest_occurrence pool", sizeof (struct et_forest_occurrence), 300);
440 forest->node_pool = create_alloc_pool ("et_forest_node pool", sizeof (struct et_forest_node), 300);
441 return forest;
446 /* Deallocate the structure. */
447 void
448 et_forest_delete (et_forest_t forest)
450 if (forest->nnodes)
451 abort ();
452 free_alloc_pool (forest->occur_pool);
453 free_alloc_pool (forest->node_pool);
454 free (forest);
457 /* Create new node with VALUE and return the edge.
458 Return NULL when memory allocation failed. */
459 et_forest_node_t
460 et_forest_add_node (et_forest_t forest, void *value)
462 /* Create node with one occurrence. */
463 et_forest_node_t node;
464 et_forest_occurrence_t occ;
466 node = pool_alloc (forest->node_pool);
467 occ = pool_alloc (forest->occur_pool);
469 node->first = node->last = occ;
470 node->value = value;
471 forest->nnodes++;
473 occ->node = node;
474 occ->left = occ->right = occ->parent = 0;
475 occ->next = 0;
476 occ->count_left = occ->count_right = 0;
477 return node;
480 /* Add new edge to the tree, return 1 if successful.
481 0 indicates that creation of the edge will close the cycle in graph. */
483 et_forest_add_edge (et_forest_t forest ATTRIBUTE_UNUSED,
484 et_forest_node_t parent_node, et_forest_node_t child_node)
486 et_forest_occurrence_t new_occ, parent_occ, child_occ;
488 if (! parent_node || ! child_node)
489 abort ();
491 parent_occ = parent_node->first;
492 child_occ = child_node->first;
494 splay (parent_occ);
495 splay (child_occ);
497 if (parent_occ->parent)
498 return 0; /* Both child and parent are in the same tree. */
500 if (child_occ->left)
501 abort (); /* child must be root of its containing tree. */
503 new_occ = pool_alloc (forest->occur_pool);
505 new_occ->node = parent_node;
506 new_occ->left = child_occ;
507 new_occ->count_left = child_occ->count_right + 1; /* count_left is 0. */
508 new_occ->right = parent_occ->right;
509 new_occ->count_right = parent_occ->count_right;
510 new_occ->parent = parent_occ;
511 new_occ->next = parent_occ->next;
512 child_occ->parent = new_occ;
513 parent_occ->right = new_occ;
514 parent_occ->count_right = new_occ->count_left + new_occ->count_right + 1;
515 parent_occ->next = new_occ;
516 if (new_occ->right)
517 new_occ->right->parent = new_occ;
519 if (parent_node->last == parent_occ)
520 parent_node->last = new_occ;
521 return 1;
524 /* Remove NODE from the tree and all connected edges. */
525 void
526 et_forest_remove_node (et_forest_t forest, et_forest_node_t node)
528 remove_all_occurrences (forest, node);
529 forest->nnodes--;
531 pool_free (forest->node_pool, node);
534 /* Remove edge from the tree, return 1 if successful,
535 0 indicates nonexisting edge. */
537 et_forest_remove_edge (et_forest_t forest ATTRIBUTE_UNUSED,
538 et_forest_node_t parent_node,
539 et_forest_node_t child_node)
541 et_forest_occurrence_t parent_pre_occ, parent_post_occ;
543 splay (child_node->first);
545 if (! child_node->first->left)
546 return 0;
548 parent_pre_occ = find_rightmost_node (child_node->first->left);
549 if (parent_pre_occ->node != parent_node)
550 abort ();
552 splay (parent_pre_occ);
553 parent_pre_occ->right->parent = 0;
555 parent_post_occ = parent_pre_occ->next;
556 splay (parent_post_occ);
558 parent_post_occ->left->parent = 0;
560 parent_pre_occ->right = parent_post_occ->right;
561 parent_pre_occ->count_right = parent_post_occ->count_right;
562 if (parent_post_occ->right)
563 parent_post_occ->right->parent = parent_pre_occ;
565 parent_pre_occ->next = parent_post_occ->next;
567 if (parent_post_occ == parent_node->last)
568 parent_node->last = parent_pre_occ;
570 pool_free (forest->occur_pool, parent_post_occ);
571 return 1;
574 /* Return the parent of the NODE if any, NULL otherwise. */
575 et_forest_node_t
576 et_forest_parent (et_forest_t forest ATTRIBUTE_UNUSED, et_forest_node_t node)
578 splay (node->first);
580 if (node->first->left)
581 return find_rightmost_node (node->first->left)->node;
582 else
583 return 0;
587 /* Return nearest common ancestor of NODE1 and NODE2.
588 Return NULL of they are in different trees. */
589 et_forest_node_t
590 et_forest_common_ancestor (et_forest_t forest ATTRIBUTE_UNUSED,
591 et_forest_node_t node1, et_forest_node_t node2)
593 int value1, value2, max_value;
594 et_forest_node_t ancestor;
596 if (node1 == node2)
597 return node1;
599 if (! node1 || ! node2)
600 abort ();
602 splay (node1->first);
603 splay (node2->first);
605 if (! node1->first->parent) /* The two vertices are in different trees. */
606 return 0;
608 value2 = calculate_value (node2->first);
609 value1 = calculate_value (node1->first);
611 if (value1 < value2)
613 ancestor = node1;
614 max_value = value2;
616 else
618 ancestor = node2;
619 max_value = value1;
622 while (calculate_value (ancestor->last) < max_value)
624 /* Find parent node. */
625 splay (ancestor->first);
626 ancestor = find_rightmost_node (ancestor->first->left) ->node;
629 return ancestor;
632 /* Return the value pointer of node set during it's creation. */
633 void *
634 et_forest_node_value (et_forest_t forest ATTRIBUTE_UNUSED,
635 et_forest_node_t node)
637 /* Alloc threading NULL as a special node of the forest. */
638 if (!node)
639 return NULL;
640 return node->value;
643 /* Find all sons of NODE and store them into ARRAY allocated by the caller.
644 Return number of nodes found. */
646 et_forest_enumerate_sons (et_forest_t forest ATTRIBUTE_UNUSED,
647 et_forest_node_t node, et_forest_node_t *array)
649 int n = 0;
650 et_forest_occurrence_t occ = node->first, stop = node->last, occ1;
652 /* Parent is the rightmost node of the left successor.
653 Look for all occurrences having no right successor
654 and lookup the sons. */
655 while (occ != stop)
657 splay (occ);
658 if (occ->right)
660 occ1 = find_leftmost_node (occ->right);
661 if (occ1->node->first == occ1)
662 array[n++] = occ1->node;
664 occ = occ->next;
666 return n;