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