rustc_mir_transform/
instsimplify.rs

1//! Performs various peephole optimizations.
2
3use rustc_abi::ExternAbi;
4use rustc_ast::attr;
5use rustc_hir::LangItem;
6use rustc_middle::bug;
7use rustc_middle::mir::visit::MutVisitor;
8use rustc_middle::mir::*;
9use rustc_middle::ty::layout::ValidityRequirement;
10use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, layout};
11use rustc_span::{Symbol, sym};
12
13use crate::simplify::simplify_duplicate_switch_targets;
14
15pub(super) enum InstSimplify {
16    BeforeInline,
17    AfterSimplifyCfg,
18}
19
20impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
21    fn name(&self) -> &'static str {
22        match self {
23            InstSimplify::BeforeInline => "InstSimplify-before-inline",
24            InstSimplify::AfterSimplifyCfg => "InstSimplify-after-simplifycfg",
25        }
26    }
27
28    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
29        sess.mir_opt_level() > 0
30    }
31
32    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
33        let preserve_ub_checks =
34            attr::contains_name(tcx.hir_krate_attrs(), sym::rustc_preserve_ub_checks);
35        if !preserve_ub_checks {
36            SimplifyUbCheck { tcx }.visit_body(body);
37        }
38        let ctx = InstSimplifyContext {
39            tcx,
40            local_decls: &body.local_decls,
41            typing_env: body.typing_env(tcx),
42        };
43        for block in body.basic_blocks.as_mut() {
44            for statement in block.statements.iter_mut() {
45                let StatementKind::Assign(box (.., rvalue)) = &mut statement.kind else {
46                    continue;
47                };
48
49                ctx.simplify_bool_cmp(rvalue);
50                ctx.simplify_ref_deref(rvalue);
51                ctx.simplify_ptr_aggregate(rvalue);
52                ctx.simplify_cast(rvalue);
53                ctx.simplify_repeated_aggregate(rvalue);
54                ctx.simplify_repeat_once(rvalue);
55            }
56
57            let terminator = block.terminator.as_mut().unwrap();
58            ctx.simplify_primitive_clone(terminator, &mut block.statements);
59            ctx.simplify_align_of_slice_val(terminator, &mut block.statements);
60            ctx.simplify_intrinsic_assert(terminator);
61            ctx.simplify_nounwind_call(terminator);
62            simplify_duplicate_switch_targets(terminator);
63        }
64    }
65
66    fn is_required(&self) -> bool {
67        false
68    }
69}
70
71struct InstSimplifyContext<'a, 'tcx> {
72    tcx: TyCtxt<'tcx>,
73    local_decls: &'a LocalDecls<'tcx>,
74    typing_env: ty::TypingEnv<'tcx>,
75}
76
77impl<'tcx> InstSimplifyContext<'_, 'tcx> {
78    /// Transform aggregates like [0, 0, 0, 0, 0] into [0; 5].
79    /// GVN can also do this optimization, but GVN is only run at mir-opt-level 2 so having this in
80    /// InstSimplify helps unoptimized builds.
81    fn simplify_repeated_aggregate(&self, rvalue: &mut Rvalue<'tcx>) {
82        let Rvalue::Aggregate(box AggregateKind::Array(_), fields) = &*rvalue else {
83            return;
84        };
85        if fields.len() < 5 {
86            return;
87        }
88        let (first, rest) = fields[..].split_first().unwrap();
89        let Operand::Constant(first) = first else {
90            return;
91        };
92        let Ok(first_val) = first.const_.eval(self.tcx, self.typing_env, first.span) else {
93            return;
94        };
95        if rest.iter().all(|field| {
96            let Operand::Constant(field) = field else {
97                return false;
98            };
99            let field = field.const_.eval(self.tcx, self.typing_env, field.span);
100            field == Ok(first_val)
101        }) {
102            let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
103            *rvalue = Rvalue::Repeat(Operand::Constant(first.clone()), len);
104        }
105    }
106
107    /// Transform boolean comparisons into logical operations.
108    fn simplify_bool_cmp(&self, rvalue: &mut Rvalue<'tcx>) {
109        let Rvalue::BinaryOp(op @ (BinOp::Eq | BinOp::Ne), box (a, b)) = &*rvalue else { return };
110        *rvalue = match (op, self.try_eval_bool(a), self.try_eval_bool(b)) {
111            // Transform "Eq(a, true)" ==> "a"
112            (BinOp::Eq, _, Some(true)) => Rvalue::Use(a.clone()),
113
114            // Transform "Ne(a, false)" ==> "a"
115            (BinOp::Ne, _, Some(false)) => Rvalue::Use(a.clone()),
116
117            // Transform "Eq(true, b)" ==> "b"
118            (BinOp::Eq, Some(true), _) => Rvalue::Use(b.clone()),
119
120            // Transform "Ne(false, b)" ==> "b"
121            (BinOp::Ne, Some(false), _) => Rvalue::Use(b.clone()),
122
123            // Transform "Eq(false, b)" ==> "Not(b)"
124            (BinOp::Eq, Some(false), _) => Rvalue::UnaryOp(UnOp::Not, b.clone()),
125
126            // Transform "Ne(true, b)" ==> "Not(b)"
127            (BinOp::Ne, Some(true), _) => Rvalue::UnaryOp(UnOp::Not, b.clone()),
128
129            // Transform "Eq(a, false)" ==> "Not(a)"
130            (BinOp::Eq, _, Some(false)) => Rvalue::UnaryOp(UnOp::Not, a.clone()),
131
132            // Transform "Ne(a, true)" ==> "Not(a)"
133            (BinOp::Ne, _, Some(true)) => Rvalue::UnaryOp(UnOp::Not, a.clone()),
134
135            _ => return,
136        };
137    }
138
139    fn try_eval_bool(&self, a: &Operand<'_>) -> Option<bool> {
140        let a = a.constant()?;
141        if a.const_.ty().is_bool() { a.const_.try_to_bool() } else { None }
142    }
143
144    /// Transform `&(*a)` ==> `a`.
145    fn simplify_ref_deref(&self, rvalue: &mut Rvalue<'tcx>) {
146        if let Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) = rvalue
147            && let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection()
148            && rvalue.ty(self.local_decls, self.tcx) == base.ty(self.local_decls, self.tcx).ty
149        {
150            *rvalue = Rvalue::Use(Operand::Copy(Place {
151                local: base.local,
152                projection: self.tcx.mk_place_elems(base.projection),
153            }));
154        }
155    }
156
157    /// Transform `Aggregate(RawPtr, [p, ()])` ==> `Cast(PtrToPtr, p)`.
158    fn simplify_ptr_aggregate(&self, rvalue: &mut Rvalue<'tcx>) {
159        if let Rvalue::Aggregate(box AggregateKind::RawPtr(pointee_ty, mutability), fields) = rvalue
160            && let meta_ty = fields.raw[1].ty(self.local_decls, self.tcx)
161            && meta_ty.is_unit()
162        {
163            // The mutable borrows we're holding prevent printing `rvalue` here
164            let mut fields = std::mem::take(fields);
165            let _meta = fields.pop().unwrap();
166            let data = fields.pop().unwrap();
167            let ptr_ty = Ty::new_ptr(self.tcx, *pointee_ty, *mutability);
168            *rvalue = Rvalue::Cast(CastKind::PtrToPtr, data, ptr_ty);
169        }
170    }
171
172    fn simplify_cast(&self, rvalue: &mut Rvalue<'tcx>) {
173        let Rvalue::Cast(kind, operand, cast_ty) = rvalue else { return };
174
175        let operand_ty = operand.ty(self.local_decls, self.tcx);
176        if operand_ty == *cast_ty {
177            *rvalue = Rvalue::Use(operand.clone());
178        } else if *kind == CastKind::Transmute
179            // Transmuting an integer to another integer is just a signedness cast
180            && let (ty::Int(int), ty::Uint(uint)) | (ty::Uint(uint), ty::Int(int)) =
181                (operand_ty.kind(), cast_ty.kind())
182            && int.bit_width() == uint.bit_width()
183        {
184            // The width check isn't strictly necessary, as different widths
185            // are UB and thus we'd be allowed to turn it into a cast anyway.
186            // But let's keep the UB around for codegen to exploit later.
187            // (If `CastKind::Transmute` ever becomes *not* UB for mismatched sizes,
188            // then the width check is necessary for big-endian correctness.)
189            *kind = CastKind::IntToInt;
190        }
191    }
192
193    /// Simplify `[x; 1]` to just `[x]`.
194    fn simplify_repeat_once(&self, rvalue: &mut Rvalue<'tcx>) {
195        if let Rvalue::Repeat(operand, count) = rvalue
196            && let Some(1) = count.try_to_target_usize(self.tcx)
197        {
198            *rvalue = Rvalue::Aggregate(
199                Box::new(AggregateKind::Array(operand.ty(self.local_decls, self.tcx))),
200                [operand.clone()].into(),
201            );
202        }
203    }
204
205    fn simplify_primitive_clone(
206        &self,
207        terminator: &mut Terminator<'tcx>,
208        statements: &mut Vec<Statement<'tcx>>,
209    ) {
210        let TerminatorKind::Call {
211            func, args, destination, target: Some(destination_block), ..
212        } = &terminator.kind
213        else {
214            return;
215        };
216
217        // It's definitely not a clone if there are multiple arguments
218        let [arg] = &args[..] else { return };
219
220        // Only bother looking more if it's easy to know what we're calling
221        let Some((fn_def_id, ..)) = func.const_fn_def() else { return };
222
223        // These types are easily available from locals, so check that before
224        // doing DefId lookups to figure out what we're actually calling.
225        let arg_ty = arg.node.ty(self.local_decls, self.tcx);
226
227        let ty::Ref(_region, inner_ty, Mutability::Not) = *arg_ty.kind() else { return };
228
229        if !self.tcx.is_lang_item(fn_def_id, LangItem::CloneFn)
230            || !inner_ty.is_trivially_pure_clone_copy()
231        {
232            return;
233        }
234
235        let Some(arg_place) = arg.node.place() else { return };
236
237        statements.push(Statement::new(
238            terminator.source_info,
239            StatementKind::Assign(Box::new((
240                *destination,
241                Rvalue::Use(Operand::Copy(
242                    arg_place.project_deeper(&[ProjectionElem::Deref], self.tcx),
243                )),
244            ))),
245        ));
246        terminator.kind = TerminatorKind::Goto { target: *destination_block };
247    }
248
249    // Convert `align_of_val::<[T]>(ptr)` to `align_of::<T>()`, since the
250    // alignment of a slice doesn't actually depend on metadata at all
251    // and the element type is always `Sized`.
252    //
253    // This is here so it can run after inlining, where it's more useful.
254    // (LowerIntrinsics is done in cleanup, before the optimization passes.)
255    fn simplify_align_of_slice_val(
256        &self,
257        terminator: &mut Terminator<'tcx>,
258        statements: &mut Vec<Statement<'tcx>>,
259    ) {
260        let source_info = terminator.source_info;
261        if let TerminatorKind::Call {
262            func, args, destination, target: Some(destination_block), ..
263        } = &terminator.kind
264            && args.len() == 1
265            && let Some((fn_def_id, generics)) = func.const_fn_def()
266            && self.tcx.is_intrinsic(fn_def_id, sym::align_of_val)
267            && let ty::Slice(elem_ty) = *generics.type_at(0).kind()
268        {
269            let align_def_id = self.tcx.require_lang_item(LangItem::AlignOf, source_info.span);
270            let align_const = Operand::unevaluated_constant(
271                self.tcx,
272                align_def_id,
273                &[elem_ty.into()],
274                source_info.span,
275            );
276            statements.push(Statement::new(
277                source_info,
278                StatementKind::Assign(Box::new((*destination, Rvalue::Use(align_const)))),
279            ));
280            terminator.kind = TerminatorKind::Goto { target: *destination_block };
281        }
282    }
283
284    fn simplify_nounwind_call(&self, terminator: &mut Terminator<'tcx>) {
285        let TerminatorKind::Call { ref func, ref mut unwind, .. } = terminator.kind else {
286            return;
287        };
288
289        let Some((def_id, _)) = func.const_fn_def() else {
290            return;
291        };
292
293        let body_ty = self.tcx.type_of(def_id).skip_binder();
294        let body_abi = match body_ty.kind() {
295            ty::FnDef(..) => body_ty.fn_sig(self.tcx).abi(),
296            ty::Closure(..) => ExternAbi::RustCall,
297            ty::Coroutine(..) => ExternAbi::Rust,
298            _ => bug!("unexpected body ty: {body_ty:?}"),
299        };
300
301        if !layout::fn_can_unwind(self.tcx, Some(def_id), body_abi) {
302            *unwind = UnwindAction::Unreachable;
303        }
304    }
305
306    fn simplify_intrinsic_assert(&self, terminator: &mut Terminator<'tcx>) {
307        let TerminatorKind::Call { ref func, target: ref mut target @ Some(target_block), .. } =
308            terminator.kind
309        else {
310            return;
311        };
312        let func_ty = func.ty(self.local_decls, self.tcx);
313        let Some((intrinsic_name, args)) = resolve_rust_intrinsic(self.tcx, func_ty) else {
314            return;
315        };
316        // The intrinsics we are interested in have one generic parameter
317        let [arg, ..] = args[..] else { return };
318
319        let known_is_valid =
320            intrinsic_assert_panics(self.tcx, self.typing_env, arg, intrinsic_name);
321        match known_is_valid {
322            // We don't know the layout or it's not validity assertion at all, don't touch it
323            None => {}
324            Some(true) => {
325                // If we know the assert panics, indicate to later opts that the call diverges
326                *target = None;
327            }
328            Some(false) => {
329                // If we know the assert does not panic, turn the call into a Goto
330                terminator.kind = TerminatorKind::Goto { target: target_block };
331            }
332        }
333    }
334}
335
336fn intrinsic_assert_panics<'tcx>(
337    tcx: TyCtxt<'tcx>,
338    typing_env: ty::TypingEnv<'tcx>,
339    arg: ty::GenericArg<'tcx>,
340    intrinsic_name: Symbol,
341) -> Option<bool> {
342    let requirement = ValidityRequirement::from_intrinsic(intrinsic_name)?;
343    let ty = arg.expect_ty();
344    Some(!tcx.check_validity_requirement((requirement, typing_env.as_query_input(ty))).ok()?)
345}
346
347fn resolve_rust_intrinsic<'tcx>(
348    tcx: TyCtxt<'tcx>,
349    func_ty: Ty<'tcx>,
350) -> Option<(Symbol, GenericArgsRef<'tcx>)> {
351    let ty::FnDef(def_id, args) = *func_ty.kind() else { return None };
352    let intrinsic = tcx.intrinsic(def_id)?;
353    Some((intrinsic.name, args))
354}
355
356struct SimplifyUbCheck<'tcx> {
357    tcx: TyCtxt<'tcx>,
358}
359
360impl<'tcx> MutVisitor<'tcx> for SimplifyUbCheck<'tcx> {
361    fn tcx(&self) -> TyCtxt<'tcx> {
362        self.tcx
363    }
364
365    fn visit_operand(&mut self, operand: &mut Operand<'tcx>, _: Location) {
366        if let Operand::RuntimeChecks(RuntimeChecks::UbChecks) = operand {
367            *operand = Operand::Constant(Box::new(ConstOperand {
368                span: rustc_span::DUMMY_SP,
369                user_ty: None,
370                const_: Const::Val(
371                    ConstValue::from_bool(self.tcx.sess.ub_checks()),
372                    self.tcx.types.bool,
373                ),
374            }));
375        }
376    }
377}