1 /* ET-trees data structure implementation.
2 Contributed by Pavel Nejedly
3 Copyright (C) 2002-2015 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 3 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 The ET-forest structure is described in:
21 D. D. Sleator and R. E. Tarjan. A data structure for dynamic trees.
22 J. G'omput. System Sci., 26(3):362 381, 1983.
27 #include "coretypes.h"
28 #include "alloc-pool.h"
29 #include "et-forest.h"
31 /* We do not enable this with ENABLE_CHECKING, since it is awfully slow. */
36 #include "hard-reg-set.h"
38 #include "basic-block.h"
41 /* The occurrence of a node in the et tree. */
44 struct et_node
*of
; /* The node. */
46 struct et_occ
*parent
; /* Parent in the splay-tree. */
47 struct et_occ
*prev
; /* Left son in the splay-tree. */
48 struct et_occ
*next
; /* Right son in the splay-tree. */
50 int depth
; /* The depth of the node is the sum of depth
51 fields on the path to the root. */
52 int min
; /* The minimum value of the depth in the subtree
53 is obtained by adding sum of depth fields
54 on the path to the root. */
55 struct et_occ
*min_occ
; /* The occurrence in the subtree with the minimal
58 /* Pool allocation new operator. */
59 inline void *operator new (size_t)
61 return pool
.allocate ();
64 /* Delete operator utilizing pool allocation. */
65 inline void operator delete (void *ptr
)
67 pool
.remove ((et_occ
*) ptr
);
70 /* Memory allocation pool. */
71 static pool_allocator
<et_occ
> pool
;
75 pool_allocator
<et_node
> et_node::pool ("et_nodes pool", 300);
76 pool_allocator
<et_occ
> et_occ::pool ("et_occ pool", 300);
78 /* Changes depth of OCC to D. */
81 set_depth (struct et_occ
*occ
, int d
)
86 occ
->min
+= d
- occ
->depth
;
90 /* Adds D to the depth of OCC. */
93 set_depth_add (struct et_occ
*occ
, int d
)
102 /* Sets prev field of OCC to P. */
105 set_prev (struct et_occ
*occ
, struct et_occ
*t
)
108 gcc_assert (occ
!= t
);
116 /* Sets next field of OCC to P. */
119 set_next (struct et_occ
*occ
, struct et_occ
*t
)
122 gcc_assert (occ
!= t
);
130 /* Recompute minimum for occurrence OCC. */
133 et_recomp_min (struct et_occ
*occ
)
135 struct et_occ
*mson
= occ
->prev
;
139 && mson
->min
> occ
->next
->min
))
142 if (mson
&& mson
->min
< 0)
144 occ
->min
= mson
->min
+ occ
->depth
;
145 occ
->min_occ
= mson
->min_occ
;
149 occ
->min
= occ
->depth
;
155 /* Checks whether neighborhood of OCC seems sane. */
158 et_check_occ_sanity (struct et_occ
*occ
)
163 gcc_assert (occ
->parent
!= occ
);
164 gcc_assert (occ
->prev
!= occ
);
165 gcc_assert (occ
->next
!= occ
);
166 gcc_assert (!occ
->next
|| occ
->next
!= occ
->prev
);
170 gcc_assert (occ
->next
!= occ
->parent
);
171 gcc_assert (occ
->next
->parent
== occ
);
176 gcc_assert (occ
->prev
!= occ
->parent
);
177 gcc_assert (occ
->prev
->parent
== occ
);
180 gcc_assert (!occ
->parent
181 || occ
->parent
->prev
== occ
182 || occ
->parent
->next
== occ
);
185 /* Checks whether tree rooted at OCC is sane. */
188 et_check_sanity (struct et_occ
*occ
)
190 et_check_occ_sanity (occ
);
192 et_check_sanity (occ
->prev
);
194 et_check_sanity (occ
->next
);
197 /* Checks whether tree containing OCC is sane. */
200 et_check_tree_sanity (struct et_occ
*occ
)
205 et_check_sanity (occ
);
208 /* For recording the paths. */
210 /* An ad-hoc constant; if the function has more blocks, this won't work,
211 but since it is used for debugging only, it does not matter. */
212 #define MAX_NODES 100000
215 static void *datas
[MAX_NODES
];
216 static int depths
[MAX_NODES
];
218 /* Records the path represented by OCC, with depth incremented by DEPTH. */
221 record_path_before_1 (struct et_occ
*occ
, int depth
)
230 m
= record_path_before_1 (occ
->prev
, depth
);
235 fprintf (stderr
, "%d (%d); ", ((basic_block
) occ
->of
->data
)->index
, depth
);
237 gcc_assert (len
< MAX_NODES
);
240 datas
[len
] = occ
->of
;
245 m
= record_path_before_1 (occ
->next
, depth
);
250 gcc_assert (mn
== occ
->min
+ depth
- occ
->depth
);
255 /* Records the path represented by a tree containing OCC. */
258 record_path_before (struct et_occ
*occ
)
264 record_path_before_1 (occ
, 0);
265 fprintf (stderr
, "\n");
268 /* Checks whether the path represented by OCC, with depth incremented by DEPTH,
269 was not changed since the last recording. */
272 check_path_after_1 (struct et_occ
*occ
, int depth
)
281 m
= check_path_after_1 (occ
->next
, depth
);
287 gcc_assert (depths
[len
] == depth
&& datas
[len
] == occ
->of
);
291 m
= check_path_after_1 (occ
->prev
, depth
);
296 gcc_assert (mn
== occ
->min
+ depth
- occ
->depth
);
301 /* Checks whether the path represented by a tree containing OCC was
302 not changed since the last recording. */
305 check_path_after (struct et_occ
*occ
)
310 check_path_after_1 (occ
, 0);
316 /* Splay the occurrence OCC to the root of the tree. */
319 et_splay (struct et_occ
*occ
)
321 struct et_occ
*f
, *gf
, *ggf
;
322 int occ_depth
, f_depth
, gf_depth
;
325 record_path_before (occ
);
326 et_check_tree_sanity (occ
);
331 occ_depth
= occ
->depth
;
340 set_depth_add (occ
, f_depth
);
341 occ
->min_occ
= f
->min_occ
;
347 set_prev (f
, occ
->next
);
349 set_depth_add (f
->prev
, occ_depth
);
354 set_next (f
, occ
->prev
);
356 set_depth_add (f
->next
, occ_depth
);
358 set_depth (f
, -occ_depth
);
363 et_check_tree_sanity (occ
);
364 check_path_after (occ
);
369 gf_depth
= gf
->depth
;
371 set_depth_add (occ
, f_depth
+ gf_depth
);
372 occ
->min_occ
= gf
->min_occ
;
382 set_prev (gf
, f
->next
);
383 set_prev (f
, occ
->next
);
387 set_depth (f
, -occ_depth
);
388 set_depth_add (f
->prev
, occ_depth
);
389 set_depth (gf
, -f_depth
);
390 set_depth_add (gf
->prev
, f_depth
);
395 set_prev (gf
, occ
->next
);
396 set_next (f
, occ
->prev
);
400 set_depth (f
, -occ_depth
);
401 set_depth_add (f
->next
, occ_depth
);
402 set_depth (gf
, -occ_depth
- f_depth
);
403 set_depth_add (gf
->prev
, occ_depth
+ f_depth
);
411 set_next (gf
, occ
->prev
);
412 set_prev (f
, occ
->next
);
416 set_depth (f
, -occ_depth
);
417 set_depth_add (f
->prev
, occ_depth
);
418 set_depth (gf
, -occ_depth
- f_depth
);
419 set_depth_add (gf
->next
, occ_depth
+ f_depth
);
424 set_next (gf
, f
->prev
);
425 set_next (f
, occ
->prev
);
429 set_depth (f
, -occ_depth
);
430 set_depth_add (f
->next
, occ_depth
);
431 set_depth (gf
, -f_depth
);
432 set_depth_add (gf
->next
, f_depth
);
448 et_check_tree_sanity (occ
);
453 et_check_sanity (occ
);
454 check_path_after (occ
);
458 /* Create a new et tree occurrence of NODE. */
460 static struct et_occ
*
461 et_new_occ (struct et_node
*node
)
463 et_occ
*nw
= new et_occ
;
477 /* Create a new et tree containing DATA. */
480 et_new_tree (void *data
)
492 nw
->rightmost_occ
= et_new_occ (nw
);
493 nw
->parent_occ
= NULL
;
498 /* Releases et tree T. */
501 et_free_tree (struct et_node
*t
)
509 delete t
->rightmost_occ
;
513 /* Releases et tree T without maintaining other nodes. */
516 et_free_tree_force (struct et_node
*t
)
518 delete t
->rightmost_occ
;
520 delete t
->parent_occ
;
524 /* Release the alloc pools, if they are empty. */
529 et_occ::pool
.release_if_empty ();
530 et_node::pool
.release_if_empty ();
533 /* Sets father of et tree T to FATHER. */
536 et_set_father (struct et_node
*t
, struct et_node
*father
)
538 struct et_node
*left
, *right
;
539 struct et_occ
*rmost
, *left_part
, *new_f_occ
, *p
;
541 /* Update the path represented in the splay tree. */
542 new_f_occ
= et_new_occ (father
);
544 rmost
= father
->rightmost_occ
;
547 left_part
= rmost
->prev
;
549 p
= t
->rightmost_occ
;
552 set_prev (new_f_occ
, left_part
);
553 set_next (new_f_occ
, p
);
557 et_recomp_min (new_f_occ
);
559 set_prev (rmost
, new_f_occ
);
561 if (new_f_occ
->min
+ rmost
->depth
< rmost
->min
)
563 rmost
->min
= new_f_occ
->min
+ rmost
->depth
;
564 rmost
->min_occ
= new_f_occ
->min_occ
;
567 t
->parent_occ
= new_f_occ
;
569 /* Update the tree. */
585 et_check_tree_sanity (rmost
);
586 record_path_before (rmost
);
590 /* Splits the edge from T to its father. */
593 et_split (struct et_node
*t
)
595 struct et_node
*father
= t
->father
;
596 struct et_occ
*r
, *l
, *rmost
, *p_occ
;
598 /* Update the path represented by the splay tree. */
599 rmost
= t
->rightmost_occ
;
602 for (r
= rmost
->next
; r
->prev
; r
= r
->prev
)
606 r
->prev
->parent
= NULL
;
607 p_occ
= t
->parent_occ
;
609 t
->parent_occ
= NULL
;
612 p_occ
->next
->parent
= NULL
;
624 /* Update the tree. */
625 if (father
->son
== t
)
626 father
->son
= t
->right
;
627 if (father
->son
== t
)
631 t
->left
->right
= t
->right
;
632 t
->right
->left
= t
->left
;
634 t
->left
= t
->right
= NULL
;
638 et_check_tree_sanity (rmost
);
639 record_path_before (rmost
);
641 et_check_tree_sanity (r
);
642 record_path_before (r
);
646 /* Finds the nearest common ancestor of the nodes N1 and N2. */
649 et_nca (struct et_node
*n1
, struct et_node
*n2
)
651 struct et_occ
*o1
= n1
->rightmost_occ
, *o2
= n2
->rightmost_occ
, *om
;
652 struct et_occ
*l
, *r
, *ret
;
667 if (l
== o2
|| (l
&& l
->parent
!= NULL
))
675 else if (r
== o2
|| (r
&& r
->parent
!= NULL
))
685 /* O1 and O2 are in different components of the forest. */
701 mn
= o2
->depth
+ o1
->depth
;
705 et_check_tree_sanity (o2
);
708 if (ret
&& ret
->min
+ o1
->depth
+ o2
->depth
< mn
)
709 return ret
->min_occ
->of
;
714 /* Checks whether the node UP is an ancestor of the node DOWN. */
717 et_below (struct et_node
*down
, struct et_node
*up
)
719 struct et_occ
*u
= up
->rightmost_occ
, *d
= down
->rightmost_occ
;
720 struct et_occ
*l
, *r
;
739 if (l
== d
|| l
->parent
!= NULL
)
745 et_check_tree_sanity (u
);
752 /* In case O1 and O2 are in two different trees, we must just restore the
754 if (r
&& r
->parent
!= NULL
)
760 et_check_tree_sanity (u
);
768 return !d
->next
|| d
->next
->min
+ d
->depth
>= 0;
771 /* Returns the root of the tree that contains NODE. */
774 et_root (struct et_node
*node
)
776 struct et_occ
*occ
= node
->rightmost_occ
, *r
;
778 /* The root of the tree corresponds to the rightmost occurrence in the
781 for (r
= occ
; r
->next
; r
= r
->next
)