forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstcombine.rs
170 lines (149 loc) · 6.32 KB
/
instcombine.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Performs various peephole optimizations.
use crate::transform::{MirPass, MirSource};
use rustc_ast::Mutability;
use rustc_data_structures::fx::FxHashMap;
use rustc_index::vec::Idx;
use rustc_middle::mir::visit::{MutVisitor, Visitor};
use rustc_middle::mir::{
BinOp, Body, Constant, Local, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue,
};
use rustc_middle::ty::{self, TyCtxt};
use std::mem;
pub struct InstCombine;
impl<'tcx> MirPass<'tcx> for InstCombine {
fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut Body<'tcx>) {
// First, find optimization opportunities. This is done in a pre-pass to keep the MIR
// read-only so that we can do global analyses on the MIR in the process (e.g.
// `Place::ty()`).
let optimizations = {
let mut optimization_finder = OptimizationFinder::new(body, tcx);
optimization_finder.visit_body(body);
optimization_finder.optimizations
};
// Then carry out those optimizations.
MutVisitor::visit_body(&mut InstCombineVisitor { optimizations, tcx }, body);
}
}
pub struct InstCombineVisitor<'tcx> {
optimizations: OptimizationList<'tcx>,
tcx: TyCtxt<'tcx>,
}
impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
if let Some(mutability) = self.optimizations.and_stars.remove(&location) {
let type_to_print = if mutability == Mutability::Not { "&" } else { "&mut" };
debug!("replacing `{}*`: {:?}", type_to_print, rvalue);
let new_place = match rvalue {
Rvalue::Ref(_, _, place) => {
if let &[ref proj_l @ .., proj_r] = place.projection.as_ref() {
place.projection = self.tcx().intern_place_elems(&[proj_r]);
Place {
// Replace with dummy
local: mem::replace(&mut place.local, Local::new(0)),
projection: self.tcx().intern_place_elems(proj_l),
}
} else {
unreachable!();
}
}
_ => bug!("Detected `{}*` but didn't find `{}*`!", type_to_print, type_to_print),
};
*rvalue = Rvalue::Use(Operand::Copy(new_place))
}
if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
debug!("replacing `Len([_; N])`: {:?}", rvalue);
*rvalue = Rvalue::Use(Operand::Constant(box constant));
}
if let Some(operand) = self.optimizations.unneeded_equality_comparison.remove(&location) {
debug!("replacing {:?} with {:?}", rvalue, operand);
*rvalue = Rvalue::Use(operand);
}
self.super_rvalue(rvalue, location)
}
}
/// Finds optimization opportunities on the MIR.
struct OptimizationFinder<'b, 'tcx> {
body: &'b Body<'tcx>,
tcx: TyCtxt<'tcx>,
optimizations: OptimizationList<'tcx>,
}
impl OptimizationFinder<'b, 'tcx> {
fn new(body: &'b Body<'tcx>, tcx: TyCtxt<'tcx>) -> OptimizationFinder<'b, 'tcx> {
OptimizationFinder { body, tcx, optimizations: OptimizationList::default() }
}
fn find_unneeded_equality_comparison(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
// find Ne(_place, false) or Ne(false, _place)
// or Eq(_place, true) or Eq(true, _place)
if let Rvalue::BinaryOp(op, l, r) = rvalue {
let const_to_find = if *op == BinOp::Ne {
false
} else if *op == BinOp::Eq {
true
} else {
return;
};
// (const, _place)
if let Some(o) = self.find_operand_in_equality_comparison_pattern(l, r, const_to_find) {
self.optimizations.unneeded_equality_comparison.insert(location, o.clone());
}
// (_place, const)
else if let Some(o) =
self.find_operand_in_equality_comparison_pattern(r, l, const_to_find)
{
self.optimizations.unneeded_equality_comparison.insert(location, o.clone());
}
}
}
fn find_operand_in_equality_comparison_pattern(
&self,
l: &Operand<'tcx>,
r: &'a Operand<'tcx>,
const_to_find: bool,
) -> Option<&'a Operand<'tcx>> {
let const_ = l.constant()?;
if const_.literal.ty == self.tcx.types.bool
&& const_.literal.val.try_to_bool() == Some(const_to_find)
{
if r.place().is_some() {
return Some(r);
}
}
return None;
}
}
impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
if let Rvalue::Ref(_, borrowkind, place) = rvalue {
if let PlaceRef { local, projection: &[ref proj_base @ .., ProjectionElem::Deref] } =
place.as_ref()
{
// The dereferenced place must have a reference type with the same mutability
let ty = Place::ty_from(local, proj_base, self.body, self.tcx).ty;
let mutability = borrowkind.mutability();
if matches!(ty.kind(), ty::Ref(_, _, derefed_place_mutability) if *derefed_place_mutability == mutability)
{
self.optimizations.and_stars.insert(location, mutability);
}
}
}
if let Rvalue::Len(ref place) = *rvalue {
let place_ty = place.ty(&self.body.local_decls, self.tcx).ty;
if let ty::Array(_, len) = place_ty.kind() {
let span = self.body.source_info(location).span;
let constant = Constant { span, literal: len, user_ty: None };
self.optimizations.arrays_lengths.insert(location, constant);
}
}
self.find_unneeded_equality_comparison(rvalue, location);
self.super_rvalue(rvalue, location)
}
}
#[derive(Default)]
struct OptimizationList<'tcx> {
and_stars: FxHashMap<Location, Mutability>,
arrays_lengths: FxHashMap<Location, Constant<'tcx>>,
unneeded_equality_comparison: FxHashMap<Location, Operand<'tcx>>,
}