]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/instcombine.rs
Reenable feature(nll) in alloc.
[rust.git] / compiler / rustc_mir_transform / src / instcombine.rs
1 //! Performs various peephole optimizations.
2
3 use crate::MirPass;
4 use rustc_hir::Mutability;
5 use rustc_middle::mir::{
6     BinOp, Body, Constant, LocalDecls, Operand, Place, ProjectionElem, Rvalue, SourceInfo,
7     StatementKind, UnOp,
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(Rvalue::Use(a.clone())),
51
52                     // Transform "Ne(a, false)" ==> "a"
53                     (BinOp::Ne, _, Some(false)) => Some(Rvalue::Use(a.clone())),
54
55                     // Transform "Eq(true, b)" ==> "b"
56                     (BinOp::Eq, Some(true), _) => Some(Rvalue::Use(b.clone())),
57
58                     // Transform "Ne(false, b)" ==> "b"
59                     (BinOp::Ne, Some(false), _) => Some(Rvalue::Use(b.clone())),
60
61                     // Transform "Eq(false, b)" ==> "Not(b)"
62                     (BinOp::Eq, Some(false), _) => Some(Rvalue::UnaryOp(UnOp::Not, b.clone())),
63
64                     // Transform "Ne(true, b)" ==> "Not(b)"
65                     (BinOp::Ne, Some(true), _) => Some(Rvalue::UnaryOp(UnOp::Not, b.clone())),
66
67                     // Transform "Eq(a, false)" ==> "Not(a)"
68                     (BinOp::Eq, _, Some(false)) => Some(Rvalue::UnaryOp(UnOp::Not, a.clone())),
69
70                     // Transform "Ne(a, true)" ==> "Not(a)"
71                     (BinOp::Ne, _, Some(true)) => Some(Rvalue::UnaryOp(UnOp::Not, a.clone())),
72
73                     _ => None,
74                 };
75
76                 if let Some(new) = new {
77                     if self.should_combine(source_info, rvalue) {
78                         *rvalue = new;
79                     }
80                 }
81             }
82
83             _ => {}
84         }
85     }
86
87     fn try_eval_bool(&self, a: &Operand<'_>) -> Option<bool> {
88         let a = a.constant()?;
89         if a.literal.ty().is_bool() { a.literal.try_to_bool() } else { None }
90     }
91
92     /// Transform "&(*a)" ==> "a".
93     fn combine_ref_deref(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
94         if let Rvalue::Ref(_, _, place) = rvalue {
95             if let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection() {
96                 if let ty::Ref(_, _, Mutability::Not) =
97                     base.ty(self.local_decls, self.tcx).ty.kind()
98                 {
99                     // The dereferenced place must have type `&_`, so that we don't copy `&mut _`.
100                 } else {
101                     return;
102                 }
103
104                 if !self.should_combine(source_info, rvalue) {
105                     return;
106                 }
107
108                 *rvalue = Rvalue::Use(Operand::Copy(Place {
109                     local: base.local,
110                     projection: self.tcx.intern_place_elems(base.projection),
111                 }));
112             }
113         }
114     }
115
116     /// Transform "Len([_; N])" ==> "N".
117     fn combine_len(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
118         if let Rvalue::Len(ref place) = *rvalue {
119             let place_ty = place.ty(self.local_decls, self.tcx).ty;
120             if let ty::Array(_, len) = *place_ty.kind() {
121                 if !self.should_combine(source_info, rvalue) {
122                     return;
123                 }
124
125                 let constant =
126                     Constant { span: source_info.span, literal: len.into(), user_ty: None };
127                 *rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
128             }
129         }
130     }
131 }