Skip to main content

rustc_mir_transform/
instsimplify.rs

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