X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustc_mir%2Fshim.rs;h=0f91b8b28bcf01e6bc69b53ea526548fd58eb0a4;hb=4de31b26d1d4a1c20456fc9ca1d39d284c79f64c;hp=b5cb6a92816beb656c7985e050324ca8b78b0e5a;hpb=3907d59bcf6824f32bf9c91205f0c2dbcc467658;p=rust.git diff --git a/src/librustc_mir/shim.rs b/src/librustc_mir/shim.rs index b5cb6a92816..0f91b8b28bc 100644 --- a/src/librustc_mir/shim.rs +++ b/src/librustc_mir/shim.rs @@ -26,7 +26,7 @@ pub fn provide(providers: &mut Providers<'_>) { providers.mir_shims = make_shim; } -fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> { +fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx BodyCache<'tcx> { debug!("make_shim({:?})", instance); let mut result = match instance { @@ -113,7 +113,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx bug!("creating shims from intrinsics ({:?}) is unsupported", instance) } }; - debug!("make_shim({:?}) = untransformed {:?}", instance, result); + debug!("make_shim({:?}) = untransformed {:?}", instance, result.body()); run_passes(tcx, &mut result, instance, None, MirPhase::Const, &[ &add_moves_for_packed_drops::AddMovesForPackedDrops, @@ -123,8 +123,9 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx &add_call_guards::CriticalCallEdges, ]); - debug!("make_shim({:?}) = {:?}", instance, result); + debug!("make_shim({:?}) = {:?}", instance, result.body()); + result.ensure_predecessors(); tcx.arena.alloc(result) } @@ -164,7 +165,7 @@ fn local_decls_for_sig<'tcx>(sig: &ty::FnSig<'tcx>, span: Span) .collect() } -fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option>) -> Body<'tcx> { +fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option>) -> BodyCache<'tcx> { debug!("build_drop_shim(def_id={:?}, ty={:?})", def_id, ty); // Check if this is a generator, if so, return the drop glue for it @@ -196,26 +197,20 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option>) block(&mut blocks, TerminatorKind::Goto { target: return_block }); block(&mut blocks, TerminatorKind::Return); - let mut body = Body::new( + let mut body = new_body( blocks, - IndexVec::from_elem_n( - SourceScopeData { span: span, parent_scope: None }, 1 - ), - ClearCrossCrate::Clear, local_decls_for_sig(&sig, span), - IndexVec::new(), sig.inputs().len(), - vec![], - span, - vec![], - ); + span); + + let mut body_cache = BodyCache::new(body); if let Some(..) = ty { // The first argument (index 0), but add 1 for the return value. let dropee_ptr = Place::from(Local::new(1+0)); if tcx.sess.opts.debugging_opts.mir_emit_retag { // Function arguments should be retagged, and we make this one raw. - body.basic_blocks_mut()[START_BLOCK].statements.insert(0, Statement { + body_cache.basic_blocks_mut()[START_BLOCK].statements.insert(0, Statement { source_info, kind: StatementKind::Retag(RetagKind::Raw, box(dropee_ptr.clone())), }); @@ -223,8 +218,8 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option>) let patch = { let param_env = tcx.param_env(def_id).with_reveal_all(); let mut elaborator = DropShimElaborator { - body: &body, - patch: MirPatch::new(&body), + body: body_cache.body(), + patch: MirPatch::new(body_cache.body()), tcx, param_env }; @@ -241,10 +236,32 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option>) ); elaborator.patch }; - patch.apply(&mut body); + patch.apply(&mut body_cache); } - body + body_cache +} + +fn new_body<'tcx>( + basic_blocks: IndexVec>, + local_decls: IndexVec>, + arg_count: usize, + span: Span, +) -> Body<'tcx> { + Body::new( + basic_blocks, + IndexVec::from_elem_n( + SourceScopeData { span, parent_scope: None, local_data: ClearCrossCrate::Clear }, + 1, + ), + local_decls, + IndexVec::new(), + arg_count, + vec![], + span, + vec![], + None, + ) } pub struct DropShimElaborator<'a, 'tcx> { @@ -300,7 +317,7 @@ fn array_subpath(&self, _path: Self::Path, _index: u32, _size: u32) -> Option(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -> Body<'tcx> { +fn build_clone_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -> BodyCache<'tcx> { debug!("build_clone_shim(def_id={:?})", def_id); let param_env = tcx.param_env(def_id); @@ -329,7 +346,7 @@ fn build_clone_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) - } }; - builder.into_mir() + BodyCache::new(builder.into_mir()) } struct CloneShimBuilder<'tcx> { @@ -362,18 +379,11 @@ fn new(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -> Self { } fn into_mir(self) -> Body<'tcx> { - Body::new( + new_body( self.blocks, - IndexVec::from_elem_n( - SourceScopeData { span: self.span, parent_scope: None }, 1 - ), - ClearCrossCrate::Clear, self.local_decls, - IndexVec::new(), self.sig.inputs().len(), - vec![], self.span, - vec![], ) } @@ -697,7 +707,7 @@ fn build_call_shim<'tcx>( rcvr_adjustment: Adjustment, call_kind: CallKind, untuple_args: Option<&[Ty<'tcx>]>, -) -> Body<'tcx> { +) -> BodyCache<'tcx> { debug!("build_call_shim(def_id={:?}, rcvr_adjustment={:?}, \ call_kind={:?}, untuple_args={:?})", def_id, rcvr_adjustment, call_kind, untuple_args); @@ -822,26 +832,20 @@ fn build_call_shim<'tcx>( block(&mut blocks, vec![], TerminatorKind::Resume, true); } - let mut body = Body::new( + let mut body = new_body( blocks, - IndexVec::from_elem_n( - SourceScopeData { span: span, parent_scope: None }, 1 - ), - ClearCrossCrate::Clear, local_decls, - IndexVec::new(), sig.inputs().len(), - vec![], span, - vec![], ); + if let Abi::RustCall = sig.abi { body.spread_arg = Some(Local::new(sig.inputs().len())); } - body + BodyCache::new(body) } -pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> &Body<'_> { +pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> &BodyCache<'_> { debug_assert!(tcx.is_constructor(ctor_id)); let span = tcx.hir().span_if_local(ctor_id) @@ -908,18 +912,11 @@ pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> &Body<'_> { is_cleanup: false }; - let body = Body::new( + let mut body = new_body( IndexVec::from_elem_n(start_block, 1), - IndexVec::from_elem_n( - SourceScopeData { span: span, parent_scope: None }, 1 - ), - ClearCrossCrate::Clear, local_decls, - IndexVec::new(), sig.inputs().len(), - vec![], span, - vec![], ); crate::util::dump_mir( @@ -932,5 +929,7 @@ pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> &Body<'_> { |_, _| Ok(()), ); - tcx.arena.alloc(body) + let mut body_cache = BodyCache::new(body); + body_cache.ensure_predecessors(); + tcx.arena.alloc(body_cache) }