Inline and remove some functions.
These are all functions with a single callsite, where having a separate function does nothing to help with readability. These changes make the code a little shorter and easier to read.
This commit is contained in:
parent
8235af07d2
commit
48064d4498
10 changed files with 341 additions and 392 deletions
|
@ -37,7 +37,169 @@ impl<'tcx> crate::MirPass<'tcx> for EnumSizeOpt {
|
|||
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
||||
// NOTE: This pass may produce different MIR based on the alignment of the target
|
||||
// platform, but it will still be valid.
|
||||
self.optim(tcx, body);
|
||||
|
||||
let mut alloc_cache = FxHashMap::default();
|
||||
let body_did = body.source.def_id();
|
||||
let param_env = tcx.param_env_reveal_all_normalized(body_did);
|
||||
|
||||
let blocks = body.basic_blocks.as_mut();
|
||||
let local_decls = &mut body.local_decls;
|
||||
|
||||
for bb in blocks {
|
||||
bb.expand_statements(|st| {
|
||||
let StatementKind::Assign(box (
|
||||
lhs,
|
||||
Rvalue::Use(Operand::Copy(rhs) | Operand::Move(rhs)),
|
||||
)) = &st.kind
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let ty = lhs.ty(local_decls, tcx).ty;
|
||||
|
||||
let (adt_def, num_variants, alloc_id) =
|
||||
self.candidate(tcx, param_env, ty, &mut alloc_cache)?;
|
||||
|
||||
let source_info = st.source_info;
|
||||
let span = source_info.span;
|
||||
|
||||
let tmp_ty = Ty::new_array(tcx, tcx.types.usize, num_variants as u64);
|
||||
let size_array_local = local_decls.push(LocalDecl::new(tmp_ty, span));
|
||||
let store_live =
|
||||
Statement { source_info, kind: StatementKind::StorageLive(size_array_local) };
|
||||
|
||||
let place = Place::from(size_array_local);
|
||||
let constant_vals = ConstOperand {
|
||||
span,
|
||||
user_ty: None,
|
||||
const_: Const::Val(
|
||||
ConstValue::Indirect { alloc_id, offset: Size::ZERO },
|
||||
tmp_ty,
|
||||
),
|
||||
};
|
||||
let rval = Rvalue::Use(Operand::Constant(Box::new(constant_vals)));
|
||||
let const_assign =
|
||||
Statement { source_info, kind: StatementKind::Assign(Box::new((place, rval))) };
|
||||
|
||||
let discr_place = Place::from(
|
||||
local_decls.push(LocalDecl::new(adt_def.repr().discr_type().to_ty(tcx), span)),
|
||||
);
|
||||
let store_discr = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
discr_place,
|
||||
Rvalue::Discriminant(*rhs),
|
||||
))),
|
||||
};
|
||||
|
||||
let discr_cast_place =
|
||||
Place::from(local_decls.push(LocalDecl::new(tcx.types.usize, span)));
|
||||
let cast_discr = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
discr_cast_place,
|
||||
Rvalue::Cast(
|
||||
CastKind::IntToInt,
|
||||
Operand::Copy(discr_place),
|
||||
tcx.types.usize,
|
||||
),
|
||||
))),
|
||||
};
|
||||
|
||||
let size_place =
|
||||
Place::from(local_decls.push(LocalDecl::new(tcx.types.usize, span)));
|
||||
let store_size = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
size_place,
|
||||
Rvalue::Use(Operand::Copy(Place {
|
||||
local: size_array_local,
|
||||
projection: tcx
|
||||
.mk_place_elems(&[PlaceElem::Index(discr_cast_place.local)]),
|
||||
})),
|
||||
))),
|
||||
};
|
||||
|
||||
let dst =
|
||||
Place::from(local_decls.push(LocalDecl::new(Ty::new_mut_ptr(tcx, ty), span)));
|
||||
let dst_ptr = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
dst,
|
||||
Rvalue::RawPtr(Mutability::Mut, *lhs),
|
||||
))),
|
||||
};
|
||||
|
||||
let dst_cast_ty = Ty::new_mut_ptr(tcx, tcx.types.u8);
|
||||
let dst_cast_place =
|
||||
Place::from(local_decls.push(LocalDecl::new(dst_cast_ty, span)));
|
||||
let dst_cast = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
dst_cast_place,
|
||||
Rvalue::Cast(CastKind::PtrToPtr, Operand::Copy(dst), dst_cast_ty),
|
||||
))),
|
||||
};
|
||||
|
||||
let src =
|
||||
Place::from(local_decls.push(LocalDecl::new(Ty::new_imm_ptr(tcx, ty), span)));
|
||||
let src_ptr = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
src,
|
||||
Rvalue::RawPtr(Mutability::Not, *rhs),
|
||||
))),
|
||||
};
|
||||
|
||||
let src_cast_ty = Ty::new_imm_ptr(tcx, tcx.types.u8);
|
||||
let src_cast_place =
|
||||
Place::from(local_decls.push(LocalDecl::new(src_cast_ty, span)));
|
||||
let src_cast = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
src_cast_place,
|
||||
Rvalue::Cast(CastKind::PtrToPtr, Operand::Copy(src), src_cast_ty),
|
||||
))),
|
||||
};
|
||||
|
||||
let deinit_old =
|
||||
Statement { source_info, kind: StatementKind::Deinit(Box::new(dst)) };
|
||||
|
||||
let copy_bytes = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Intrinsic(Box::new(
|
||||
NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping {
|
||||
src: Operand::Copy(src_cast_place),
|
||||
dst: Operand::Copy(dst_cast_place),
|
||||
count: Operand::Copy(size_place),
|
||||
}),
|
||||
)),
|
||||
};
|
||||
|
||||
let store_dead =
|
||||
Statement { source_info, kind: StatementKind::StorageDead(size_array_local) };
|
||||
|
||||
let iter = [
|
||||
store_live,
|
||||
const_assign,
|
||||
store_discr,
|
||||
cast_discr,
|
||||
store_size,
|
||||
dst_ptr,
|
||||
dst_cast,
|
||||
src_ptr,
|
||||
src_cast,
|
||||
deinit_old,
|
||||
copy_bytes,
|
||||
store_dead,
|
||||
]
|
||||
.into_iter();
|
||||
|
||||
st.make_nop();
|
||||
|
||||
Some(iter)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,185 +278,4 @@ impl EnumSizeOpt {
|
|||
let alloc = tcx.reserve_and_set_memory_alloc(tcx.mk_const_alloc(alloc));
|
||||
Some((*adt_def, num_discrs, *alloc_cache.entry(ty).or_insert(alloc)))
|
||||
}
|
||||
|
||||
fn optim<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
||||
let mut alloc_cache = FxHashMap::default();
|
||||
let body_did = body.source.def_id();
|
||||
let param_env = tcx.param_env_reveal_all_normalized(body_did);
|
||||
|
||||
let blocks = body.basic_blocks.as_mut();
|
||||
let local_decls = &mut body.local_decls;
|
||||
|
||||
for bb in blocks {
|
||||
bb.expand_statements(|st| {
|
||||
if let StatementKind::Assign(box (
|
||||
lhs,
|
||||
Rvalue::Use(Operand::Copy(rhs) | Operand::Move(rhs)),
|
||||
)) = &st.kind
|
||||
{
|
||||
let ty = lhs.ty(local_decls, tcx).ty;
|
||||
|
||||
let source_info = st.source_info;
|
||||
let span = source_info.span;
|
||||
|
||||
let (adt_def, num_variants, alloc_id) =
|
||||
self.candidate(tcx, param_env, ty, &mut alloc_cache)?;
|
||||
|
||||
let tmp_ty = Ty::new_array(tcx, tcx.types.usize, num_variants as u64);
|
||||
|
||||
let size_array_local = local_decls.push(LocalDecl::new(tmp_ty, span));
|
||||
let store_live = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::StorageLive(size_array_local),
|
||||
};
|
||||
|
||||
let place = Place::from(size_array_local);
|
||||
let constant_vals = ConstOperand {
|
||||
span,
|
||||
user_ty: None,
|
||||
const_: Const::Val(
|
||||
ConstValue::Indirect { alloc_id, offset: Size::ZERO },
|
||||
tmp_ty,
|
||||
),
|
||||
};
|
||||
let rval = Rvalue::Use(Operand::Constant(Box::new(constant_vals)));
|
||||
|
||||
let const_assign = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((place, rval))),
|
||||
};
|
||||
|
||||
let discr_place = Place::from(
|
||||
local_decls
|
||||
.push(LocalDecl::new(adt_def.repr().discr_type().to_ty(tcx), span)),
|
||||
);
|
||||
|
||||
let store_discr = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
discr_place,
|
||||
Rvalue::Discriminant(*rhs),
|
||||
))),
|
||||
};
|
||||
|
||||
let discr_cast_place =
|
||||
Place::from(local_decls.push(LocalDecl::new(tcx.types.usize, span)));
|
||||
|
||||
let cast_discr = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
discr_cast_place,
|
||||
Rvalue::Cast(
|
||||
CastKind::IntToInt,
|
||||
Operand::Copy(discr_place),
|
||||
tcx.types.usize,
|
||||
),
|
||||
))),
|
||||
};
|
||||
|
||||
let size_place =
|
||||
Place::from(local_decls.push(LocalDecl::new(tcx.types.usize, span)));
|
||||
|
||||
let store_size = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
size_place,
|
||||
Rvalue::Use(Operand::Copy(Place {
|
||||
local: size_array_local,
|
||||
projection: tcx
|
||||
.mk_place_elems(&[PlaceElem::Index(discr_cast_place.local)]),
|
||||
})),
|
||||
))),
|
||||
};
|
||||
|
||||
let dst = Place::from(
|
||||
local_decls.push(LocalDecl::new(Ty::new_mut_ptr(tcx, ty), span)),
|
||||
);
|
||||
|
||||
let dst_ptr = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
dst,
|
||||
Rvalue::RawPtr(Mutability::Mut, *lhs),
|
||||
))),
|
||||
};
|
||||
|
||||
let dst_cast_ty = Ty::new_mut_ptr(tcx, tcx.types.u8);
|
||||
let dst_cast_place =
|
||||
Place::from(local_decls.push(LocalDecl::new(dst_cast_ty, span)));
|
||||
|
||||
let dst_cast = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
dst_cast_place,
|
||||
Rvalue::Cast(CastKind::PtrToPtr, Operand::Copy(dst), dst_cast_ty),
|
||||
))),
|
||||
};
|
||||
|
||||
let src = Place::from(
|
||||
local_decls.push(LocalDecl::new(Ty::new_imm_ptr(tcx, ty), span)),
|
||||
);
|
||||
|
||||
let src_ptr = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
src,
|
||||
Rvalue::RawPtr(Mutability::Not, *rhs),
|
||||
))),
|
||||
};
|
||||
|
||||
let src_cast_ty = Ty::new_imm_ptr(tcx, tcx.types.u8);
|
||||
let src_cast_place =
|
||||
Place::from(local_decls.push(LocalDecl::new(src_cast_ty, span)));
|
||||
|
||||
let src_cast = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
src_cast_place,
|
||||
Rvalue::Cast(CastKind::PtrToPtr, Operand::Copy(src), src_cast_ty),
|
||||
))),
|
||||
};
|
||||
|
||||
let deinit_old =
|
||||
Statement { source_info, kind: StatementKind::Deinit(Box::new(dst)) };
|
||||
|
||||
let copy_bytes = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::Intrinsic(Box::new(
|
||||
NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping {
|
||||
src: Operand::Copy(src_cast_place),
|
||||
dst: Operand::Copy(dst_cast_place),
|
||||
count: Operand::Copy(size_place),
|
||||
}),
|
||||
)),
|
||||
};
|
||||
|
||||
let store_dead = Statement {
|
||||
source_info,
|
||||
kind: StatementKind::StorageDead(size_array_local),
|
||||
};
|
||||
let iter = [
|
||||
store_live,
|
||||
const_assign,
|
||||
store_discr,
|
||||
cast_discr,
|
||||
store_size,
|
||||
dst_ptr,
|
||||
dst_cast,
|
||||
src_ptr,
|
||||
src_cast,
|
||||
deinit_old,
|
||||
copy_bytes,
|
||||
store_dead,
|
||||
]
|
||||
.into_iter();
|
||||
|
||||
st.make_nop();
|
||||
Some(iter)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue