-
Notifications
You must be signed in to change notification settings - Fork 13.2k
/
Copy pathinstcombine.rs
330 lines (293 loc) · 12.7 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! Performs various peephole optimizations.
use crate::transform::MirPass;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::Mutability;
use rustc_index::vec::Idx;
use rustc_middle::mir::{
visit::PlaceContext,
visit::{MutVisitor, Visitor},
Statement,
};
use rustc_middle::mir::{
BinOp, Body, BorrowKind, 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>, 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
};
if !optimizations.is_empty() {
// 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> InstCombineVisitor<'tcx> {
fn should_combine(&self, rvalue: &Rvalue<'tcx>, location: Location) -> bool {
self.tcx.consider_optimizing(|| {
format!("InstCombine - Rvalue: {:?} Location: {:?}", rvalue, location)
})
}
}
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 self.optimizations.and_stars.remove(&location) && self.should_combine(rvalue, location) {
debug!("replacing `&*`: {:?}", 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 `&*`!"),
};
*rvalue = Rvalue::Use(Operand::Copy(new_place))
}
if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
if self.should_combine(rvalue, location) {
debug!("replacing `Len([_; N])`: {:?}", rvalue);
*rvalue = Rvalue::Use(Operand::Constant(box constant));
}
}
if let Some(operand) = self.optimizations.unneeded_equality_comparison.remove(&location) {
if self.should_combine(rvalue, location) {
debug!("replacing {:?} with {:?}", rvalue, operand);
*rvalue = Rvalue::Use(operand);
}
}
if let Some(place) = self.optimizations.unneeded_deref.remove(&location) {
if self.should_combine(rvalue, location) {
debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
*rvalue = Rvalue::Use(Operand::Copy(place));
}
}
// We do not call super_rvalue as we are not interested in any other parts of the tree
}
}
struct MutatingUseVisitor {
has_mutating_use: bool,
local_to_look_for: Local,
}
impl MutatingUseVisitor {
fn has_mutating_use_in_stmt(local: Local, stmt: &Statement<'tcx>, location: Location) -> bool {
let mut _self = Self { has_mutating_use: false, local_to_look_for: local };
_self.visit_statement(stmt, location);
_self.has_mutating_use
}
}
impl<'tcx> Visitor<'tcx> for MutatingUseVisitor {
fn visit_local(&mut self, local: &Local, context: PlaceContext, _: Location) {
if *local == self.local_to_look_for {
self.has_mutating_use |= context.is_mutating_use();
}
}
}
/// 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_deref_of_address(&mut self, rvalue: &Rvalue<'tcx>, location: Location) -> Option<()> {
// FIXME(#78192): This optimization can result in unsoundness.
if !self.tcx.sess.opts.debugging_opts.unsound_mir_opts {
return None;
}
// Look for the sequence
//
// _2 = &_1;
// ...
// _5 = (*_2);
//
// which we can replace the last statement with `_5 = _1;` to avoid the load of `_2`.
if let Rvalue::Use(op) = rvalue {
let local_being_derefed = match op.place()?.as_ref() {
PlaceRef { local, projection: [ProjectionElem::Deref] } => Some(local),
_ => None,
}?;
let mut dead_locals_seen = vec![];
let stmt_index = location.statement_index;
// Look behind for statement that assigns the local from a address of operator.
// 6 is chosen as a heuristic determined by seeing the number of times
// the optimization kicked in compiling rust std.
let lower_index = stmt_index.saturating_sub(6);
let statements_to_look_in = self.body.basic_blocks()[location.block].statements
[lower_index..stmt_index]
.iter()
.rev();
for stmt in statements_to_look_in {
match &stmt.kind {
// Exhaustive match on statements to detect conditions that warrant we bail out of the optimization.
rustc_middle::mir::StatementKind::Assign(box (l, r))
if l.local == local_being_derefed =>
{
match r {
// Looking for immutable reference e.g _local_being_deref = &_1;
Rvalue::Ref(
_,
// Only apply the optimization if it is an immutable borrow.
BorrowKind::Shared,
place_taken_address_of,
) => {
// Make sure that the place has not been marked dead
if dead_locals_seen.contains(&place_taken_address_of.local) {
return None;
}
self.optimizations
.unneeded_deref
.insert(location, *place_taken_address_of);
return Some(());
}
// We found an assignment of `local_being_deref` that is not an immutable ref, e.g the following sequence
// _2 = &_1;
// _3 = &5
// _2 = _3; <-- this means it is no longer valid to replace the last statement with `_5 = _1;`
// _5 = (*_2);
_ => return None,
}
}
// Inline asm can do anything, so bail out of the optimization.
rustc_middle::mir::StatementKind::LlvmInlineAsm(_) => return None,
// Remember `StorageDead`s, as the local being marked dead could be the
// place RHS we are looking for, in which case we need to abort to avoid UB
// using an uninitialized place
rustc_middle::mir::StatementKind::StorageDead(dead) => {
dead_locals_seen.push(*dead)
}
// Check that `local_being_deref` is not being used in a mutating way which can cause misoptimization.
rustc_middle::mir::StatementKind::Assign(box (_, _))
| rustc_middle::mir::StatementKind::Coverage(_)
| rustc_middle::mir::StatementKind::Nop
| rustc_middle::mir::StatementKind::FakeRead(_, _)
| rustc_middle::mir::StatementKind::StorageLive(_)
| rustc_middle::mir::StatementKind::Retag(_, _)
| rustc_middle::mir::StatementKind::AscribeUserType(_, _)
| rustc_middle::mir::StatementKind::SetDiscriminant { .. } => {
if MutatingUseVisitor::has_mutating_use_in_stmt(
local_being_derefed,
stmt,
location,
) {
return None;
}
}
}
}
}
Some(())
}
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);
}
}
None
}
}
impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
if let Rvalue::Ref(_, _, place) = rvalue {
if let Some((place_base, ProjectionElem::Deref)) = place.as_ref().last_projection() {
// The dereferenced place must have type `&_`.
let ty = place_base.ty(self.body, self.tcx).ty;
if let ty::Ref(_, _, Mutability::Not) = ty.kind() {
self.optimizations.and_stars.insert(location);
}
}
}
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);
}
}
let _ = self.find_deref_of_address(rvalue, location);
self.find_unneeded_equality_comparison(rvalue, location);
// We do not call super_rvalue as we are not interested in any other parts of the tree
}
}
#[derive(Default)]
struct OptimizationList<'tcx> {
and_stars: FxHashSet<Location>,
arrays_lengths: FxHashMap<Location, Constant<'tcx>>,
unneeded_equality_comparison: FxHashMap<Location, Operand<'tcx>>,
unneeded_deref: FxHashMap<Location, Place<'tcx>>,
}
impl<'tcx> OptimizationList<'tcx> {
fn is_empty(&self) -> bool {
match self {
OptimizationList {
and_stars,
arrays_lengths,
unneeded_equality_comparison,
unneeded_deref,
} => {
and_stars.is_empty()
&& arrays_lengths.is_empty()
&& unneeded_equality_comparison.is_empty()
&& unneeded_deref.is_empty()
}
}
}
}