From eb4e94b2e5f479ac44bf8d4094cdcb73d5941b22 Mon Sep 17 00:00:00 2001 From: oli Date: Wed, 28 Oct 2020 13:49:10 +0000 Subject: [PATCH] Simplify the `optimize_mir` query --- compiler/rustc_middle/src/mir/query.rs | 2 +- compiler/rustc_middle/src/ty/mod.rs | 2 +- compiler/rustc_mir/src/transform/mod.rs | 26 +++++++++---------------- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index 3e4490e4915f..a7b847fc5e0e 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -439,7 +439,7 @@ impl<'tcx> TyCtxt<'tcx> { } #[inline] - pub fn optimized_mir_opt_const_arg( + pub fn optimized_mir_or_const_arg_mir( self, def: ty::WithOptConstParam, ) -> &'tcx Body<'tcx> { diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index ca4db8abc0ca..ccdec2bf89f1 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -3018,7 +3018,7 @@ impl<'tcx> TyCtxt<'tcx> { | DefKind::AnonConst => self.mir_for_ctfe_opt_const_arg(def), // If the caller wants `mir_for_ctfe` they should not be using `instance_mir`, so // we'll assume const fn also wants the optimized version. - _ => self.optimized_mir_opt_const_arg(def), + _ => self.optimized_mir_or_const_arg_mir(def), }, ty::InstanceDef::VtableShim(..) | ty::InstanceDef::ReifyShim(..) diff --git a/compiler/rustc_mir/src/transform/mod.rs b/compiler/rustc_mir/src/transform/mod.rs index 8a4c5c1ef69f..edf6fe2e0efa 100644 --- a/compiler/rustc_mir/src/transform/mod.rs +++ b/compiler/rustc_mir/src/transform/mod.rs @@ -517,34 +517,26 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn optimized_mir<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx Body<'tcx> { let did = did.expect_local(); - if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) { - tcx.mir_for_ctfe_of_const_arg(def) - } else { - tcx.arena.alloc(inner_optimized_mir(tcx, ty::WithOptConstParam::unknown(did))) - } + assert_eq!(ty::WithOptConstParam::try_lookup(did, tcx), None); + tcx.arena.alloc(inner_optimized_mir(tcx, did)) } -fn inner_optimized_mir(tcx: TyCtxt<'_>, def: ty::WithOptConstParam) -> Body<'_> { - if tcx.is_constructor(def.did.to_def_id()) { +fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> { + if tcx.is_constructor(did.to_def_id()) { // There's no reason to run all of the MIR passes on constructors when // we can just output the MIR we want directly. This also saves const // qualification and borrow checking the trouble of special casing // constructors. - return shim::build_adt_ctor(tcx, def.did.to_def_id()); + return shim::build_adt_ctor(tcx, did.to_def_id()); } - match tcx.hir().body_const_context(def.did) { - Some(hir::ConstContext::ConstFn) => { - if let Some((did, param_did)) = def.to_global().as_const_arg() { - tcx.ensure().mir_for_ctfe_of_const_arg((did, param_did)) - } else { - tcx.ensure().mir_for_ctfe(def.did) - } - } + match tcx.hir().body_const_context(did) { + Some(hir::ConstContext::ConstFn) => tcx.ensure().mir_for_ctfe(did), None => {} Some(other) => panic!("do not use `optimized_mir` for constants: {:?}", other), } - let mut body = tcx.mir_drops_elaborated_and_const_checked(def).steal(); + let mut body = + tcx.mir_drops_elaborated_and_const_checked(ty::WithOptConstParam::unknown(did)).steal(); run_optimization_passes(tcx, &mut body); debug_assert!(!body.has_free_regions(), "Free regions in optimized MIR");