@@ -59,12 +59,19 @@ impl<'tcx> MonoItem<'tcx> {
5959 pub fn size_estimate ( & self , tcx : TyCtxt < ' tcx > ) -> usize {
6060 match * self {
6161 MonoItem :: Fn ( instance) => {
62- // Estimate the size of a function based on how many statements
63- // it contains.
64- tcx. instance_def_size_estimate ( instance. def )
62+ match instance. def {
63+ // "Normal" functions size estimate: the number of
64+ // statements, plus one for the terminator.
65+ InstanceDef :: Item ( ..) | InstanceDef :: DropGlue ( ..) => {
66+ let mir = tcx. instance_mir ( instance. def ) ;
67+ mir. basic_blocks . iter ( ) . map ( |bb| bb. statements . len ( ) + 1 ) . sum ( )
68+ }
69+ // Other compiler-generated shims size estimate: 1
70+ _ => 1 ,
71+ }
6572 }
66- // Conservatively estimate the size of a static declaration
67- // or assembly to be 1.
73+ // Conservatively estimate the size of a static declaration or
74+ // assembly item to be 1.
6875 MonoItem :: Static ( _) | MonoItem :: GlobalAsm ( _) => 1 ,
6976 }
7077 }
@@ -230,14 +237,22 @@ pub struct CodegenUnit<'tcx> {
230237 /// contain something unique to this crate (e.g., a module path)
231238 /// as well as the crate name and disambiguator.
232239 name : Symbol ,
233- items : FxHashMap < MonoItem < ' tcx > , ( Linkage , Visibility ) > ,
240+ items : FxHashMap < MonoItem < ' tcx > , MonoItemData > ,
234241 size_estimate : usize ,
235242 primary : bool ,
236243 /// True if this is CGU is used to hold code coverage information for dead code,
237244 /// false otherwise.
238245 is_code_coverage_dead_code_cgu : bool ,
239246}
240247
248+ /// Auxiliary info about a `MonoItem`.
249+ #[ derive( Copy , Clone , PartialEq , Debug , HashStable ) ]
250+ pub struct MonoItemData {
251+ pub linkage : Linkage ,
252+ pub visibility : Visibility ,
253+ pub size_estimate : usize ,
254+ }
255+
241256/// Specifies the linkage type for a `MonoItem`.
242257///
243258/// See <https://llvm.org/docs/LangRef.html#linkage-types> for more details about these variants.
@@ -292,12 +307,12 @@ impl<'tcx> CodegenUnit<'tcx> {
292307 }
293308
294309 /// The order of these items is non-determinstic.
295- pub fn items ( & self ) -> & FxHashMap < MonoItem < ' tcx > , ( Linkage , Visibility ) > {
310+ pub fn items ( & self ) -> & FxHashMap < MonoItem < ' tcx > , MonoItemData > {
296311 & self . items
297312 }
298313
299314 /// The order of these items is non-determinstic.
300- pub fn items_mut ( & mut self ) -> & mut FxHashMap < MonoItem < ' tcx > , ( Linkage , Visibility ) > {
315+ pub fn items_mut ( & mut self ) -> & mut FxHashMap < MonoItem < ' tcx > , MonoItemData > {
301316 & mut self . items
302317 }
303318
@@ -320,16 +335,16 @@ impl<'tcx> CodegenUnit<'tcx> {
320335 base_n:: encode ( hash, base_n:: CASE_INSENSITIVE )
321336 }
322337
323- pub fn compute_size_estimate ( & mut self , tcx : TyCtxt < ' tcx > ) {
324- // Estimate the size of a codegen unit as (approximately) the number of MIR
325- // statements it corresponds to .
326- self . size_estimate = self . items . keys ( ) . map ( |mi| mi . size_estimate ( tcx ) ) . sum ( ) ;
338+ pub fn compute_size_estimate ( & mut self ) {
339+ // The size of a codegen unit as the sum of the sizes of the items
340+ // within it.
341+ self . size_estimate = self . items . values ( ) . map ( |data| data . size_estimate ) . sum ( ) ;
327342 }
328343
329- #[ inline]
330344 /// Should only be called if [`compute_size_estimate`] has previously been called.
331345 ///
332346 /// [`compute_size_estimate`]: Self::compute_size_estimate
347+ #[ inline]
333348 pub fn size_estimate ( & self ) -> usize {
334349 // Items are never zero-sized, so if we have items the estimate must be
335350 // non-zero, unless we forgot to call `compute_size_estimate` first.
@@ -355,7 +370,7 @@ impl<'tcx> CodegenUnit<'tcx> {
355370 pub fn items_in_deterministic_order (
356371 & self ,
357372 tcx : TyCtxt < ' tcx > ,
358- ) -> Vec < ( MonoItem < ' tcx > , ( Linkage , Visibility ) ) > {
373+ ) -> Vec < ( MonoItem < ' tcx > , MonoItemData ) > {
359374 // The codegen tests rely on items being process in the same order as
360375 // they appear in the file, so for local items, we sort by node_id first
361376 #[ derive( PartialEq , Eq , PartialOrd , Ord ) ]
@@ -390,7 +405,7 @@ impl<'tcx> CodegenUnit<'tcx> {
390405 )
391406 }
392407
393- let mut items: Vec < _ > = self . items ( ) . iter ( ) . map ( |( & i, & l ) | ( i, l ) ) . collect ( ) ;
408+ let mut items: Vec < _ > = self . items ( ) . iter ( ) . map ( |( & i, & data ) | ( i, data ) ) . collect ( ) ;
394409 items. sort_by_cached_key ( |& ( i, _) | item_sort_key ( tcx, i) ) ;
395410 items
396411 }
0 commit comments