@@ -37,12 +37,26 @@ pub fn inline(sess: &Session, module: &mut Module) -> super::Result<()> {
37
37
. find ( |inst| inst. class . opcode == Op :: TypeVoid )
38
38
. map ( |inst| inst. result_id . unwrap ( ) )
39
39
. unwrap_or ( 0 ) ;
40
+ let ptr_map: FxHashMap < _ , _ > = module
41
+ . types_global_values
42
+ . iter ( )
43
+ . filter_map ( |inst| {
44
+ if inst. class . opcode == Op :: TypePointer
45
+ && inst. operands [ 0 ] . unwrap_storage_class ( ) == StorageClass :: Function
46
+ {
47
+ Some ( ( inst. operands [ 1 ] . unwrap_id_ref ( ) , inst. result_id . unwrap ( ) ) )
48
+ } else {
49
+ None
50
+ }
51
+ } )
52
+ . collect ( ) ;
40
53
// Drop all the functions we'll be inlining. (This also means we won't waste time processing
41
54
// inlines in functions that will get inlined)
42
55
let mut inliner = Inliner {
43
56
header : module. header . as_mut ( ) . unwrap ( ) ,
44
57
types_global_values : & mut module. types_global_values ,
45
58
void,
59
+ ptr_map,
46
60
functions : & functions,
47
61
disallowed_argument_types : & disallowed_argument_types,
48
62
disallowed_return_types : & disallowed_return_types,
@@ -283,6 +297,7 @@ struct Inliner<'m, 'map> {
283
297
header : & ' m mut ModuleHeader ,
284
298
types_global_values : & ' m mut Vec < Instruction > ,
285
299
void : Word ,
300
+ ptr_map : FxHashMap < Word , Word > ,
286
301
functions : & ' map FunctionMap ,
287
302
disallowed_argument_types : & ' map FxHashSet < Word > ,
288
303
disallowed_return_types : & ' map FxHashSet < Word > ,
@@ -297,14 +312,9 @@ impl Inliner<'_, '_> {
297
312
}
298
313
299
314
fn ptr_ty ( & mut self , pointee : Word ) -> Word {
300
- // TODO: This is horribly slow, fix this
301
- let existing = self . types_global_values . iter ( ) . find ( |inst| {
302
- inst. class . opcode == Op :: TypePointer
303
- && inst. operands [ 0 ] . unwrap_storage_class ( ) == StorageClass :: Function
304
- && inst. operands [ 1 ] . unwrap_id_ref ( ) == pointee
305
- } ) ;
315
+ let existing = self . ptr_map . get ( & pointee) ;
306
316
if let Some ( existing) = existing {
307
- return existing. result_id . unwrap ( ) ;
317
+ return * existing;
308
318
}
309
319
let inst_id = self . id ( ) ;
310
320
self . types_global_values . push ( Instruction :: new (
@@ -316,6 +326,7 @@ impl Inliner<'_, '_> {
316
326
Operand :: IdRef ( pointee) ,
317
327
] ,
318
328
) ) ;
329
+ self . ptr_map . insert ( pointee, inst_id) ;
319
330
inst_id
320
331
}
321
332
0 commit comments