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

Rename InstCombine to InstSimplify #111309

Merged
merged 1 commit into from
May 8, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Performs various peephole optimizations.

use crate::simplify::combine_duplicate_switch_targets;
use crate::simplify::simplify_duplicate_switch_targets;
use crate::MirPass;
use rustc_hir::Mutability;
use rustc_middle::mir::*;
Expand All @@ -10,15 +10,15 @@ use rustc_middle::ty::{self, ParamEnv, SubstsRef, Ty, TyCtxt};
use rustc_span::symbol::Symbol;
use rustc_target::abi::FieldIdx;

pub struct InstCombine;
pub struct InstSimplify;

impl<'tcx> MirPass<'tcx> for InstCombine {
impl<'tcx> MirPass<'tcx> for InstSimplify {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let ctx = InstCombineContext {
let ctx = InstSimplifyContext {
tcx,
local_decls: &body.local_decls,
param_env: tcx.param_env_reveal_all_normalized(body.source.def_id()),
Expand All @@ -27,43 +27,43 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
for statement in block.statements.iter_mut() {
match statement.kind {
StatementKind::Assign(box (_place, ref mut rvalue)) => {
ctx.combine_bool_cmp(&statement.source_info, rvalue);
ctx.combine_ref_deref(&statement.source_info, rvalue);
ctx.combine_len(&statement.source_info, rvalue);
ctx.combine_cast(&statement.source_info, rvalue);
ctx.simplify_bool_cmp(&statement.source_info, rvalue);
ctx.simplify_ref_deref(&statement.source_info, rvalue);
ctx.simplify_len(&statement.source_info, rvalue);
ctx.simplify_cast(&statement.source_info, rvalue);
}
_ => {}
}
}

ctx.combine_primitive_clone(
ctx.simplify_primitive_clone(
&mut block.terminator.as_mut().unwrap(),
&mut block.statements,
);
ctx.combine_intrinsic_assert(
ctx.simplify_intrinsic_assert(
&mut block.terminator.as_mut().unwrap(),
&mut block.statements,
);
combine_duplicate_switch_targets(block.terminator.as_mut().unwrap());
simplify_duplicate_switch_targets(block.terminator.as_mut().unwrap());
}
}
}

struct InstCombineContext<'tcx, 'a> {
struct InstSimplifyContext<'tcx, 'a> {
tcx: TyCtxt<'tcx>,
local_decls: &'a LocalDecls<'tcx>,
param_env: ParamEnv<'tcx>,
}

impl<'tcx> InstCombineContext<'tcx, '_> {
fn should_combine(&self, source_info: &SourceInfo, rvalue: &Rvalue<'tcx>) -> bool {
impl<'tcx> InstSimplifyContext<'tcx, '_> {
fn should_simplify(&self, source_info: &SourceInfo, rvalue: &Rvalue<'tcx>) -> bool {
self.tcx.consider_optimizing(|| {
format!("InstCombine - Rvalue: {:?} SourceInfo: {:?}", rvalue, source_info)
format!("InstSimplify - Rvalue: {:?} SourceInfo: {:?}", rvalue, source_info)
})
}

/// Transform boolean comparisons into logical operations.
fn combine_bool_cmp(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
fn simplify_bool_cmp(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
match rvalue {
Rvalue::BinaryOp(op @ (BinOp::Eq | BinOp::Ne), box (a, b)) => {
let new = match (op, self.try_eval_bool(a), self.try_eval_bool(b)) {
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
_ => None,
};

if let Some(new) = new && self.should_combine(source_info, rvalue) {
if let Some(new) = new && self.should_simplify(source_info, rvalue) {
*rvalue = new;
}
}
Expand All @@ -109,14 +109,14 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
}

/// Transform "&(*a)" ==> "a".
fn combine_ref_deref(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
fn simplify_ref_deref(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::Ref(_, _, place) = rvalue {
if let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection() {
if rvalue.ty(self.local_decls, self.tcx) != base.ty(self.local_decls, self.tcx).ty {
return;
}

if !self.should_combine(source_info, rvalue) {
if !self.should_simplify(source_info, rvalue) {
return;
}

Expand All @@ -129,11 +129,11 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
}

/// Transform "Len([_; N])" ==> "N".
fn combine_len(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
fn simplify_len(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::Len(ref place) = *rvalue {
let place_ty = place.ty(self.local_decls, self.tcx).ty;
if let ty::Array(_, len) = *place_ty.kind() {
if !self.should_combine(source_info, rvalue) {
if !self.should_simplify(source_info, rvalue) {
return;
}

Expand All @@ -144,7 +144,7 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
}
}

fn combine_cast(&self, _source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
fn simplify_cast(&self, _source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::Cast(kind, operand, cast_ty) = rvalue {
let operand_ty = operand.ty(self.local_decls, self.tcx);
if operand_ty == *cast_ty {
Expand Down Expand Up @@ -196,7 +196,7 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
}
}

fn combine_primitive_clone(
fn simplify_primitive_clone(
&self,
terminator: &mut Terminator<'tcx>,
statements: &mut Vec<Statement<'tcx>>,
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<'tcx> InstCombineContext<'tcx, '_> {

if !self.tcx.consider_optimizing(|| {
format!(
"InstCombine - Call: {:?} SourceInfo: {:?}",
"InstSimplify - Call: {:?} SourceInfo: {:?}",
(fn_def_id, fn_substs),
terminator.source_info
)
Expand All @@ -262,7 +262,7 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
terminator.kind = TerminatorKind::Goto { target: destination_block };
}

fn combine_intrinsic_assert(
fn simplify_intrinsic_assert(
&self,
terminator: &mut Terminator<'tcx>,
_statements: &mut Vec<Statement<'tcx>>,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod ffi_unwind_calls;
mod function_item_references;
mod generator;
mod inline;
mod instcombine;
mod instsimplify;
mod large_enums;
mod lower_intrinsics;
mod lower_slice_len;
Expand Down Expand Up @@ -547,7 +547,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&match_branches::MatchBranchSimplification,
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
&multiple_return_terminators::MultipleReturnTerminators,
&instcombine::InstCombine,
&instsimplify::InstSimplify,
&separate_const_switch::SeparateConstSwitch,
&simplify::SimplifyLocals::BeforeConstProp,
&copy_prop::CopyProp,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
}
}

pub fn combine_duplicate_switch_targets(terminator: &mut Terminator<'_>) {
pub fn simplify_duplicate_switch_targets(terminator: &mut Terminator<'_>) {
if let TerminatorKind::SwitchInt { targets, .. } = &mut terminator.kind {
let otherwise = targets.otherwise();
if targets.iter().any(|t| t.1 == otherwise) {
Expand Down Expand Up @@ -310,7 +310,7 @@ pub fn remove_duplicate_unreachable_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut B
}
}

combine_duplicate_switch_targets(terminator);
simplify_duplicate_switch_targets(terminator);

self.super_terminator(terminator, location);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ options! {
"emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
(default: no)"),
mir_enable_passes: Vec<(String, bool)> = (Vec::new(), parse_list_with_polarity, [TRACKED],
"use like `-Zmir-enable-passes=+DestProp,-InstCombine`. Forces the specified passes to be \
"use like `-Zmir-enable-passes=+DestinationPropagation,-InstSimplify`. Forces the specified passes to be \
enabled, overriding all other checks. Passes that are not specified are enabled or \
disabled by other flags as usual."),
mir_keep_place_mention: bool = (false, parse_bool, [TRACKED],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `opt1` before InstCombine
+ // MIR for `opt1` after InstCombine
- // MIR for `opt1` before InstSimplify
+ // MIR for `opt1` after InstSimplify

fn opt1(_1: bool) -> u32 {
debug x => _1; // in scope 0 at $DIR/bool_compare.rs:+0:9: +0:10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `opt2` before InstCombine
+ // MIR for `opt2` after InstCombine
- // MIR for `opt2` before InstSimplify
+ // MIR for `opt2` after InstSimplify

fn opt2(_1: bool) -> u32 {
debug x => _1; // in scope 0 at $DIR/bool_compare.rs:+0:9: +0:10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `opt3` before InstCombine
+ // MIR for `opt3` after InstCombine
- // MIR for `opt3` before InstSimplify
+ // MIR for `opt3` after InstSimplify

fn opt3(_1: bool) -> u32 {
debug x => _1; // in scope 0 at $DIR/bool_compare.rs:+0:9: +0:10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `opt4` before InstCombine
+ // MIR for `opt4` after InstCombine
- // MIR for `opt4` before InstSimplify
+ // MIR for `opt4` after InstSimplify

fn opt4(_1: bool) -> u32 {
debug x => _1; // in scope 0 at $DIR/bool_compare.rs:+0:9: +0:10
Expand Down
10 changes: 5 additions & 5 deletions tests/mir-opt/bool_compare.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// unit-test: InstCombine
// unit-test: InstSimplify

// EMIT_MIR bool_compare.opt1.InstCombine.diff
// EMIT_MIR bool_compare.opt1.InstSimplify.diff
fn opt1(x: bool) -> u32 {
if x != true { 0 } else { 1 }
}

// EMIT_MIR bool_compare.opt2.InstCombine.diff
// EMIT_MIR bool_compare.opt2.InstSimplify.diff
fn opt2(x: bool) -> u32 {
if true != x { 0 } else { 1 }
}

// EMIT_MIR bool_compare.opt3.InstCombine.diff
// EMIT_MIR bool_compare.opt3.InstSimplify.diff
fn opt3(x: bool) -> u32 {
if x == false { 0 } else { 1 }
}

// EMIT_MIR bool_compare.opt4.InstCombine.diff
// EMIT_MIR bool_compare.opt4.InstSimplify.diff
fn opt4(x: bool) -> u32 {
if false == x { 0 } else { 1 }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `redundant` before InstCombine
+ // MIR for `redundant` after InstCombine
- // MIR for `redundant` before InstSimplify
+ // MIR for `redundant` after InstSimplify

fn redundant(_1: *const &u8) -> *const &u8 {
debug x => _1; // in scope 0 at $DIR/casts.rs:+0:30: +0:31
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/casts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![crate_type = "lib"]

// EMIT_MIR casts.redundant.InstCombine.diff
// EMIT_MIR casts.redundant.InstSimplify.diff
// EMIT_MIR casts.redundant.PreCodegen.after.mir
pub fn redundant<'a, 'b: 'a>(x: *const &'a u8) -> *const &'a u8 {
generic_cast::<&'a u8, &'b u8>(x) as *const &'a u8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `norm2` before InstCombine
+ // MIR for `norm2` after InstCombine
- // MIR for `norm2` before InstSimplify
+ // MIR for `norm2` after InstSimplify

fn norm2(_1: [f32; 2]) -> f32 {
debug x => _1; // in scope 0 at $DIR/combine_array_len.rs:+0:10: +0:11
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/combine_array_len.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ignore-wasm32 compiled with panic=abort by default
// unit-test: InstCombine
// EMIT_MIR combine_array_len.norm2.InstCombine.diff
// unit-test: InstSimplify
// EMIT_MIR combine_array_len.norm2.InstSimplify.diff

fn norm2(x: [f32; 2]) -> f32 {
let a = x[0];
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/combine_clone_of_primitives.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// unit-test: InstCombine
// unit-test: InstSimplify
// ignore-wasm32 compiled with panic=abort by default

// EMIT_MIR combine_clone_of_primitives.{impl#0}-clone.InstCombine.diff
// EMIT_MIR combine_clone_of_primitives.{impl#0}-clone.InstSimplify.diff

#[derive(Clone)]
struct MyThing<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:6:10: 6:15>::clone` before InstCombine
+ // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:6:10: 6:15>::clone` after InstCombine
- // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:6:10: 6:15>::clone` before InstSimplify
+ // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:6:10: 6:15>::clone` after InstSimplify

fn <impl at $DIR/combine_clone_of_primitives.rs:6:10: 6:15>::clone(_1: &MyThing<T>) -> MyThing<T> {
debug self => _1; // in scope 0 at $DIR/combine_clone_of_primitives.rs:+0:10: +0:15
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `adt_transmutes` before InstCombine
+ // MIR for `adt_transmutes` after InstCombine
- // MIR for `adt_transmutes` before InstSimplify
+ // MIR for `adt_transmutes` after InstSimplify

fn adt_transmutes() -> () {
let mut _0: (); // return place in scope 0 at $DIR/combine_transmutes.rs:+0:32: +0:32
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `identity_transmutes` before InstCombine
+ // MIR for `identity_transmutes` after InstCombine
- // MIR for `identity_transmutes` before InstSimplify
+ // MIR for `identity_transmutes` after InstSimplify

fn identity_transmutes() -> () {
let mut _0: (); // return place in scope 0 at $DIR/combine_transmutes.rs:+0:37: +0:37
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `integer_transmutes` before InstCombine
+ // MIR for `integer_transmutes` after InstCombine
- // MIR for `integer_transmutes` before InstSimplify
+ // MIR for `integer_transmutes` after InstSimplify

fn integer_transmutes() -> () {
let mut _0: (); // return place in scope 0 at $DIR/combine_transmutes.rs:+0:36: +0:36
Expand Down
8 changes: 4 additions & 4 deletions tests/mir-opt/combine_transmutes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// unit-test: InstCombine
// unit-test: InstSimplify
// compile-flags: -C panic=abort

#![crate_type = "lib"]
Expand All @@ -8,15 +8,15 @@
use std::intrinsics::mir::*;
use std::mem::{MaybeUninit, ManuallyDrop, transmute};

// EMIT_MIR combine_transmutes.identity_transmutes.InstCombine.diff
// EMIT_MIR combine_transmutes.identity_transmutes.InstSimplify.diff
pub unsafe fn identity_transmutes() {
// These are nops and should be removed
let _a = transmute::<i32, i32>(1);
let _a = transmute::<Vec<i32>, Vec<i32>>(Vec::new());
}

#[custom_mir(dialect = "runtime", phase = "initial")]
// EMIT_MIR combine_transmutes.integer_transmutes.InstCombine.diff
// EMIT_MIR combine_transmutes.integer_transmutes.InstSimplify.diff
pub unsafe fn integer_transmutes() {
mir! {
{
Expand All @@ -30,7 +30,7 @@ pub unsafe fn integer_transmutes() {
}
}

// EMIT_MIR combine_transmutes.adt_transmutes.InstCombine.diff
// EMIT_MIR combine_transmutes.adt_transmutes.InstSimplify.diff
pub unsafe fn adt_transmutes() {
let _a: u8 = transmute(EnumNoRepr::A);
let _a: i8 = transmute(EnumNoRepr::B);
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/slice_len.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ignore-wasm32 compiled with panic=abort by default
// unit-test: ConstProp
// compile-flags: -Zmir-enable-passes=+InstCombine
// compile-flags: -Zmir-enable-passes=+InstSimplify
// EMIT_MIR_FOR_EACH_BIT_WIDTH

// EMIT_MIR slice_len.main.ConstProp.diff
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `generic` before InstCombine
+ // MIR for `generic` after InstCombine
- // MIR for `generic` before InstSimplify
+ // MIR for `generic` after InstSimplify

fn generic() -> () {
let mut _0: (); // return place in scope 0 at $DIR/dont_yeet_assert.rs:+0:21: +0:21
Expand Down
Loading