]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir/src/transform/instcombine.rs
Shrink the size of Rvalue by 16 bytes
[rust.git] / compiler / rustc_mir / src / transform / instcombine.rs
1 //! Performs various peephole optimizations.
2
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,
7     StatementKind,
8 };
9 use rustc_middle::ty::{self, TyCtxt};
10
11 pub struct InstCombine;
12
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);
24                     }
25                     _ => {}
26                 }
27             }
28         }
29     }
30 }
31
32 struct InstCombineContext<'tcx, 'a> {
33     tcx: TyCtxt<'tcx>,
34     local_decls: &'a LocalDecls<'tcx>,
35 }
36
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)
41         })
42     }
43
44     /// Transform boolean comparisons into logical operations.
45     fn combine_bool_cmp(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
46         match rvalue {
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()),
51
52                     // Transform "Ne(a, false)" ==> "a"
53                     (BinOp::Ne, _, Some(false)) => Some(a.clone()),
54
55                     // Transform "Eq(true, b)" ==> "b"
56                     (BinOp::Eq, Some(true), _) => Some(b.clone()),
57
58                     // Transform "Ne(false, b)" ==> "b"
59                     (BinOp::Ne, Some(false), _) => Some(b.clone()),
60
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)"
66                     _ => None,
67                 };
68
69                 if let Some(new) = new {
70                     if self.should_combine(source_info, rvalue) {
71                         *rvalue = Rvalue::Use(new);
72                     }
73                 }
74             }
75
76             _ => {}
77         }
78     }
79
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 }
83     }
84
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()
91                 {
92                     // The dereferenced place must have type `&_`, so that we don't copy `&mut _`.
93                 } else {
94                     return;
95                 }
96
97                 if !self.should_combine(source_info, rvalue) {
98                     return;
99                 }
100
101                 *rvalue = Rvalue::Use(Operand::Copy(Place {
102                     local: base.local,
103                     projection: self.tcx.intern_place_elems(base.projection),
104                 }));
105             }
106         }
107     }
108
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) {
115                     return;
116                 }
117
118                 let constant = Constant { span: source_info.span, literal: len, user_ty: None };
119                 *rvalue = Rvalue::Use(Operand::Constant(box constant));
120             }
121         }
122     }
123 }