Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Optimize calls to copy_nonoverlapping intrinsic to assignment #81344

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 83 additions & 2 deletions compiler/rustc_mir/src/transform/instcombine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
use crate::transform::MirPass;
use rustc_hir::Mutability;
use rustc_middle::mir::{
BinOp, Body, Constant, LocalDecls, Operand, Place, ProjectionElem, Rvalue, SourceInfo,
StatementKind,
BasicBlock, LocalDecls, PlaceElem, SourceInfo, Statement, StatementKind, Terminator,
TerminatorKind,
};
use rustc_middle::mir::{BinOp, Body, Constant, Local, Operand, Place, ProjectionElem, Rvalue};
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::{sym, Symbol};
use rustc_target::spec::abi::Abi;

pub struct InstCombine;

Expand All @@ -25,6 +28,10 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
_ => {}
}
}

if let Some(terminator) = &mut block.terminator {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use block.terminator_mut(). The terminator is always Some after mir building.

ctx.combine_copy_nonoverlapping(terminator, local_decls, &mut block.statements);
}
}
}
}
Expand Down Expand Up @@ -120,4 +127,78 @@ impl<'tcx, 'a> InstCombineContext<'tcx, 'a> {
}
}
}

fn func_as_intrinsic(
&self,
operand: &Operand<'tcx>,
locals: &LocalDecls<'tcx>,
) -> Option<Symbol> {
let func_ty = operand.ty(locals, self.tcx);

if let ty::FnDef(def_id, _) = *func_ty.kind() {
let fn_sig = func_ty.fn_sig(self.tcx);

if fn_sig.abi() == Abi::RustIntrinsic {
return Some(self.tcx.item_name(def_id));
}
}

None
}

fn find_copy_nonoverlapping(
&self,
terminator: &Terminator<'tcx>,
locals: &LocalDecls<'tcx>,
) -> Option<(Local, Local, BasicBlock)> {
if let TerminatorKind::Call { func, args, destination: Some((_, next_bb)), .. } =
&terminator.kind
{
let intrinsic = self.func_as_intrinsic(func, locals)?;

if intrinsic == sym::copy_nonoverlapping && args.len() == 3 {
let src = args[0].place()?.as_local()?;
let dest = args[1].place()?.as_local()?;
let constant = args[2].constant()?;

if constant.literal.ty == self.tcx.types.usize {
let val = constant
.literal
.val
.try_to_value()?
.try_to_scalar()?
.to_machine_usize(&self.tcx)
.ok()?;

if val == 1 {
return Some((src, dest, *next_bb));
}
}
}
}

None
}

fn combine_copy_nonoverlapping(
&self,
terminator: &mut Terminator<'tcx>,
locals: &LocalDecls<'tcx>,
statements: &mut Vec<Statement<'tcx>>,
) {
if let Some((src, dest, next_bb)) = self.find_copy_nonoverlapping(terminator, locals) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: respect optimization fuel

trace!("replacing call to copy_nonoverlapping({:?}, {:?}, 1) intrinsic", src, dest);
let deref_projection = self.tcx._intern_place_elems(&[PlaceElem::Deref]);

statements.push(Statement {
source_info: terminator.source_info,
kind: StatementKind::Assign(Box::new((
Place { local: dest, projection: deref_projection },
Rvalue::Use(Operand::Copy(Place { local: src, projection: deref_projection })),
))),
});

terminator.kind = TerminatorKind::Goto { target: next_bb };
}
}
}
1 change: 1 addition & 0 deletions compiler/rustc_mir/src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
&multiple_return_terminators::MultipleReturnTerminators,
&instcombine::InstCombine,
&simplify::SimplifyCfg::new("after-instcombine"),
&const_prop::ConstProp,
&simplify_branches::SimplifyBranches::new("after-const-prop"),
&early_otherwise_branch::EarlyOtherwiseBranch,
Expand Down
Loading