11use crate :: allocator:: * ;
22use crate :: codegen:: SubsumedBranchHits ;
3- use crate :: forms:: { GenContext , Level } ;
3+ use crate :: forms:: { BranchNumber , GenContext , Level } ;
44use crate :: instructions:: * ;
55use crate :: machine:: disjuncts:: VarData ;
66use crate :: parser:: ast:: * ;
@@ -24,32 +24,34 @@ pub struct BranchOccurrences {
2424 pub shallow_safety : BitSet < usize > , // unset means safe, set means unsafe (after the branch merge)
2525 pub deep_safety : BitSet < usize > ,
2626 pub num_branches : usize ,
27- pub current_branch : usize ,
27+ pub current_branch_idx : usize ,
28+ pub current_branch_num : BranchNumber ,
2829 pub subsumed_hits : SubsumedBranchHits ,
2930}
3031
3132impl BranchOccurrences {
32- fn new ( num_branches : usize ) -> Self {
33+ fn new ( current_branch_num : BranchNumber , num_branches : usize ) -> Self {
3334 Self {
3435 hits : BranchHits :: with_hasher ( FxBuildHasher :: default ( ) ) ,
3536 shallow_safety : BitSet :: default ( ) ,
3637 deep_safety : BitSet :: default ( ) ,
3738 num_branches,
38- current_branch : 0 ,
39+ current_branch_idx : 0 ,
40+ current_branch_num,
3941 subsumed_hits : SubsumedBranchHits :: with_hasher ( FxBuildHasher :: default ( ) ) ,
4042 }
4143 }
4244
4345 pub ( crate ) fn add_branch_occurrence ( & mut self , var_num : usize ) {
44- debug_assert ! ( self . current_branch < self . num_branches) ;
46+ debug_assert ! ( self . current_branch_idx < self . num_branches) ;
4547 let num_branches = self . num_branches ;
4648
4749 let entry = self
4850 . hits
4951 . entry ( var_num)
5052 . or_insert_with ( || BitVec :: repeat ( false , num_branches) ) ;
5153
52- entry. set ( self . current_branch , true ) ;
54+ entry. set ( self . current_branch_idx , true ) ;
5355 self . subsumed_hits . insert ( var_num) ;
5456 }
5557}
@@ -76,29 +78,16 @@ impl DerefMut for BranchStack {
7678}
7779
7880impl BranchStack {
79- fn branch_subsumes ( & self , branch : & BranchDesignator , sub_branch : & BranchDesignator ) -> bool {
80- if branch. branch_stack_num < sub_branch. branch_stack_num {
81- if branch. branch_stack_num == 0 {
82- true
83- } else {
84- let idx = branch. branch_stack_num - 1 ;
85- self [ idx] . current_branch == branch. branch_num
86- }
87- } else {
88- branch == sub_branch
89- }
90- }
91-
9281 fn safety_unneeded_in_branch (
9382 & self ,
9483 safety : & VarSafetyStatus ,
9584 branch : & BranchDesignator ,
9685 ) -> bool {
9786 match safety {
9887 VarSafetyStatus :: Needed => false ,
99- VarSafetyStatus :: LocallyUnneeded ( planter_branch) => {
100- self . branch_subsumes ( planter_branch , branch )
101- }
88+ VarSafetyStatus :: LocallyUnneeded ( planter_branch) => planter_branch
89+ . branch_num
90+ . has_as_subbranch ( & branch . branch_num ) ,
10291 VarSafetyStatus :: GloballyUnneeded => true ,
10392 }
10493 }
@@ -109,27 +98,24 @@ impl BranchStack {
10998 }
11099 }
111100
112- pub ( crate ) fn add_branch_stack ( & mut self , num_branches : usize ) {
113- self . push ( BranchOccurrences :: new ( num_branches) ) ;
101+ pub ( crate ) fn add_branch_stack ( & mut self , branch_num : BranchNumber , num_branches : usize ) {
102+ self . push ( BranchOccurrences :: new ( branch_num , num_branches) ) ;
114103 }
115104
116105 pub ( crate ) fn current_branch_designator ( & self ) -> BranchDesignator {
117- let branch_stack_num = self . len ( ) ;
118106 let branch_num = self
119107 . last ( )
120- . map ( |occurrences| occurrences. current_branch )
121- . unwrap_or ( 0 ) ;
108+ . map ( |occurrences| occurrences. current_branch_num . clone ( ) )
109+ . unwrap_or_else ( || BranchNumber :: default ( ) ) ;
122110
123- BranchDesignator {
124- branch_stack_num,
125- branch_num,
126- }
111+ BranchDesignator { branch_num }
127112 }
128113
129114 #[ inline]
130- pub ( crate ) fn incr_current_branch ( & mut self ) {
115+ pub ( crate ) fn incr_current_branch ( & mut self , branch_num : BranchNumber ) {
131116 let branch_occurrences = self . last_mut ( ) . unwrap ( ) ;
132- branch_occurrences. current_branch += 1 ;
117+ branch_occurrences. current_branch_idx += 1 ;
118+ branch_occurrences. current_branch_num = branch_num;
133119 }
134120
135121 #[ inline]
@@ -235,12 +221,12 @@ impl DebrayAllocator {
235221 VarAlloc :: Perm ( _, allocation) => {
236222 let shallow_safety = VarSafetyStatus :: needed_if (
237223 shallow_safety. contains ( var_num) ,
238- branch_designator,
224+ & branch_designator,
239225 ) ;
240226
241227 let deep_safety = VarSafetyStatus :: needed_if (
242228 deep_safety. contains ( var_num) ,
243- branch_designator,
229+ & branch_designator,
244230 ) ;
245231
246232 if running_count < num_occurrences {
@@ -531,11 +517,11 @@ impl DebrayAllocator {
531517 ..
532518 } ,
533519 ) => {
534- * deep_safety = VarSafetyStatus :: unneeded ( branch_designator) ;
535- * shallow_safety = VarSafetyStatus :: unneeded ( branch_designator) ;
520+ * deep_safety = VarSafetyStatus :: unneeded ( & branch_designator) ;
521+ * shallow_safety = VarSafetyStatus :: unneeded ( & branch_designator) ;
536522 }
537523 VarAlloc :: Temp { safety, .. } => {
538- * safety = VarSafetyStatus :: unneeded ( branch_designator) ;
524+ * safety = VarSafetyStatus :: unneeded ( & branch_designator) ;
539525 }
540526 _ => {
541527 unreachable ! ( )
@@ -557,8 +543,8 @@ impl DebrayAllocator {
557543 ) => {
558544 // GetVariable in head chunk is considered safe.
559545 if lvl == Level :: Deep {
560- * deep_safety = VarSafetyStatus :: unneeded ( branch_designator) ;
561- * shallow_safety = VarSafetyStatus :: unneeded ( branch_designator) ;
546+ * deep_safety = VarSafetyStatus :: unneeded ( & branch_designator) ;
547+ * shallow_safety = VarSafetyStatus :: unneeded ( & branch_designator) ;
562548 } else if term_loc == GenContext :: Head {
563549 * shallow_safety = VarSafetyStatus :: GloballyUnneeded ;
564550 } else if let Some ( & temp_var_num) = self . shallow_temp_mappings . get ( & self . arg_c ) {
@@ -605,7 +591,7 @@ impl DebrayAllocator {
605591 {
606592 Target :: argument_to_value ( r, arg_c)
607593 } else {
608- * shallow_safety = VarSafetyStatus :: unneeded ( branch_designator) ;
594+ * shallow_safety = VarSafetyStatus :: unneeded ( & branch_designator) ;
609595 Target :: unsafe_argument_to_value ( r, arg_c)
610596 }
611597 }
@@ -640,7 +626,7 @@ impl DebrayAllocator {
640626 {
641627 Target :: subterm_to_value ( r)
642628 } else {
643- * deep_safety = VarSafetyStatus :: unneeded ( branch_designator) ;
629+ * deep_safety = VarSafetyStatus :: unneeded ( & branch_designator) ;
644630 Target :: unsafe_subterm_to_value ( r)
645631 }
646632 }
@@ -651,7 +637,7 @@ impl DebrayAllocator {
651637 {
652638 Target :: subterm_to_value ( r)
653639 } else {
654- * safety = VarSafetyStatus :: unneeded ( branch_designator) ;
640+ * safety = VarSafetyStatus :: unneeded ( & branch_designator) ;
655641 Target :: unsafe_subterm_to_value ( r)
656642 }
657643 }
0 commit comments