rust/compiler/rustc_mir_transform/src/deaggregator.rs
Tomasz Miąsko c9dd1d9983 Make MIR basic blocks field public
This makes it possible to mutably borrow different fields of the MIR
body without resorting to methods like `basic_blocks_local_decls_mut_and_var_debug_info`.

To preserve validity of control flow graph caches in the presence of
modifications, a new struct `BasicBlocks` wraps together basic blocks
and control flow graph caches.

The `BasicBlocks` dereferences to `IndexVec<BasicBlock, BasicBlockData>`.
On the other hand a mutable access requires explicit `as_mut()` call.
2022-07-07 08:11:49 +02:00

50 lines
1.6 KiB
Rust

use crate::util::expand_aggregate;
use crate::MirPass;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
pub struct Deaggregator;
impl<'tcx> MirPass<'tcx> for Deaggregator {
fn phase_change(&self) -> Option<MirPhase> {
Some(MirPhase::Deaggregated)
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let basic_blocks = body.basic_blocks.as_mut_preserves_cfg();
for bb in basic_blocks {
bb.expand_statements(|stmt| {
// FIXME(eddyb) don't match twice on `stmt.kind` (post-NLL).
match stmt.kind {
// FIXME(#48193) Deaggregate arrays when it's cheaper to do so.
StatementKind::Assign(box (
_,
Rvalue::Aggregate(box AggregateKind::Array(_), _),
)) => {
return None;
}
StatementKind::Assign(box (_, Rvalue::Aggregate(_, _))) => {}
_ => return None,
}
let stmt = stmt.replace_nop();
let source_info = stmt.source_info;
let StatementKind::Assign(box (lhs, Rvalue::Aggregate(kind, operands))) = stmt.kind else {
bug!();
};
Some(expand_aggregate(
lhs,
operands.into_iter().map(|op| {
let ty = op.ty(&body.local_decls, tcx);
(op, ty)
}),
*kind,
source_info,
tcx,
))
});
}
}
}