1 //! Performs various peephole optimizations.
3 use crate::transform::MirPass;
4 use rustc_hir::Mutability;
5 use rustc_middle::mir::{
6 BinOp, Body, Constant, LocalDecls, Operand, Place, ProjectionElem, Rvalue, SourceInfo,
9 use rustc_middle::ty::{self, TyCtxt};
11 pub struct InstCombine;
13 impl<'tcx> MirPass<'tcx> for InstCombine {
14 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
15 let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
16 let ctx = InstCombineContext { tcx, local_decls };
17 for block in basic_blocks.iter_mut() {
18 for statement in block.statements.iter_mut() {
19 match statement.kind {
20 StatementKind::Assign(box (_place, ref mut rvalue)) => {
21 ctx.combine_bool_cmp(&statement.source_info, rvalue);
22 ctx.combine_ref_deref(&statement.source_info, rvalue);
23 ctx.combine_len(&statement.source_info, rvalue);
32 struct InstCombineContext<'tcx, 'a> {
34 local_decls: &'a LocalDecls<'tcx>,
37 impl<'tcx, 'a> InstCombineContext<'tcx, 'a> {
38 fn should_combine(&self, source_info: &SourceInfo, rvalue: &Rvalue<'tcx>) -> bool {
39 self.tcx.consider_optimizing(|| {
40 format!("InstCombine - Rvalue: {:?} SourceInfo: {:?}", rvalue, source_info)
44 /// Transform boolean comparisons into logical operations.
45 fn combine_bool_cmp(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
47 Rvalue::BinaryOp(op @ (BinOp::Eq | BinOp::Ne), box (a, b)) => {
48 let new = match (op, self.try_eval_bool(a), self.try_eval_bool(b)) {
49 // Transform "Eq(a, true)" ==> "a"
50 (BinOp::Eq, _, Some(true)) => Some(a.clone()),
52 // Transform "Ne(a, false)" ==> "a"
53 (BinOp::Ne, _, Some(false)) => Some(a.clone()),
55 // Transform "Eq(true, b)" ==> "b"
56 (BinOp::Eq, Some(true), _) => Some(b.clone()),
58 // Transform "Ne(false, b)" ==> "b"
59 (BinOp::Ne, Some(false), _) => Some(b.clone()),
61 // FIXME: Consider combining remaining comparisons into logical operations:
62 // Transform "Eq(false, b)" ==> "Not(b)"
63 // Transform "Ne(true, b)" ==> "Not(b)"
64 // Transform "Eq(a, false)" ==> "Not(a)"
65 // Transform "Ne(a, true)" ==> "Not(a)"
69 if let Some(new) = new {
70 if self.should_combine(source_info, rvalue) {
71 *rvalue = Rvalue::Use(new);
80 fn try_eval_bool(&self, a: &Operand<'_>) -> Option<bool> {
81 let a = a.constant()?;
82 if a.literal.ty.is_bool() { a.literal.val.try_to_bool() } else { None }
85 /// Transform "&(*a)" ==> "a".
86 fn combine_ref_deref(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
87 if let Rvalue::Ref(_, _, place) = rvalue {
88 if let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection() {
89 if let ty::Ref(_, _, Mutability::Not) =
90 base.ty(self.local_decls, self.tcx).ty.kind()
92 // The dereferenced place must have type `&_`, so that we don't copy `&mut _`.
97 if !self.should_combine(source_info, rvalue) {
101 *rvalue = Rvalue::Use(Operand::Copy(Place {
103 projection: self.tcx.intern_place_elems(base.projection),
109 /// Transform "Len([_; N])" ==> "N".
110 fn combine_len(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
111 if let Rvalue::Len(ref place) = *rvalue {
112 let place_ty = place.ty(self.local_decls, self.tcx).ty;
113 if let ty::Array(_, len) = place_ty.kind() {
114 if !self.should_combine(source_info, rvalue) {
118 let constant = Constant { span: source_info.span, literal: len, user_ty: None };
119 *rvalue = Rvalue::Use(Operand::Constant(box constant));