@@ -13,12 +13,15 @@ use std::collections::BTreeMap;
1313use std:: fmt;
1414use std:: ops:: Not ;
1515
16- use rustc_data_structures:: fx:: FxHashSet ;
16+ use rustc_data_structures:: fx:: { FxHashSet , FxHasher } ;
1717
1818use cranelift_codegen:: cursor:: { Cursor , FuncCursor } ;
1919use cranelift_codegen:: ir:: { InstructionData , Opcode , ValueDef } ;
2020use cranelift_codegen:: ir:: immediates:: Offset32 ;
2121
22+ use hashbrown:: HashSet ;
23+ use std:: hash:: BuildHasherDefault ;
24+
2225use crate :: prelude:: * ;
2326
2427/// Workaround for `StackSlot` not implementing `Ord`.
@@ -45,9 +48,9 @@ impl Ord for OrdStackSlot {
4548
4649#[ derive( Debug , Default ) ]
4750struct StackSlotUsage {
48- stack_addr : FxHashSet < Inst > ,
49- stack_load : FxHashSet < Inst > ,
50- stack_store : FxHashSet < Inst > ,
51+ stack_addr : HashSet < Inst , BuildHasherDefault < FxHasher > > ,
52+ stack_load : HashSet < Inst , BuildHasherDefault < FxHasher > > ,
53+ stack_store : HashSet < Inst , BuildHasherDefault < FxHasher > > ,
5154}
5255
5356impl StackSlotUsage {
@@ -79,16 +82,14 @@ impl StackSlotUsage {
7982 } ) . collect :: < Vec < Inst > > ( )
8083 }
8184
82- fn remove_unused_stack_addr ( & mut self , func : & mut Function , inst : Inst ) {
85+ fn remove_unused_stack_addr ( func : & mut Function , inst : Inst ) {
8386 func. dfg . detach_results ( inst) ;
8487 func. dfg . replace ( inst) . nop ( ) ;
85- self . stack_addr . remove ( & inst) ;
8688 }
8789
88- fn remove_unused_load ( & mut self , func : & mut Function , load : Inst ) {
90+ fn remove_unused_load ( func : & mut Function , load : Inst ) {
8991 func. dfg . detach_results ( load) ;
9092 func. dfg . replace ( load) . nop ( ) ;
91- self . stack_load . remove ( & load) ;
9293 }
9394
9495 fn remove_dead_store ( & mut self , func : & mut Function , store : Inst ) {
@@ -314,19 +315,21 @@ fn remove_unused_stack_addr_and_stack_load(opt_ctx: &mut OptimizeContext<'_>) {
314315 }
315316
316317 // Replace all unused stack_addr and stack_load instructions with nop.
317- for stack_slot_users in opt_ctx. stack_slot_usage_map . values_mut ( ) {
318- // FIXME remove clone
319- for & inst in stack_slot_users. stack_addr . clone ( ) . iter ( ) {
320- if stack_addr_load_insts_users. get ( & inst) . map ( |users| users. is_empty ( ) ) . unwrap_or ( true ) {
321- stack_slot_users. remove_unused_stack_addr ( & mut opt_ctx. ctx . func , inst) ;
322- }
323- }
318+ let mut func = & mut opt_ctx. ctx . func ;
324319
325- for & inst in stack_slot_users. stack_load . clone ( ) . iter ( ) {
326- if stack_addr_load_insts_users. get ( & inst) . map ( |users| users. is_empty ( ) ) . unwrap_or ( true ) {
327- stack_slot_users. remove_unused_load ( & mut opt_ctx. ctx . func , inst) ;
328- }
329- }
320+ // drain_filter() on hashbrown::HashSet drains the items that do *not* match the
321+ // predicate. Once hashbrown gets updated to match the behaviour of std::drain_filter
322+ // (0.8.2), the predicate will have to be reversed
323+ for stack_slot_users in opt_ctx. stack_slot_usage_map . values_mut ( ) {
324+ stack_slot_users
325+ . stack_addr
326+ . drain_filter ( |inst| !( stack_addr_load_insts_users. get ( inst) . map ( |users| users. is_empty ( ) ) . unwrap_or ( true ) ) )
327+ . for_each ( |inst| StackSlotUsage :: remove_unused_stack_addr ( & mut func, inst) ) ;
328+
329+ stack_slot_users
330+ . stack_load
331+ . drain_filter ( |inst| !( stack_addr_load_insts_users. get ( inst) . map ( |users| users. is_empty ( ) ) . unwrap_or ( true ) ) )
332+ . for_each ( |inst| StackSlotUsage :: remove_unused_load ( & mut func, inst) ) ;
330333 }
331334}
332335
0 commit comments