@@ -619,7 +619,7 @@ impl<T: Copy + Into<usize> + PartialEq> UnionFind<T> {
619
619
}
620
620
621
621
/// Find the set representative for `insn` without doing path compression.
622
- pub fn find_const ( & self , insn : T ) -> T {
622
+ fn find_const ( & self , insn : T ) -> T {
623
623
let mut result = insn;
624
624
loop {
625
625
match self . at ( result) {
@@ -645,7 +645,7 @@ pub struct Function {
645
645
// TODO: get method name and source location from the ISEQ
646
646
647
647
insns : Vec < Insn > ,
648
- union_find : UnionFind < InsnId > ,
648
+ union_find : std :: cell :: RefCell < UnionFind < InsnId > > ,
649
649
insn_types : Vec < Type > ,
650
650
blocks : Vec < Block > ,
651
651
entry_block : BlockId ,
@@ -657,7 +657,7 @@ impl Function {
657
657
iseq,
658
658
insns : vec ! [ ] ,
659
659
insn_types : vec ! [ ] ,
660
- union_find : UnionFind :: new ( ) ,
660
+ union_find : UnionFind :: new ( ) . into ( ) ,
661
661
blocks : vec ! [ Block :: default ( ) ] ,
662
662
entry_block : BlockId ( 0 ) ,
663
663
}
@@ -740,7 +740,7 @@ impl Function {
740
740
macro_rules! find {
741
741
( $x: expr ) => {
742
742
{
743
- self . union_find. find_const ( $x)
743
+ self . union_find. borrow_mut ( ) . find ( $x)
744
744
}
745
745
} ;
746
746
}
@@ -749,12 +749,12 @@ impl Function {
749
749
{
750
750
BranchEdge {
751
751
target: $edge. target,
752
- args: $edge. args. iter( ) . map( |x| self . union_find . find_const ( * x) ) . collect( ) ,
752
+ args: $edge. args. iter( ) . map( |x| find! ( * x) ) . collect( ) ,
753
753
}
754
754
}
755
755
} ;
756
756
}
757
- let insn_id = self . union_find . find_const ( insn_id) ;
757
+ let insn_id = self . union_find . borrow_mut ( ) . find ( insn_id) ;
758
758
use Insn :: * ;
759
759
match & self . insns [ insn_id. 0 ] {
760
760
result@( PutSelf | Const { ..} | Param { ..} | NewArray { ..} | GetConstantPath { ..}
@@ -822,12 +822,12 @@ impl Function {
822
822
/// Replace `insn` with the new instruction `replacement`, which will get appended to `insns`.
823
823
fn make_equal_to ( & mut self , insn : InsnId , replacement : InsnId ) {
824
824
// Don't push it to the block
825
- self . union_find . make_equal_to ( insn, replacement) ;
825
+ self . union_find . borrow_mut ( ) . make_equal_to ( insn, replacement) ;
826
826
}
827
827
828
828
fn type_of ( & self , insn : InsnId ) -> Type {
829
829
assert ! ( self . insns[ insn. 0 ] . has_output( ) ) ;
830
- self . insn_types [ self . union_find . find_const ( insn) . 0 ]
830
+ self . insn_types [ self . union_find . borrow_mut ( ) . find ( insn) . 0 ]
831
831
}
832
832
833
833
/// Check if the type of `insn` is a subtype of `ty`.
@@ -1978,19 +1978,19 @@ mod union_find_tests {
1978
1978
}
1979
1979
1980
1980
#[ test]
1981
- fn test_find_const_returns_target ( ) {
1981
+ fn test_find_returns_target ( ) {
1982
1982
let mut uf = UnionFind :: new ( ) ;
1983
1983
uf. make_equal_to ( 3 , 4 ) ;
1984
- assert_eq ! ( uf. find_const ( 3usize ) , 4 ) ;
1984
+ assert_eq ! ( uf. find ( 3usize ) , 4 ) ;
1985
1985
}
1986
1986
1987
1987
#[ test]
1988
- fn test_find_const_returns_transitive_target ( ) {
1988
+ fn test_find_returns_transitive_target ( ) {
1989
1989
let mut uf = UnionFind :: new ( ) ;
1990
1990
uf. make_equal_to ( 3 , 4 ) ;
1991
1991
uf. make_equal_to ( 4 , 5 ) ;
1992
- assert_eq ! ( uf. find_const ( 3usize ) , 5 ) ;
1993
- assert_eq ! ( uf. find_const ( 4usize ) , 5 ) ;
1992
+ assert_eq ! ( uf. find ( 3usize ) , 5 ) ;
1993
+ assert_eq ! ( uf. find ( 4usize ) , 5 ) ;
1994
1994
}
1995
1995
1996
1996
#[ test]
0 commit comments