1
Fork 0

Simplify the optimize_mir query

This commit is contained in:
oli 2020-10-28 13:49:10 +00:00
parent 1f5fb3e056
commit eb4e94b2e5
3 changed files with 11 additions and 19 deletions

View file

@ -439,7 +439,7 @@ impl<'tcx> TyCtxt<'tcx> {
} }
#[inline] #[inline]
pub fn optimized_mir_opt_const_arg( pub fn optimized_mir_or_const_arg_mir(
self, self,
def: ty::WithOptConstParam<DefId>, def: ty::WithOptConstParam<DefId>,
) -> &'tcx Body<'tcx> { ) -> &'tcx Body<'tcx> {

View file

@ -3018,7 +3018,7 @@ impl<'tcx> TyCtxt<'tcx> {
| DefKind::AnonConst => self.mir_for_ctfe_opt_const_arg(def), | 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 // 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. // 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::VtableShim(..)
| ty::InstanceDef::ReifyShim(..) | ty::InstanceDef::ReifyShim(..)

View file

@ -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> { fn optimized_mir<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx Body<'tcx> {
let did = did.expect_local(); let did = did.expect_local();
if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) { assert_eq!(ty::WithOptConstParam::try_lookup(did, tcx), None);
tcx.mir_for_ctfe_of_const_arg(def) tcx.arena.alloc(inner_optimized_mir(tcx, did))
} else {
tcx.arena.alloc(inner_optimized_mir(tcx, ty::WithOptConstParam::unknown(did)))
}
} }
fn inner_optimized_mir(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_> { fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> {
if tcx.is_constructor(def.did.to_def_id()) { if tcx.is_constructor(did.to_def_id()) {
// There's no reason to run all of the MIR passes on constructors when // 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 // we can just output the MIR we want directly. This also saves const
// qualification and borrow checking the trouble of special casing // qualification and borrow checking the trouble of special casing
// constructors. // 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) { match tcx.hir().body_const_context(did) {
Some(hir::ConstContext::ConstFn) => { Some(hir::ConstContext::ConstFn) => tcx.ensure().mir_for_ctfe(did),
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)
}
}
None => {} None => {}
Some(other) => panic!("do not use `optimized_mir` for constants: {:?}", other), 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); run_optimization_passes(tcx, &mut body);
debug_assert!(!body.has_free_regions(), "Free regions in optimized MIR"); debug_assert!(!body.has_free_regions(), "Free regions in optimized MIR");