1 //! Performs various peephole optimizations.
4 use rustc_hir::Mutability;
5 use rustc_middle::mir::{
6 BinOp, Body, Constant, ConstantKind, LocalDecls, Operand, Place, ProjectionElem, Rvalue,
7 SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp,
9 use rustc_middle::ty::{self, TyCtxt};
11 pub struct InstCombine;
13 impl<'tcx> MirPass<'tcx> for InstCombine {
14 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
15 sess.mir_opt_level() > 0
18 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
19 let ctx = InstCombineContext { tcx, local_decls: &body.local_decls };
20 for block in body.basic_blocks.as_mut() {
21 for statement in block.statements.iter_mut() {
22 match statement.kind {
23 StatementKind::Assign(box (_place, ref mut rvalue)) => {
24 ctx.combine_bool_cmp(&statement.source_info, rvalue);
25 ctx.combine_ref_deref(&statement.source_info, rvalue);
26 ctx.combine_len(&statement.source_info, rvalue);
32 ctx.combine_primitive_clone(
33 &mut block.terminator.as_mut().unwrap(),
34 &mut block.statements,
40 struct InstCombineContext<'tcx, 'a> {
42 local_decls: &'a LocalDecls<'tcx>,
45 impl<'tcx> InstCombineContext<'tcx, '_> {
46 fn should_combine(&self, source_info: &SourceInfo, rvalue: &Rvalue<'tcx>) -> bool {
47 self.tcx.consider_optimizing(|| {
48 format!("InstCombine - Rvalue: {:?} SourceInfo: {:?}", rvalue, source_info)
52 /// Transform boolean comparisons into logical operations.
53 fn combine_bool_cmp(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
55 Rvalue::BinaryOp(op @ (BinOp::Eq | BinOp::Ne), box (a, b)) => {
56 let new = match (op, self.try_eval_bool(a), self.try_eval_bool(b)) {
57 // Transform "Eq(a, true)" ==> "a"
58 (BinOp::Eq, _, Some(true)) => Some(Rvalue::Use(a.clone())),
60 // Transform "Ne(a, false)" ==> "a"
61 (BinOp::Ne, _, Some(false)) => Some(Rvalue::Use(a.clone())),
63 // Transform "Eq(true, b)" ==> "b"
64 (BinOp::Eq, Some(true), _) => Some(Rvalue::Use(b.clone())),
66 // Transform "Ne(false, b)" ==> "b"
67 (BinOp::Ne, Some(false), _) => Some(Rvalue::Use(b.clone())),
69 // Transform "Eq(false, b)" ==> "Not(b)"
70 (BinOp::Eq, Some(false), _) => Some(Rvalue::UnaryOp(UnOp::Not, b.clone())),
72 // Transform "Ne(true, b)" ==> "Not(b)"
73 (BinOp::Ne, Some(true), _) => Some(Rvalue::UnaryOp(UnOp::Not, b.clone())),
75 // Transform "Eq(a, false)" ==> "Not(a)"
76 (BinOp::Eq, _, Some(false)) => Some(Rvalue::UnaryOp(UnOp::Not, a.clone())),
78 // Transform "Ne(a, true)" ==> "Not(a)"
79 (BinOp::Ne, _, Some(true)) => Some(Rvalue::UnaryOp(UnOp::Not, a.clone())),
84 if let Some(new) = new && self.should_combine(source_info, rvalue) {
93 fn try_eval_bool(&self, a: &Operand<'_>) -> Option<bool> {
94 let a = a.constant()?;
95 if a.literal.ty().is_bool() { a.literal.try_to_bool() } else { None }
98 /// Transform "&(*a)" ==> "a".
99 fn combine_ref_deref(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
100 if let Rvalue::Ref(_, _, place) = rvalue {
101 if let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection() {
102 if let ty::Ref(_, _, Mutability::Not) =
103 base.ty(self.local_decls, self.tcx).ty.kind()
105 // The dereferenced place must have type `&_`, so that we don't copy `&mut _`.
110 if !self.should_combine(source_info, rvalue) {
114 *rvalue = Rvalue::Use(Operand::Copy(Place {
116 projection: self.tcx.intern_place_elems(base.projection),
122 /// Transform "Len([_; N])" ==> "N".
123 fn combine_len(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
124 if let Rvalue::Len(ref place) = *rvalue {
125 let place_ty = place.ty(self.local_decls, self.tcx).ty;
126 if let ty::Array(_, len) = *place_ty.kind() {
127 if !self.should_combine(source_info, rvalue) {
131 let literal = ConstantKind::from_const(len, self.tcx);
132 let constant = Constant { span: source_info.span, literal, user_ty: None };
133 *rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
138 fn combine_primitive_clone(
140 terminator: &mut Terminator<'tcx>,
141 statements: &mut Vec<Statement<'tcx>>,
143 let TerminatorKind::Call { func, args, destination, target, .. } = &mut terminator.kind
146 // It's definitely not a clone if there are multiple arguments
151 let Some(destination_block) = *target
154 // Only bother looking more if it's easy to know what we're calling
155 let Some((fn_def_id, fn_substs)) = func.const_fn_def()
158 // Clone needs one subst, so we can cheaply rule out other stuff
159 if fn_substs.len() != 1 {
163 // These types are easily available from locals, so check that before
164 // doing DefId lookups to figure out what we're actually calling.
165 let arg_ty = args[0].ty(self.local_decls, self.tcx);
167 let ty::Ref(_region, inner_ty, Mutability::Not) = *arg_ty.kind()
170 if !inner_ty.is_trivially_pure_clone_copy() {
174 let trait_def_id = self.tcx.trait_of_item(fn_def_id);
175 if trait_def_id.is_none() || trait_def_id != self.tcx.lang_items().clone_trait() {
179 if !self.tcx.consider_optimizing(|| {
181 "InstCombine - Call: {:?} SourceInfo: {:?}",
182 (fn_def_id, fn_substs),
183 terminator.source_info
189 let Some(arg_place) = args.pop().unwrap().place()
192 statements.push(Statement {
193 source_info: terminator.source_info,
194 kind: StatementKind::Assign(Box::new((
196 Rvalue::Use(Operand::Copy(
197 arg_place.project_deeper(&[ProjectionElem::Deref], self.tcx),
201 terminator.kind = TerminatorKind::Goto { target: destination_block };