@@ -558,72 +558,78 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
558
558
WithMinOptLevel ( 1 , x)
559
559
}
560
560
561
+ let passes: [ & dyn MirPass < ' tcx > ; 40 ] = [
562
+ // Add some UB checks before any UB gets optimized away.
563
+ & check_alignment:: CheckAlignment as & dyn MirPass < ' _ > ,
564
+ // Before inlining: trim down MIR with passes to reduce inlining work.
565
+
566
+ // Has to be done before inlining, otherwise actual call will be almost always inlined.
567
+ // Also simple, so can just do first
568
+ & lower_slice_len:: LowerSliceLenCalls ,
569
+ // Perform instsimplify before inline to eliminate some trivial calls (like clone shims).
570
+ & instsimplify:: InstSimplify :: BeforeInline ,
571
+ // Perform inlining, which may add a lot of code.
572
+ & inline:: Inline ,
573
+ // Code from other crates may have storage markers, so this needs to happen after inlining.
574
+ & remove_storage_markers:: RemoveStorageMarkers ,
575
+ // Inlining and instantiation may introduce ZST and useless drops.
576
+ & remove_zsts:: RemoveZsts ,
577
+ & remove_unneeded_drops:: RemoveUnneededDrops ,
578
+ // Type instantiation may create uninhabited enums.
579
+ // Also eliminates some unreachable branches based on variants of enums.
580
+ & unreachable_enum_branching:: UnreachableEnumBranching ,
581
+ & unreachable_prop:: UnreachablePropagation ,
582
+ & o1 ( simplify:: SimplifyCfg :: AfterUnreachableEnumBranching ) ,
583
+ // Inlining may have introduced a lot of redundant code and a large move pattern.
584
+ // Now, we need to shrink the generated MIR.
585
+ & ref_prop:: ReferencePropagation ,
586
+ & sroa:: ScalarReplacementOfAggregates ,
587
+ & match_branches:: MatchBranchSimplification ,
588
+ // inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
589
+ & multiple_return_terminators:: MultipleReturnTerminators ,
590
+ // After simplifycfg, it allows us to discover new opportunities for peephole optimizations.
591
+ & instsimplify:: InstSimplify :: AfterSimplifyCfg ,
592
+ & simplify:: SimplifyLocals :: BeforeConstProp ,
593
+ & dead_store_elimination:: DeadStoreElimination :: Initial ,
594
+ & gvn:: GVN ,
595
+ & simplify:: SimplifyLocals :: AfterGVN ,
596
+ & dataflow_const_prop:: DataflowConstProp ,
597
+ & single_use_consts:: SingleUseConsts ,
598
+ & o1 ( simplify_branches:: SimplifyConstCondition :: AfterConstProp ) ,
599
+ & jump_threading:: JumpThreading ,
600
+ & early_otherwise_branch:: EarlyOtherwiseBranch ,
601
+ & simplify_comparison_integral:: SimplifyComparisonIntegral ,
602
+ & dest_prop:: DestinationPropagation ,
603
+ & o1 ( simplify_branches:: SimplifyConstCondition :: Final ) ,
604
+ & o1 ( remove_noop_landing_pads:: RemoveNoopLandingPads ) ,
605
+ & o1 ( simplify:: SimplifyCfg :: Final ) ,
606
+ & copy_prop:: CopyProp ,
607
+ & dead_store_elimination:: DeadStoreElimination :: Final ,
608
+ & nrvo:: RenameReturnPlace ,
609
+ & simplify:: SimplifyLocals :: Final ,
610
+ & multiple_return_terminators:: MultipleReturnTerminators ,
611
+ & deduplicate_blocks:: DeduplicateBlocks ,
612
+ & large_enums:: EnumSizeOpt { discrepancy : 128 } ,
613
+ // Some cleanup necessary at least for LLVM and potentially other codegen backends.
614
+ & add_call_guards:: CriticalCallEdges ,
615
+ // Cleanup for human readability, off by default.
616
+ & prettify:: ReorderBasicBlocks ,
617
+ & prettify:: ReorderLocals ,
618
+ // Dump the end result for testing and debugging purposes.
619
+ & dump_mir:: Marker ( "PreCodegen" ) ,
620
+ ] ;
621
+
622
+ let mut passes = passes. to_vec ( ) ;
623
+ let def_id = body. source . def_id ( ) ;
624
+ if tcx. def_kind ( def_id) . has_codegen_attrs ( )
625
+ && let Some ( ref attrs) = tcx. codegen_fn_attrs ( def_id) . optimize
626
+ && attrs. do_not_optimize ( )
627
+ {
628
+ passes. retain ( |pass| pass. min_mir_opt_level ( ) <= 1 ) ;
629
+ }
630
+
561
631
// The main optimizations that we do on MIR.
562
- pm:: run_passes (
563
- tcx,
564
- body,
565
- & [
566
- // Add some UB checks before any UB gets optimized away.
567
- & check_alignment:: CheckAlignment ,
568
- // Before inlining: trim down MIR with passes to reduce inlining work.
569
-
570
- // Has to be done before inlining, otherwise actual call will be almost always inlined.
571
- // Also simple, so can just do first
572
- & lower_slice_len:: LowerSliceLenCalls ,
573
- // Perform instsimplify before inline to eliminate some trivial calls (like clone shims).
574
- & instsimplify:: InstSimplify :: BeforeInline ,
575
- // Perform inlining, which may add a lot of code.
576
- & inline:: Inline ,
577
- // Code from other crates may have storage markers, so this needs to happen after inlining.
578
- & remove_storage_markers:: RemoveStorageMarkers ,
579
- // Inlining and instantiation may introduce ZST and useless drops.
580
- & remove_zsts:: RemoveZsts ,
581
- & remove_unneeded_drops:: RemoveUnneededDrops ,
582
- // Type instantiation may create uninhabited enums.
583
- // Also eliminates some unreachable branches based on variants of enums.
584
- & unreachable_enum_branching:: UnreachableEnumBranching ,
585
- & unreachable_prop:: UnreachablePropagation ,
586
- & o1 ( simplify:: SimplifyCfg :: AfterUnreachableEnumBranching ) ,
587
- // Inlining may have introduced a lot of redundant code and a large move pattern.
588
- // Now, we need to shrink the generated MIR.
589
- & ref_prop:: ReferencePropagation ,
590
- & sroa:: ScalarReplacementOfAggregates ,
591
- & match_branches:: MatchBranchSimplification ,
592
- // inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
593
- & multiple_return_terminators:: MultipleReturnTerminators ,
594
- // After simplifycfg, it allows us to discover new opportunities for peephole optimizations.
595
- & instsimplify:: InstSimplify :: AfterSimplifyCfg ,
596
- & simplify:: SimplifyLocals :: BeforeConstProp ,
597
- & dead_store_elimination:: DeadStoreElimination :: Initial ,
598
- & gvn:: GVN ,
599
- & simplify:: SimplifyLocals :: AfterGVN ,
600
- & dataflow_const_prop:: DataflowConstProp ,
601
- & single_use_consts:: SingleUseConsts ,
602
- & o1 ( simplify_branches:: SimplifyConstCondition :: AfterConstProp ) ,
603
- & jump_threading:: JumpThreading ,
604
- & early_otherwise_branch:: EarlyOtherwiseBranch ,
605
- & simplify_comparison_integral:: SimplifyComparisonIntegral ,
606
- & dest_prop:: DestinationPropagation ,
607
- & o1 ( simplify_branches:: SimplifyConstCondition :: Final ) ,
608
- & o1 ( remove_noop_landing_pads:: RemoveNoopLandingPads ) ,
609
- & o1 ( simplify:: SimplifyCfg :: Final ) ,
610
- & copy_prop:: CopyProp ,
611
- & dead_store_elimination:: DeadStoreElimination :: Final ,
612
- & nrvo:: RenameReturnPlace ,
613
- & simplify:: SimplifyLocals :: Final ,
614
- & multiple_return_terminators:: MultipleReturnTerminators ,
615
- & deduplicate_blocks:: DeduplicateBlocks ,
616
- & large_enums:: EnumSizeOpt { discrepancy : 128 } ,
617
- // Some cleanup necessary at least for LLVM and potentially other codegen backends.
618
- & add_call_guards:: CriticalCallEdges ,
619
- // Cleanup for human readability, off by default.
620
- & prettify:: ReorderBasicBlocks ,
621
- & prettify:: ReorderLocals ,
622
- // Dump the end result for testing and debugging purposes.
623
- & dump_mir:: Marker ( "PreCodegen" ) ,
624
- ] ,
625
- Some ( MirPhase :: Runtime ( RuntimePhase :: Optimized ) ) ,
626
- ) ;
632
+ pm:: run_passes ( tcx, body, & passes, Some ( MirPhase :: Runtime ( RuntimePhase :: Optimized ) ) ) ;
627
633
}
628
634
629
635
/// Optimize the MIR and prepare it for codegen.
0 commit comments